ScrollArea unstable
A native scroll container with custom scrollbars, overflow state, and edge-fade CSS variables.
ScrollArea is a headless component: it keeps hover, per-axis scrolling,
and overflow state, measures the viewport and content, and handles scrollbar
wheel, track press, and thumb dragging. Your view supplies the markup and
styles.
Anatomy
| Part | Element |
|---|---|
root |
<div>, role="presentation", position: relative |
viewport |
<div>, scrollable container (overflow: scroll; scrollbar-width: none) |
content |
<div>, role="presentation", min-width: fit-content |
scrollbar |
<div> track with its thumb <div> placed inside, after the track's existing children |
corner |
<div> at the intersection of both scrollbars |
scrollbar takes { element, thumb, orientation?, keepMounted? }:
element is the track and thumb is the draggable handle placed inside it.
orientation defaults to "vertical". scrollbar returns undefined when
that axis does not overflow unless keepMounted is true. corner returns
undefined unless both axes overflow.
Styling and CSS variables
The viewport sets scrollbar-width: none inline to hide native scrollbars in
modern browsers. On older WebKit versions that do not support
scrollbar-width, add a ::-webkit-scrollbar { display: none; } rule in
your stylesheet.
The mount writes these CSS custom properties directly to inline styles so scrolling does not re-render:
root:--scroll-area-corner-width,--scroll-area-corner-heightviewport:--scroll-area-overflow-x-start,--scroll-area-overflow-x-end,--scroll-area-overflow-y-start,--scroll-area-overflow-y-endscrollbar:--scroll-area-thumb-height(vertical) or--scroll-area-thumb-width(horizontal)
On browsers that support CSS.registerProperty (except WebKit), the four
--scroll-area-overflow-* properties are registered with inherits: false so
updating them on scroll does not invalidate styles across the viewport
subtree or leak into nested scroll areas. Children inside the viewport that
want to read them can opt in with --scroll-area-overflow-y-start: inherit.
On WebKit, those properties still inherit, so an outer scroll area's values
can reach a nested one until it measures.
Data attributes
Each present with an empty value or absent, except data-orientation:
root,viewport,content:data-scrolling,data-has-overflow-x,data-has-overflow-y,data-overflow-x-start,data-overflow-x-end,data-overflow-y-start,data-overflow-y-endscrollbar:data-orientation("vertical"or"horizontal"),data-hovering, per-axisdata-scrolling, and the overflow attributes abovethumb:data-orientation, per-axisdata-scrolling
Keyboard
viewport receives tabindex="0" when content overflows on either axis and
tabindex="-1" otherwise, so native keyboard scrolling (arrow keys, Page
Up/Down, Home/End, Space) works when focused.
Limits
rootcarries the component's mount, so it cannot take a mount of your own.- On the server, and on a host that does not run mounts, overflow is not measured.
Example A vertical scroll area with a custom scrollbar
import { Html, HtmlServer } from "effect/ui/html"import { ScrollArea } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Panel = Html.component({ name: "Panel", model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, view: (h) => ScrollArea(h, { view: (scrollArea) => scrollArea.root(h.div.class("area")).children( scrollArea.viewport(h.div.class("viewport")).children( scrollArea.content(h.div.class("content")).children(h.p("Long text")) ), scrollArea.scrollbar({ element: h.div.class("bar"), thumb: h.div.class("thumb"), keepMounted: true }) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Panel, {}))if (!html.includes('class="area"') || !html.includes('role="presentation"')) { throw new Error("expected scroll area markup")}Components
ScrollArea
unstableA headless scroll area. Call it with the consumer's h, optional props,
and a view closure that draws root, viewport, content, one or two
scrollbar parts, and an optional corner.
Details
The parts object also carries scrolling, scrollingX, scrollingY,
hovering, hasOverflowX, hasOverflowY, overflowXStart,
overflowXEnd, overflowYStart, overflowYEnd, and cornerHidden for
markup that depends on state.
scrollbar({ element, thumb, orientation?, keepMounted? }) returns
element with thumb placed inside, or undefined when that axis does
not overflow unless keepMounted is true. The thumb comes after the
children already on element and before any chained on the result.
corner returns undefined unless both axes overflow. Both validate their element tags
before deciding whether to return undefined.
The component's own attributes win over those set on a builder before the
part: role on root and content, tabindex on viewport,
aria-hidden on scrollbar and corner, and the data attributes on every
part. Layout and sizing inline styles are appended after the consumer's
inline style.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const ScrollArea: Headless<{ readonly overflowEdgeThreshold?: number | { readonly xEnd?: number; readonly xStart?: number; readonly yEnd?: number; readonly yStart?: number; };}, never, { content: Part<"div", [], never>; corner: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; cornerHidden: boolean; hasOverflowX: boolean; hasOverflowY: boolean; hovering: boolean; overflowXEnd: boolean; overflowXStart: boolean; overflowYEnd: boolean; overflowYStart: boolean; root: Part<"div", [], never>; scrollbar: <B extends PartTarget<"div">, T extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly keepMounted?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly thumb: T; }) => WithRequirements<B, Requirements<T>> | undefined & Pick<Part<"div", [orientation: "horizontal" | "vertical", thumb: Content<unknown, any>], never>, "~R">; scrolling: boolean; scrollingX: boolean; scrollingY: boolean; viewport: Part<"div", [], never>;}, never, never, never, never, never, never, { readonly hasOverflowX: boolean; readonly hasOverflowY: boolean; readonly hovering: boolean; readonly measured: boolean; readonly overflowXEnd: boolean; readonly overflowXStart: boolean; readonly overflowYEnd: boolean; readonly overflowYStart: boolean; readonly scrollingX: boolean; readonly scrollingY: boolean;}, { readonly _tag: "StartedScrolling"; readonly orientation: "horizontal" | "vertical";} | { readonly _tag: "StoppedScrolling"; readonly orientation: "horizontal" | "vertical";} | { readonly _tag: "EnteredPointer";} | { readonly _tag: "LeftPointer";} | { readonly _tag: "MeasuredOverflow"; readonly hasOverflowX: boolean; readonly hasOverflowY: boolean; readonly overflowXEnd: boolean; readonly overflowXStart: boolean; readonly overflowYEnd: boolean; readonly overflowYStart: boolean;}, QueryEntries<{}, {}>>Models
ScrollAreaOrientation
unstableThe orientation of a scrollbar.
Signature
declare const ScrollAreaOrientation: Literals<readonly ["vertical", "horizontal"]>The orientation of a scrollbar.
Signature
type ScrollAreaOrientation = typeof ScrollAreaOrientation.TypeScrollAreaOverflowEdgeThreshold
unstableThe threshold in pixels that must be passed before the overflow edge attributes are applied. Pass a single number for all four edges or an object to configure them individually.
Signature
declare const ScrollAreaOverflowEdgeThreshold: Union<readonly [Number, Struct<{ readonly xEnd: optional<Number>; readonly xStart: optional<Number>; readonly yEnd: optional<Number>; readonly yStart: optional<Number>;}>]>The threshold in pixels that must be passed before the overflow edge attributes are applied.
Signature
type ScrollAreaOverflowEdgeThreshold = typeof ScrollAreaOverflowEdgeThreshold.TypeScrollAreaProps
unstableSchema for ScrollAreaProps.
Signature
declare const ScrollAreaProps: Struct<{ readonly overflowEdgeThreshold: optional<Union<readonly [Number, Struct<{ readonly xEnd: optional<Number>; readonly xStart: optional<Number>; readonly yEnd: optional<Number>; readonly yStart: optional<Number>; }>]>>;}>The props of a ScrollArea.
Signature
type ScrollAreaProps = typeof ScrollAreaProps.Type