Scroll Area
A scrolling region with its scrollbars drawn by hand: a faint pen line for the track, a heavier doubled one for the thumb.
import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
const tags = Array.from({ length: 40 }, (_, i) => `v1.2.0-beta.${40 - i}`);
export default function ScrollAreaDemo() {
return (
<ScrollArea seed="scroll-demo" className="h-72 w-56">
<div className="p-4">
<h4 className="mb-3 text-lg font-bold">Tags</h4>
{tags.map((tag) => (
<div key={tag}>
<div className="py-1.5 text-base text-ink-2">{tag}</div>
<Separator />
</div>
))}
</div>
</ScrollArea>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ scroll-area"use client";
import { useMemo, type CSSProperties } from "react";
import { ScrollArea as ScrollAreaPrimitive } from "@base-ui/react/scroll-area";
import { cn } from "@/lib/utils";
import { penStyle, useInkSeed, usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke } from "@/lib/ink";
import { ruleStroke, verticalStroke } from "@/lib/ink-sketch";
/**
* A scrolling region with the scrollbar drawn by hand: a faint pen line for
* the track and a heavier, doubled one for the thumb. A scrollbar is there,
* faintly, whenever there is more to see, and darkens when you point at the
* area or scroll it.
*/
function ScrollArea({
className,
children,
seed,
weight,
speed,
...props
}: ScrollAreaPrimitive.Root.Props & Pick<Pen, "weight" | "speed"> & { seed?: string | number }) {
return (
<ScrollAreaPrimitive.Root data-slot="scroll-area" className={inkClassName("group/scroll-area relative overflow-hidden", className)} {...props}>
<ScrollAreaPrimitive.Viewport
data-slot="scroll-area-viewport"
className="size-full rounded-[inherit] outline-none focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:-outline-offset-2 focus-visible:outline-ring"
>
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar seed={seed} weight={weight} speed={speed} />
<ScrollBar orientation="horizontal" seed={seed} weight={weight} speed={speed} />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
);
}
const LENGTH = 400;
function ScrollBar({
className,
orientation = "vertical",
seed,
weight,
speed,
...props
}: ScrollAreaPrimitive.Scrollbar.Props & Pick<Pen, "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ weight, speed });
const s = useInkSeed(seed);
const vertical = orientation === "vertical";
const paths = useMemo(() => {
const line = (n: number) => (vertical ? verticalStroke(s + n, LENGTH) : ruleStroke(s + n, LENGTH));
return { track: line(0), thumb: [line(1), line(2)] };
}, [s, vertical]);
const box = vertical ? ([0, -1, 6, LENGTH + 2] as const) : ([-1, 0, LENGTH + 2, 6] as const);
const stretched = "[vector-effect:non-scaling-stroke]";
return (
<ScrollAreaPrimitive.Scrollbar
data-slot="scroll-area-scrollbar"
orientation={orientation}
className={inkClassName(
"flex touch-none p-px opacity-40 transition-opacity duration-(--dur-state) select-none data-hovering:opacity-100 data-scrolling:opacity-100",
"data-[orientation=vertical]:w-2.5 data-[orientation=horizontal]:h-2.5 data-[orientation=horizontal]:flex-col",
className,
)}
style={penStyle(pen) as CSSProperties}
{...props}
>
<InkSvg box={box} stretch className="inset-0 size-full text-ink-4">
<Stroke d={paths.track} width={1} opacity={0.5} className={stretched} />
</InkSvg>
<ScrollAreaPrimitive.Thumb data-slot="scroll-area-thumb" className="relative flex-1 text-ink-3 transition-colors hover:text-ink">
<InkSvg box={box} stretch className="inset-0 size-full">
<Stroke d={paths.thumb[0]} width={2.6} className={stretched} />
<Stroke d={paths.thumb[1]} width={1.2} opacity={0.6} className={cn(stretched)} />
</InkSvg>
</ScrollAreaPrimitive.Thumb>
</ScrollAreaPrimitive.Scrollbar>
);
}
export { ScrollArea, ScrollBar };Usage
import { ScrollArea } from "@/components/ui/scroll-area"
<ScrollArea className="h-72 w-56">
…long content…
</ScrollArea>Examples
Horizontal
A row of pictures that scrolls sideways.
import { ScrollArea } from "@/components/ui/scroll-area";
const works = ["Spiral", "Waves", "Corner", "Star", "Slash", "Loops", "Zigzag", "Ring", "Tick", "Cross"];
export default function ScrollAreaHorizontal() {
return (
<ScrollArea seed="scroll-h" className="w-full max-w-md whitespace-nowrap">
<div className="flex w-max gap-4 p-4">
{works.map((work, i) => (
<figure key={work} className="shrink-0">
<div className="grid h-28 w-36 place-items-center text-ink-3 outline outline-1 outline-ink-4/60" style={{ rotate: `${(i % 3) - 1}deg` }}>
{i + 1}
</div>
<figcaption className="pt-2 text-sm text-ink-3">{work}</figcaption>
</figure>
))}
</div>
</ScrollArea>
);
}Both directions
Both scrollbars on content wider and taller than the area.
import { ScrollArea } from "@/components/ui/scroll-area";
export default function ScrollAreaBoth() {
return (
<ScrollArea seed="scroll-both" className="h-56 w-full max-w-sm">
<div className="grid w-[640px] grid-cols-8 gap-2 p-3">
{Array.from({ length: 96 }, (_, i) => (
<div key={i} className="grid h-12 place-items-center text-sm text-ink-3 outline outline-1 outline-ink-4/40">
{i + 1}
</div>
))}
</div>
</ScrollArea>
);
}API reference
- className
stringSize the area (a height or width is needed for it to scroll).
- seed
string | numberPins the drawing of the scrollbars.
Every other prop goes to Base UI's Scroll Area.
Pen settings
The drawing also takes weight and speed, as props or from the nearest InkProvider. What each one does.