Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/scroll-area.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { ScrollArea, ScrollAreaOrientation } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type ScrollAreaParts = Component.Headless.Parts<typeof ScrollArea>
    
    // Each mask is one literal, so Tailwind's scanner sees the whole class.
    const fades = {
      none: "",
      y: "[mask-image:linear-gradient(to_bottom,transparent,black_min(40px,var(--scroll-area-overflow-y-start,0px)),black_calc(100%_-_min(40px,var(--scroll-area-overflow-y-end,0px))),transparent)]",
      x: "[mask-image:linear-gradient(to_right,transparent,black_min(40px,var(--scroll-area-overflow-x-start,0px)),black_calc(100%_-_min(40px,var(--scroll-area-overflow-x-end,0px))),transparent)]"
    } as const
    
    const visibilities = {
      hover: "opacity-0 data-[hovering]:opacity-100 data-[scrolling]:opacity-100",
      always: "opacity-100"
    } as const
    
    export type ScrollAreaFade = keyof typeof fades
    export type ScrollAreaScrollbarVisibility = keyof typeof visibilities
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /** The frame. Give it a size with `class`: `ScrollAreaRoot(h, scrollArea, { class: "h-64 w-56" })`. */
    export const ScrollAreaRoot = <Msg>(h: Html.H<Msg>, scrollArea: ScrollAreaParts, options: ClassOption = {}) =>
      scrollArea.root(h.div.class(cn("overflow-hidden rounded-md border bg-background", options.class)))
    
    /**
     * The element that scrolls. `fade` masks the edges the content overflows,
     * from the kit's `--scroll-area-overflow-*` variables, so an edge with nothing
     * beyond it stays sharp.
     */
    export const ScrollAreaViewport = <Msg>(
      h: Html.H<Msg>,
      scrollArea: ScrollAreaParts,
      options: ClassOption & { readonly fade?: ScrollAreaFade | undefined } = {}
    ) =>
      scrollArea.viewport(
        h.div.class(cn(
          "size-full overscroll-contain rounded-[inherit] -outline-offset-2 [&::-webkit-scrollbar]:hidden",
          fades[options.fade ?? "none"],
          options.class
        ))
      )
    
    /** The content. Padding goes here, not on the viewport. */
    export const ScrollAreaContent = <Msg>(h: Html.H<Msg>, scrollArea: ScrollAreaParts, options: ClassOption = {}) =>
      scrollArea.content(h.div.class(cn(options.class)))
    
    /**
     * A scrollbar with its thumb. `undefined` while its axis doesn't overflow,
     * unless `keepMounted`. It shows on hover and while scrolling, or always.
     */
    export const ScrollAreaScrollbar = <Msg>(
      h: Html.H<Msg>,
      scrollArea: ScrollAreaParts,
      options: ClassOption & {
        readonly orientation?: ScrollAreaOrientation | undefined
        readonly keepMounted?: boolean | undefined
        readonly visibility?: ScrollAreaScrollbarVisibility | undefined
      } = {}
    ) =>
      scrollArea.scrollbar({
        orientation: options.orientation,
        keepMounted: options.keepMounted,
        element: h.div.class(cn(
          "z-10 flex touch-none p-0.5 transition-opacity duration-150 select-none " +
            "data-[orientation=horizontal]:h-2.5 data-[orientation=horizontal]:flex-col " +
            "data-[orientation=vertical]:w-2.5",
          visibilities[options.visibility ?? "hover"],
          options.class
        )),
        thumb: h.div.class(
          "flex-1 rounded-full bg-foreground/25 transition-colors duration-150 hover:bg-foreground/40 " +
            "data-[scrolling]:bg-foreground/40"
        )
      })
    
    /** The square where two scrollbars meet. `undefined` unless both axes overflow. */
    export const ScrollAreaCorner = <Msg>(h: Html.H<Msg>, scrollArea: ScrollAreaParts, options: ClassOption = {}) =>
      scrollArea.corner(h.div.class(cn("bg-transparent", options.class)))
    
  3. Import it

    import { ScrollAreaRoot, ScrollAreaViewport, ScrollAreaContent, ScrollAreaScrollbar, ScrollAreaCorner } from "#ui/scroll-area"
    

Usage

Vertical list.ts
import { Html } from "@effect/ui/html"
import { ScrollArea } from "@effect/ui-kit"
import { Schema } from "effect"
import { ScrollAreaContent, ScrollAreaRoot, ScrollAreaScrollbar, ScrollAreaViewport } from "#ui/scroll-area"
import { Separator } from "#ui/separator"

const versions = Array.from({ length: 30 }, (_, index) => `v0.${30 - index}.0`)

export const ScrollAreaVertical = Html.component({
  name: "ScrollAreaVertical",
  model: Schema.Struct({}),
  message: Schema.Never
}).make({
  init: () => ({}),
  update: ($) => $.model,
  view: (h) =>
    ScrollArea(h, {
      view: (scrollArea) =>
        ScrollAreaRoot(h, scrollArea, { class: "h-72 w-56" }).children(
          ScrollAreaViewport(h, scrollArea).children(
            ScrollAreaContent(h, scrollArea, { class: "p-4" }).children(
              h.h4.class("m-0 mb-3 text-sm font-medium").children("Releases"),
              versions.flatMap((version, index) => [
                index > 0 ? Separator(h, { class: "my-2" }) : undefined,
                h.div.class("font-mono text-[13px]").children(version)
              ])
            )
          ),
          ScrollAreaScrollbar(h, scrollArea)
        )
    })
})

Examples

Horizontal gallery

A row of cards behind a horizontal scrollbar.

Both axes

A wide table with two scrollbars and the corner where they meet.

Edge fade

A mask built from the kit's overflow variables fades only the edges with more content beyond them.

In a card

A list of notifications that scrolls between a card's header and footer.

Notifications

10 unread

12 notifications this week

Always visible

A scrollbar that stays mounted and visible, and a hint read from the overflow state.

Everything fits

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <div> frame, position: relative.
viewportThe <div> that scrolls. Focusable while the content overflows.
contentThe <div> around the content, which the kit measures.
scrollbar({ element, thumb, orientation?, keepMounted? })The track with its thumb inside. undefined while that axis doesn't overflow, unless keepMounted.
cornerThe <div> where two scrollbars meet. undefined unless both axes overflow.
hasOverflowX, overflowYEnd, scrolling, hovering ...Plain values for markup that depends on the state.

API reference

Props

PropTypeDescription
overflowEdgeThresholdnumber | { xStart?, xEnd?, yStart?, yEnd? }Pixels that must be scrolled past before an overflow edge attribute applies.

Data attributes

AttributeDescription
data-has-overflow-x / data-has-overflow-yOn root, viewport, content and scrollbar while that axis overflows.
data-overflow-x-start / -x-end / -y-start / -y-endWhile there is content beyond that edge.
data-scrollingWhile the user scrolls. Per axis on a scrollbar and its thumb.
data-hoveringOn a scrollbar while the pointer is over the scroll area.
data-orientation"vertical" or "horizontal", on a scrollbar and its thumb.
--scroll-area-overflow-x-start / -x-end / -y-start / -y-endCSS variables on the viewport: the distance to each edge in px.
--scroll-area-thumb-height / --scroll-area-thumb-widthCSS variables on a scrollbar: the size of its thumb.
--scroll-area-corner-width / --scroll-area-corner-heightCSS variables on the root: the size of the corner.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.
  • Separator: A horizontal or vertical divider between sections of content.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.