# Scroll Area

> Named, keyboard-focusable scroll region with thin themed scrollbars, optional edge fade masks, and scroll containment.

- Kind: ui · Category: Layout and media · Interactivity: script
- Install: `npx shadcn@latest add @haze/scroll-area`
- File: `src/components/haze/ui/scroll-area.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/scroll-area

## Use when
- One panel contains a long list or log with a bounded height.
- Horizontal tag strips or vertical sidebars need subtle scroll edge cues.

## Avoid when
- The content can use normal page scrolling without obscuring nearby controls.

## Alternatives
- [card](https://hazeui.com/ui/card.md)
- [scroll-progress](https://hazeui.com/ui/scroll-progress.md)
- [carousel](https://hazeui.com/ui/carousel.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` | Scrollable content | no | Accessible name for the scroll region. |
| direction | `"vertical" \| "horizontal" \| "both"` | vertical | no | Axes on which the content can overflow. |
| fade | `boolean` | false | no | Fades overflowed edges when scrolled away from a boundary. |
| scrollbarGutter | `boolean` | true | no | Reserves space for vertical scrollbars to prevent layout shift. |
| class | `string` |  | no | Classes for the region, including an optional height limit. |

## Accessibility
- The region is named and its scrollbar is native.
- Keyboard scrolling works while focus is inside the region.
- Edge fade masks update progressively via script and never trap clicks or content.
- Key Tab: Focus the scroll region.
- Key Arrow keys / Page Up / Page Down: Scroll the focused region.

## Agent prompts

### Install and use

Install the Haze UI component "Scroll Area" with `npx shadcn@latest add @haze/scroll-area`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import ScrollArea from "@/components/haze/ui/scroll-area.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/scroll-area.md

### Customize with real content

Adapt the Haze scroll-area ui for our product. Replace the demo values of these props with our real content: label, direction, fade, scrollbarGutter, class. Keep the layout, the heading level rules and these accessibility details: The region is named and its scrollbar is native. Keyboard scrolling works while focus is inside the region. Edge fade masks update progressively via script and never trap clicks or content. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using scroll-area, check it fits: use it when one panel contains a long list or log with a bounded height.; horizontal tag strips or vertical sidebars need subtle scroll edge cues.. Avoid it when the content can use normal page scrolling without obscuring nearby controls.. Alternatives: @haze/card, @haze/scroll-progress, @haze/carousel.

## Example requests that should lead here
- Put the release log in a scroll area with faded edges.
- Add a horizontal fading scroll strip of section pills.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  label?: string;
  direction?: "vertical" | "horizontal" | "both";
  /** Adds dynamic edge fade masks at scrolled boundaries. */
  fade?: boolean;
  /** Reserves stable scrollbar gutter space to avoid layout shifts. */
  scrollbarGutter?: boolean;
  class?: string;
}

const { label = "Scrollable content", direction = "vertical", fade = false, scrollbarGutter = true, class: className } = Astro.props;
const directions = {
  vertical: "max-h-44 overflow-y-auto overflow-x-hidden",
  horizontal: "overflow-x-auto overflow-y-hidden",
  both: "max-h-44 overflow-auto",
};
const fadeMasks = {
  vertical: "[mask-image:linear-gradient(to_bottom,transparent_0,currentColor_var(--fade-top,0px),currentColor_calc(100%-var(--fade-bottom,0px)),transparent_100%)]",
  horizontal: "[mask-image:linear-gradient(to_right,transparent_0,currentColor_var(--fade-left,0px),currentColor_calc(100%-var(--fade-right,0px)),transparent_100%)]",
  both: "[mask-image:linear-gradient(to_bottom,transparent_0,currentColor_var(--fade-top,0px),currentColor_calc(100%-var(--fade-bottom,0px)),transparent_100%)]",
};
---

<div
  role="region"
  aria-label={label}
  tabindex="0"
  data-haze-scroll-area
  data-fade={fade ? "" : undefined}
  data-direction={direction}
  class={cn(
    "min-w-0 rounded-xl border border-border bg-card text-card-foreground [scrollbar-color:var(--color-border-strong)_transparent] [scrollbar-width:thin]",
    "overscroll-contain focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
    scrollbarGutter && direction !== "horizontal" && "[scrollbar-gutter:stable]",
    directions[direction],
    fade && fadeMasks[direction],
    className,
  )}
>
  <slot><p class="px-3 py-2 text-[0.8125rem]/5">Scrollable content appears here.</p></slot>
</div>

<script>
  function enhanceScrollAreas() {
    document.querySelectorAll<HTMLElement>("[data-haze-scroll-area][data-fade]:not([data-enhanced])").forEach((el) => {
      const update = () => {
        const top = el.scrollTop > 2 ? 20 : 0;
        const bottom = el.scrollTop + el.clientHeight < el.scrollHeight - 2 ? 20 : 0;
        const left = el.scrollLeft > 2 ? 20 : 0;
        const right = el.scrollLeft + el.clientWidth < el.scrollWidth - 2 ? 20 : 0;
        el.style.setProperty("--fade-top", `${top}px`);
        el.style.setProperty("--fade-bottom", `${bottom}px`);
        el.style.setProperty("--fade-left", `${left}px`);
        el.style.setProperty("--fade-right", `${right}px`);
      };
      el.addEventListener("scroll", update, { passive: true });
      if ("ResizeObserver" in window) new ResizeObserver(update).observe(el);
      update();
      el.setAttribute("data-enhanced", "");
    });
  }
  enhanceScrollAreas();
  if (!document.documentElement.hasAttribute("data-haze-scroll-area-listener")) {
    document.documentElement.setAttribute("data-haze-scroll-area-listener", "");
    document.addEventListener("astro:page-load", enhanceScrollAreas);
  }
</script>
```
