# Scroll Area
A fixed-size scrolling box with thin styled scrollbars.
Source page: https://dinachi.dev/docs/components/scroll-area

## Installation
```bash
npx @dinachi/cli@latest add scroll-area
```
Adds to your project:
- `components/ui/scroll-area/scroll-area.tsx`
- `components/ui/scroll-area/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import { ScrollArea } from "@/components/ui/scroll-area"

export function ReleaseNotes() {
  return (
    <ScrollArea className="h-64 w-72 rounded-md border">
      <div className="p-4">{/* long content */}</div>
    </ScrollArea>
  )
}
```

## Anatomy
```tsx
<ScrollAreaRoot>
  <ScrollAreaViewport>
    <ScrollAreaContent />
  </ScrollAreaViewport>
  <ScrollAreaScrollbar>
    <ScrollAreaThumb />
  </ScrollAreaScrollbar>
  <ScrollAreaCorner />
</ScrollAreaRoot>
```
- `ScrollArea`: Root, viewport, content, scrollbars and corner in one.
- `ScrollAreaRoot`: The bare root, for custom composition.
- `ScrollAreaViewport`: The element that scrolls.
- `ScrollAreaContent`: Wraps the content for measuring.
- `ScrollAreaScrollbar`: One scrollbar track.
- `ScrollAreaThumb`: The draggable handle inside a scrollbar.
- `ScrollAreaCorner`: Fills the gap where two scrollbars meet.

## Examples

### Vertical
A long list in a fixed height.
```tsx
import { ScrollArea } from '@/components/ui/scroll-area';
import { Separator } from '@/components/ui/separator';

const tags = Array.from({ length: 50 }).map(
  (_, i, a) => `v1.2.0-beta.${a.length - i}`
);

export function Example() {
  return (
    <ScrollArea className="h-72 w-48 rounded-md border">
      <div className="p-4">
        <h4 className="mb-4 text-sm font-medium leading-none">Tags</h4>
        {tags.map((tag) => (
          <div key={tag}>
            <div className="text-sm">{tag}</div>
            <Separator className="my-2" />
          </div>
        ))}
      </div>
    </ScrollArea>
  );
}
```

### Horizontal
A row of tiles that scrolls sideways.
```tsx
import { ScrollArea } from '@/components/ui/scroll-area';

export function Example() {
  const items = Array.from({ length: 20 }).map((_, i) => ({
    title: `Item ${i + 1}`,
    color: `hsl(${i * 18}, 70%, 80%)`,
  }));

  return (
    <ScrollArea className="w-full max-w-md rounded-md border" orientation="horizontal">
      <div className="flex gap-3 p-4">
        {items.map((item) => (
          <div
            key={item.title}
            className="flex h-20 w-20 shrink-0 items-center justify-center rounded-md text-xs font-medium"
            style={{ backgroundColor: item.color }}
          >
            {item.title}
          </div>
        ))}
      </div>
    </ScrollArea>
  );
}
```

## Notes

### Compound or parts
`ScrollArea` builds every part; wrap content in it. Use `ScrollAreaRoot` and the parts only for a custom layout, such as relocated scrollbars.

### Scrollbars on hover
Scrollbars show whenever there is overflow. To show them only on hover or while scrolling, style on `data-hovering` and `data-scrolling`.

### Edge fades
The root sets `data-overflow-y-start`, `data-overflow-y-end` and the `x` versions while content continues past that edge. Use them to fade only cut-off edges.

## API reference

### ScrollArea
Root, viewport, content, scrollbars and corner in one.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `"vertical" \| "horizontal" \| "both"` | `"vertical"` | Which scrollbars to render. `both` adds the corner. |
| `viewportClassName` | `string` | - | Classes for the inner viewport. |
| `scrollbarClassName` | `string` | - | Classes for every scrollbar. |
| `overflowEdgeThreshold` | `number \| { xStart, xEnd, yStart, yEnd }` | `0` | Pixels to scroll before the overflow edge attributes apply. |
| `className` | `string` | - | Size the area here, such as `h-64`. It does not grow to fit. |

### ScrollAreaRoot
The bare root, for custom composition.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `overflowEdgeThreshold` | `number \| { xStart, xEnd, yStart, yEnd }` | `0` | Pixels to scroll before the overflow edge attributes apply. |

### ScrollAreaViewport
The element that scrolls.

Takes the props of the element it renders.

### ScrollAreaContent
Wraps the content for measuring.

Takes the props of the element it renders.

### ScrollAreaScrollbar
One scrollbar track.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | The axis it scrolls. |
| `keepMounted` | `boolean` | `false` | Keeps the scrollbar mounted when there is nothing to scroll. |

### ScrollAreaThumb
The draggable handle inside a scrollbar.

Takes the props of the element it renders.

### ScrollAreaCorner
Fills the gap where two scrollbars meet.

Takes the props of the element it renders.

## Accessibility
- The viewport scrolls natively: wheel, touch and keyboard all work.
- The viewport is focusable while it has overflow.
- The native scrollbar is replaced by the styled one; its thumb is draggable.
### Keyboard
| Keys | Does |
| --- | --- |
| `Tab` | Focuses the viewport when it can scroll |
| `↑` / `↓` / `←` / `→` | Scrolls the focused viewport |
| `Page Up` / `Page Down` | Scrolls by a page |
| `Home` / `End` | Scrolls to the start or end |

