Scroll Area
The shadcn/ui Scroll Area with slim custom scrollbars that fade in on hover and out when idle — plus two React scroll blocks: edge fade masks that appear only when there's more content, and a Motion-powered scroll progress bar with a back-to-top button.
- Drop-in shadcn API
- Native scrolling
- Fading scrollbars
- Edge fade masks
- Motion block
import { FadeScrollArea } from "@/components/dusk/scroll-area/fade-scroll-area";
const notifications = [
"Ada Kim commented on Dock spring tuning",
"Deploy #482 finished in 41s",
"Sam Patel requested your review",
"New sign-in from Berlin",
"Lee Wong merged motion-presets",
"Weekly usage report is ready",
"Maya Ruiz mentioned you in Design QA",
"Invoice for November is available",
"Deploy #481 rolled back",
"3 new issues labelled a11y",
"Your API key expires in 7 days",
"Ada Kim starred dusk-web",
];
const tags = ["React", "Motion", "Radix", "Tailwind", "shadcn/ui", "a11y", "Springs", "Layout", "Gestures", "SSR"];
export default function FadeScrollAreaDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-4">
<FadeScrollArea className="h-64 rounded-xl border bg-card/40">
<ul className="divide-y px-4">
{notifications.map((text) => (
<li key={text} className="py-3 text-sm text-muted-foreground">
{text}
</li>
))}
</ul>
</FadeScrollArea>
<FadeScrollArea orientation="horizontal" className="rounded-xl border bg-card/40">
<div className="flex w-max gap-2 p-3">
{tags.map((tag) => (
<span key={tag} className="rounded-full border px-3 py-1 text-xs text-muted-foreground">
{tag}
</span>
))}
</div>
</FadeScrollArea>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/scroll-area.json1. Install the dependencies.
npm install @radix-ui/react-scroll-area2. Copy the source into
components/ui/scroll-area.tsx.scroll-area.tsx "use client" import * as React from "react" import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area" import { cn } from "@/lib/utils" /** * DuskUI Scroll Area — the shadcn/ui Scroll Area with DuskUI polish. * * Same parts, props and data-slot attributes as shadcn, plus: * - Scrollbars fade in when you hover (or scroll, with `type="scroll"`) and * fade out after `scrollHideDelay`, instead of popping. * - A slim thumb that brightens on hover and while dragged. * - `viewportRef` and `viewportClassName` to reach the scrolling element * (for scroll listeners, `useScroll` or masks). * - A visible focus ring on the viewport for keyboard scrolling. * - Under `prefers-reduced-motion` scrollbars appear without fading. * CSS only. Radix keeps native scrolling (wheel, touch, keyboard) and * custom-styled scrollbars in sync. */ function ScrollArea({ className, children, viewportRef, viewportClassName, ...props }: React.ComponentProps<typeof ScrollAreaPrimitive.Root> & { /** Ref to the scrolling element. */ viewportRef?: React.Ref<HTMLDivElement> /** Classes for the scrolling element. */ viewportClassName?: string }) { return ( <ScrollAreaPrimitive.Root data-slot="scroll-area" className={cn("relative", className)} {...props} > <ScrollAreaPrimitive.Viewport ref={viewportRef} data-slot="scroll-area-viewport" className={cn( "size-full rounded-[inherit] outline-none transition-[color,box-shadow] focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1", viewportClassName )} > {children} </ScrollAreaPrimitive.Viewport> <ScrollBar /> <ScrollAreaPrimitive.Corner /> </ScrollAreaPrimitive.Root> ) } /** A scrollbar. One vertical bar is included; add `<ScrollBar orientation="horizontal" />` for sideways scrolling. */ function ScrollBar({ className, orientation = "vertical", ...props }: React.ComponentProps<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>) { return ( <ScrollAreaPrimitive.ScrollAreaScrollbar data-slot="scroll-area-scrollbar" orientation={orientation} className={cn( "group/scrollbar flex touch-none p-px select-none", // Radix unmounts hidden bars after the exit animation, so they fade both ways. "data-[state=visible]:animate-in data-[state=visible]:fade-in-0 data-[state=visible]:duration-200", "data-[state=hidden]:animate-out data-[state=hidden]:fade-out-0 data-[state=hidden]:duration-200", "motion-reduce:animate-none", orientation === "vertical" && "h-full w-2.5 border-l border-l-transparent", orientation === "horizontal" && "h-2.5 flex-col border-t border-t-transparent", className )} {...props} > <ScrollAreaPrimitive.ScrollAreaThumb data-slot="scroll-area-thumb" className={cn( "relative flex-1 rounded-full bg-white/15 transition-colors duration-150 motion-reduce:transition-none", "group-hover/scrollbar:bg-white/30 active:bg-white/40", // A larger hit area than the visible thumb. "before:absolute before:top-1/2 before:left-1/2 before:size-full before:min-h-11 before:min-w-11 before:-translate-x-1/2 before:-translate-y-1/2" )} /> </ScrollAreaPrimitive.ScrollAreaScrollbar> ) } export { ScrollArea, ScrollBar }
Same exports and props as the shadcn/ui Scroll Area (ScrollArea and ScrollBar), plus optional viewportRef and viewportClassName to reach the scrolling element. CSS only, no Motion.
Usage
import { ScrollArea } from "@/components/ui/scroll-area"
export function ReleaseList({ releases }: { releases: string[] }) {
return (
<ScrollArea className="h-72 w-48 rounded-md border">
<div className="p-4">
{releases.map((release) => (
<div key={release} className="py-2 text-sm">{release}</div>
))}
</div>
</ScrollArea>
)
}Examples
Default
Hover the list: the scrollbar fades in, brightens when you hover it and while you drag the thumb, and fades out shortly after the pointer leaves. Wheel, touch and keyboard scrolling stay native. Pass type="scroll" to show it only while scrolling, or type="always".
import { ScrollArea } from "@/components/ui/scroll-area";
const releases = Array.from({ length: 40 }, (_, i) => `v2.${40 - i}.0`);
export default function ScrollAreaBasicDemo() {
return (
<ScrollArea className="h-72 w-52 rounded-xl border bg-card/40">
<div className="p-4">
<h4 className="mb-3 text-sm font-medium">Releases</h4>
{releases.map((release) => (
<div key={release} className="border-b py-2 text-sm text-muted-foreground last:border-0">
{release}
</div>
))}
</div>
</ScrollArea>
);
}Horizontal
Add <ScrollBar orientation="horizontal" /> and give the content w-max to scroll sideways.
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
const works = [
{ title: "Northern Ridge", hue: 160 },
{ title: "Glass Harbor", hue: 190 },
{ title: "Moss Line", hue: 135 },
{ title: "Low Tide", hue: 210 },
{ title: "Ember Field", hue: 40 },
{ title: "Night Garden", hue: 280 },
];
export default function ScrollAreaHorizontalDemo() {
return (
<ScrollArea className="w-full max-w-md rounded-xl border bg-card whitespace-nowrap">
<div className="flex w-max gap-3 p-4">
{works.map((work) => (
<figure key={work.title} className="shrink-0">
<div
className="h-36 w-28 rounded-lg border"
style={{ background: `oklch(0.24 0.03 ${work.hue})` }}
/>
<figcaption className="pt-2 text-xs text-muted-foreground">{work.title}</figcaption>
</figure>
))}
</div>
<ScrollBar orientation="horizontal" />
</ScrollArea>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Fade Scroll Area
Edges fade out only when there's more to see in that direction: scroll down and the top begins to fade, reach the end and the bottom fade disappears; if everything fits there's no fade at all. The fades are a CSS mask, so they work over any background, and they grow with the scroll distance instead of popping. Works vertically or horizontally, updates in a requestAnimationFrame and never re-renders React.
import { FadeScrollArea } from "@/components/dusk/scroll-area/fade-scroll-area";
const notifications = [
"Ada Kim commented on Dock spring tuning",
"Deploy #482 finished in 41s",
"Sam Patel requested your review",
"New sign-in from Berlin",
"Lee Wong merged motion-presets",
"Weekly usage report is ready",
"Maya Ruiz mentioned you in Design QA",
"Invoice for November is available",
"Deploy #481 rolled back",
"3 new issues labelled a11y",
"Your API key expires in 7 days",
"Ada Kim starred dusk-web",
];
const tags = ["React", "Motion", "Radix", "Tailwind", "shadcn/ui", "a11y", "Springs", "Layout", "Gestures", "SSR"];
export default function FadeScrollAreaDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-4">
<FadeScrollArea className="h-64 rounded-xl border bg-card/40">
<ul className="divide-y px-4">
{notifications.map((text) => (
<li key={text} className="py-3 text-sm text-muted-foreground">
{text}
</li>
))}
</ul>
</FadeScrollArea>
<FadeScrollArea orientation="horizontal" className="rounded-xl border bg-card/40">
<div className="flex w-max gap-2 p-3">
{tags.map((tag) => (
<span key={tag} className="rounded-full border px-3 py-1 text-xs text-muted-foreground">
{tag}
</span>
))}
</div>
</FadeScrollArea>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/fade-scroll-area.json1. Install the dependencies.
npm install @radix-ui/react-scroll-area2. Copy the source into
components/dusk/scroll-area/fade-scroll-area.tsx.fade-scroll-area.tsx "use client"; import * as React from "react"; import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; import { cn } from "@/lib/utils"; export interface FadeScrollAreaProps extends React.ComponentProps<typeof ScrollArea> { /** Scroll direction. Horizontal areas get a horizontal scrollbar and side fades. */ orientation?: "vertical" | "horizontal"; /** Length of each edge fade in px. */ fadeSize?: number; } function assignRef<T>(ref: React.Ref<T> | undefined, value: T | null) { if (typeof ref === "function") ref(value); else if (ref) (ref as React.RefObject<T | null>).current = value; } /** * A Scroll Area whose edges fade out only when there's more to see. Scroll * down and the top edge starts to fade; reach the end and the bottom fade * disappears. If the content fits, there's no fade at all. The fades are a * CSS mask, so they work over any background, and they grow with the scroll * distance instead of popping. Updates run in a requestAnimationFrame and * never re-render React. */ export function FadeScrollArea({ orientation = "vertical", fadeSize = 40, className, viewportClassName, viewportRef, children, ...props }: FadeScrollAreaProps) { const localRef = React.useRef<HTMLDivElement | null>(null); const vertical = orientation === "vertical"; const setViewport = React.useCallback( (node: HTMLDivElement | null) => { localRef.current = node; assignRef(viewportRef, node); }, [viewportRef] ); React.useEffect(() => { const el = localRef.current; if (!el) return; let frame = 0; const update = () => { frame = 0; const position = vertical ? el.scrollTop : Math.abs(el.scrollLeft); const max = vertical ? el.scrollHeight - el.clientHeight : el.scrollWidth - el.clientWidth; const start = Math.min(fadeSize, Math.max(0, position)); const end = Math.min(fadeSize, Math.max(0, max - position)); el.style.setProperty("--fade-start", `${start}px`); el.style.setProperty("--fade-end", `${end}px`); }; const schedule = () => { if (!frame) frame = requestAnimationFrame(update); }; update(); el.addEventListener("scroll", schedule, { passive: true }); // Re-measure when the area or its content changes size. const observer = new ResizeObserver(schedule); observer.observe(el); if (el.firstElementChild) observer.observe(el.firstElementChild); return () => { el.removeEventListener("scroll", schedule); observer.disconnect(); cancelAnimationFrame(frame); }; }, [vertical, fadeSize]); return ( <ScrollArea data-slot="fade-scroll-area" className={className} viewportRef={setViewport} viewportClassName={cn( vertical ? "[mask-image:linear-gradient(to_bottom,transparent,#000_var(--fade-start),#000_calc(100%_-_var(--fade-end)),transparent)]" : "[mask-image:linear-gradient(to_right,transparent,#000_var(--fade-start),#000_calc(100%_-_var(--fade-end)),transparent)]", viewportClassName )} {...props} > {children} {!vertical && <ScrollBar orientation="horizontal" />} </ScrollArea> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| orientation | "vertical" | "horizontal" | "vertical" | Scroll direction. Horizontal adds a horizontal scrollbar and side fades. |
| fadeSize | number | 40 | Length of each edge fade in px. |
| …ScrollArea props | ScrollArea props | — | className, type, scrollHideDelay, viewportRef, viewportClassName… |
Scroll Progress
A reading-progress bar along the top fills as you scroll, spring-smoothed with Motion's useScroll + useSpring on the viewport. Past backToTop, a button rises in whose ring traces the same progress and scrolls back to the top. Scrolling never re-renders React.
import { ScrollProgress } from "@/components/dusk/scroll-area/scroll-progress";
const sections = [
["Springs, not curves", "Springs carry velocity from one gesture into the next, so interrupted motion never jumps. Give them a duration and a bounce instead of stiffness and damping when you want predictable timing."],
["Animate the cheap things", "Transforms and opacity run on the compositor. Width, height and top cause layout on every frame — reach for layout animations or FLIP instead."],
["Exit faster than you enter", "Entrances can take their time to explain where something came from. Exits should get out of the way: roughly half the duration feels right."],
["Respect reduced motion", "Swap slides and scales for fades when the user asks for less motion. The interface should keep its meaning, not its choreography."],
["Direction is information", "Content that slides in from the side you're moving toward tells people where they are without a single word of copy."],
] as const;
export default function ScrollProgressDemo() {
return (
<ScrollProgress className="h-80 w-full max-w-md rounded-xl border bg-card/40">
<article className="space-y-6 p-6 pt-7">
<h3 className="text-lg font-semibold tracking-tight">Five rules for interface motion</h3>
{sections.map(([title, body], i) => (
<section key={title} className="space-y-2">
<h4 className="text-sm font-medium text-foreground">
{i + 1}. {title}
</h4>
<p className="text-sm leading-relaxed text-muted-foreground">{body}</p>
</section>
))}
</article>
</ScrollProgress>
);
}$ npx shadcn@latest add https://duskui.com/r/scroll-progress.json1. Install the dependencies.
npm install motion @radix-ui/react-scroll-area lucide-react2. Copy the source into
components/dusk/scroll-area/scroll-progress.tsx.scroll-progress.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useMotionValueEvent, useReducedMotion, useScroll, useSpring, } from "motion/react"; import { ArrowUp } from "lucide-react"; import { ScrollArea } from "@/components/ui/scroll-area"; import { cn } from "@/lib/utils"; export interface ScrollProgressProps extends React.ComponentProps<typeof ScrollArea> { /** Show a back-to-top button, ringed with progress, after this much of the content (0–1). `false` hides it. */ backToTop?: number | false; /** Classes for the progress bar. */ barClassName?: string; } function assignRef<T>(ref: React.Ref<T> | undefined, value: T | null) { if (typeof ref === "function") ref(value); else if (ref) (ref as React.RefObject<T | null>).current = value; } /** * A Scroll Area with a reading-progress bar. A thin bar along the top fills * as you scroll (spring-smoothed so it never stutters), and once you're past * `backToTop` a button rises in whose ring traces the same progress; it * scrolls back to the top. Driven by Motion's `useScroll` on the viewport, so * scrolling never re-renders React. Under reduced motion the bar tracks the * scroll exactly and the jump to the top is instant. */ export function ScrollProgress({ backToTop = 0.2, barClassName, className, viewportRef, children, ...props }: ScrollProgressProps) { const reduceMotion = useReducedMotion() ?? false; const localRef = React.useRef<HTMLDivElement | null>(null); const { scrollYProgress } = useScroll({ container: localRef }); const smooth = useSpring(scrollYProgress, { stiffness: 260, damping: 36, restDelta: 0.001 }); const progress = reduceMotion ? scrollYProgress : smooth; const [showTop, setShowTop] = React.useState(false); useMotionValueEvent(scrollYProgress, "change", (value) => { if (backToTop !== false) setShowTop(value > backToTop); }); const setViewport = React.useCallback( (node: HTMLDivElement | null) => { localRef.current = node; assignRef(viewportRef, node); }, [viewportRef] ); return ( <div data-slot="scroll-progress" className={cn("relative overflow-hidden", className)}> <ScrollArea className="size-full" viewportRef={setViewport} {...props}> {children} </ScrollArea> <div aria-hidden className="pointer-events-none absolute inset-x-0 top-0 h-0.5 bg-white/5"> <motion.div className={cn("h-full origin-left bg-primary", barClassName)} style={{ scaleX: progress }} /> </div> <AnimatePresence> {backToTop !== false && showTop && ( <motion.button key="top" type="button" aria-label="Back to top" onClick={() => localRef.current?.scrollTo({ top: 0, behavior: reduceMotion ? "auto" : "smooth" })} className="absolute right-4 bottom-4 inline-flex size-10 items-center justify-center rounded-full border bg-background text-foreground shadow-md outline-none transition-colors hover:bg-accent focus-visible:ring-[3px] focus-visible:ring-ring/50" initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8, scale: 0.96 }} animate={reduceMotion ? { opacity: 1 } : { opacity: 1, y: 0, scale: 1 }} exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8, scale: 0.96 }} transition={{ type: "spring", duration: 0.25, bounce: 0.1 }} > <svg aria-hidden viewBox="0 0 40 40" className="absolute inset-0 size-full -rotate-90"> <circle cx="20" cy="20" r="18" fill="none" stroke="currentColor" strokeWidth="2" className="text-white/10" /> <motion.circle cx="20" cy="20" r="18" fill="none" stroke="var(--color-primary)" strokeWidth="2" strokeLinecap="round" style={{ pathLength: progress }} /> </svg> <ArrowUp className="size-4" aria-hidden /> </motion.button> )} </AnimatePresence> </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| backToTop | number | false | 0.2 | Show the back-to-top button after this fraction of the content. false hides it. |
| barClassName | string | — | Classes for the progress bar, e.g. a different color. |
| …ScrollArea props | ScrollArea props | — | className sizes the wrapper; the rest go to the ScrollArea. |
API Reference
ScrollArea wraps Radix's Root, Viewport, a vertical ScrollBar and Corner. Add more ScrollBars as children.
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "auto" | "always" | "scroll" | "hover" | "hover" | When scrollbars are shown (on ScrollArea). |
| scrollHideDelay | number | 600 | ms before scrollbars fade out (on ScrollArea). |
| viewportRef | Ref<HTMLDivElement> | — | Ref to the scrolling element (on ScrollArea). |
| viewportClassName | string | — | Classes for the scrolling element (on ScrollArea). |
| orientation | "vertical" | "horizontal" | "vertical" | On ScrollBar. |
Accessibility
- Scrolling stays native: wheel, trackpad, touch, keyboard (arrows, Page Up/Down, Space) and screen-reader navigation all work as usual.
- The viewport shows a focus ring when it's focused for keyboard scrolling.
- Custom scrollbars are decorative mirrors of the native scroll position; the thumb has a 44px hit area for pointer users.
- Scroll Progress's back-to-top button is a labelled button, and the progress bar is hidden from assistive tech.
- Under prefers-reduced-motion scrollbars appear without fading, the progress bar tracks exactly and back-to-top jumps instantly.