## Source
`components/ui/scroll-area/scroll-area.tsx`
```tsx
"use client"

import * as React from "react"
import { ScrollArea as ScrollAreaPrimitive } from "@base-ui/react/scroll-area"
import { cn } from "@/lib/utils"

/* -------------------------------------------------------------------------------------------------
 * Compound ScrollArea — handles Root + Viewport + Content + Scrollbar + Thumb internally
 * ------------------------------------------------------------------------------------------------*/

const ScrollArea = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> & {
    orientation?: "vertical" | "horizontal" | "both"
    viewportClassName?: string
    scrollbarClassName?: string
  }
>(
  (
    {
      className,
      orientation = "vertical",
      viewportClassName,
      scrollbarClassName,
      children,
      ...props
    },
    ref
  ) => (
    <ScrollAreaPrimitive.Root
      data-slot="scroll-area"
      ref={ref}
      className={cn("relative overflow-hidden", className)}
      {...props}
    >
      <ScrollAreaPrimitive.Viewport
        data-slot="scroll-area-viewport"
        className={cn("size-full rounded-[inherit]", viewportClassName)}
      >
        <ScrollAreaPrimitive.Content data-slot="scroll-area-content" className="min-w-full">
          {children}
        </ScrollAreaPrimitive.Content>
      </ScrollAreaPrimitive.Viewport>

      {(orientation === "vertical" || orientation === "both") && (
        <ScrollAreaPrimitive.Scrollbar
          data-slot="scroll-area-scrollbar"
          orientation="vertical"
          className={cn(
            "flex touch-none select-none p-0.5 transition-colors",
            "h-full w-2.5 border-l border-l-transparent",
            scrollbarClassName
          )}
        >
          <ScrollAreaPrimitive.Thumb data-slot="scroll-area-thumb" className="relative flex-1 rounded-full bg-border" />
        </ScrollAreaPrimitive.Scrollbar>
      )}

      {(orientation === "horizontal" || orientation === "both") && (
        <ScrollAreaPrimitive.Scrollbar
          data-slot="scroll-area-scrollbar"
          orientation="horizontal"
          className={cn(
            "flex touch-none select-none p-0.5 transition-colors",
            "h-2.5 w-full flex-col border-t border-t-transparent",
            scrollbarClassName
          )}
        >
          <ScrollAreaPrimitive.Thumb data-slot="scroll-area-thumb" className="relative flex-1 rounded-full bg-border" />
        </ScrollAreaPrimitive.Scrollbar>
      )}

      {orientation === "both" && <ScrollAreaPrimitive.Corner data-slot="scroll-area-corner" className="bg-background" />}
    </ScrollAreaPrimitive.Root>
  )
)
ScrollArea.displayName = "ScrollArea"

/* -------------------------------------------------------------------------------------------------
 * Primitives — re-exported for advanced composition (e.g. dual-axis with custom placement)
 * ------------------------------------------------------------------------------------------------*/

const ScrollAreaRoot = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, ...props }, ref) => (
  <ScrollAreaPrimitive.Root
    data-slot="scroll-area-root"
    ref={ref}
    className={cn("relative overflow-hidden", className)}
    {...props}
  />
))
ScrollAreaRoot.displayName = "ScrollAreaRoot"

const ScrollAreaViewport = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Viewport>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Viewport>
>(({ className, ...props }, ref) => (
  <ScrollAreaPrimitive.Viewport
    data-slot="scroll-area-viewport"
    ref={ref}
    className={cn("size-full rounded-[inherit]", className)}
    {...props}
  />
))
ScrollAreaViewport.displayName = "ScrollAreaViewport"

const ScrollAreaContent = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Content>
>(({ className, ...props }, ref) => (
  <ScrollAreaPrimitive.Content
    data-slot="scroll-area-content"
    ref={ref}
    className={cn("min-w-full", className)}
    {...props}
  />
))
ScrollAreaContent.displayName = "ScrollAreaContent"

const ScrollAreaScrollbar = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Scrollbar>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Scrollbar>
>(({ className, orientation = "vertical", ...props }, ref) => (
  <ScrollAreaPrimitive.Scrollbar
    data-slot="scroll-area-scrollbar"
    ref={ref}
    orientation={orientation}
    className={cn(
      "flex touch-none select-none p-0.5 transition-colors",
      orientation === "vertical"
        ? "h-full w-2.5 border-l border-l-transparent"
        : "h-2.5 w-full flex-col border-t border-t-transparent",
      className
    )}
    {...props}
  />
))
ScrollAreaScrollbar.displayName = "ScrollAreaScrollbar"

const ScrollAreaThumb = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Thumb>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Thumb>
>(({ className, ...props }, ref) => (
  <ScrollAreaPrimitive.Thumb
    data-slot="scroll-area-thumb"
    ref={ref}
    className={cn("relative flex-1 rounded-full bg-border", className)}
    {...props}
  />
))
ScrollAreaThumb.displayName = "ScrollAreaThumb"

const ScrollAreaCorner = React.forwardRef<
  React.ComponentRef<typeof ScrollAreaPrimitive.Corner>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Corner>
>(({ className, ...props }, ref) => (
  <ScrollAreaPrimitive.Corner
    data-slot="scroll-area-corner"
    ref={ref}
    className={cn("bg-background", className)}
    {...props}
  />
))
ScrollAreaCorner.displayName = "ScrollAreaCorner"

export {
  ScrollArea,
  ScrollAreaRoot,
  ScrollAreaViewport,
  ScrollAreaContent,
  ScrollAreaScrollbar,
  ScrollAreaThumb,
  ScrollAreaCorner,
}
```
