# Scroll Area

Scrollable container with a thin token-styled scrollbar that keyboard users can focus and scroll. No JavaScript.

- Category: Data
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/scroll-area.json
- CLI: `npx markasui@latest add scroll-area`

## When to use

Long content inside a fixed-size box, such as a list of releases, a notification panel, or a wide schedule.

- Always set a max height or max width through class, otherwise nothing scrolls
- Give every scroll area a label that describes its content
- Do not nest scroll areas that scroll in the same direction

## Code

### Blade

Save at `resources/views/components/markas/scroll-area.blade.php`. No extra dependencies.

```blade
@props([
    'orientation' => 'vertical',
    'label' => 'Scrollable content',
])

@php
    $base = 'rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent';
    $orientations = [
        'vertical' => 'overflow-x-hidden overflow-y-auto',
        'horizontal' => 'overflow-x-auto overflow-y-hidden',
        'both' => 'overflow-auto',
    ];
@endphp

<div {{ $attributes->merge(['role' => 'region', 'aria-label' => $label, 'tabindex' => '0', 'data-orientation' => $orientation])->class([$base, $orientations[$orientation] ?? $orientations['vertical']]) }}>{{ $slot }}</div>
```

```blade
<x-markas.scroll-area label="Releases" class="max-h-56 border border-line">
    <div class="p-4">...</div>
</x-markas.scroll-area>
```

### React

Save at `src/components/markas/ScrollArea.tsx`. No extra dependencies.

```tsx
import type { ComponentProps } from "react";

const base =
  "rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent";
const orientations = {
  vertical: "overflow-x-hidden overflow-y-auto",
  horizontal: "overflow-x-auto overflow-y-hidden",
  both: "overflow-auto",
};
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type ScrollAreaProps = ComponentProps<"div"> & {
  orientation?: keyof typeof orientations;
  label?: string;
};

export function ScrollArea({
  orientation = "vertical",
  label = "Scrollable content",
  className,
  ...props
}: ScrollAreaProps) {
  return (
    <div
      role="region"
      aria-label={label}
      tabIndex={0}
      data-orientation={orientation}
      className={cx(base, orientations[orientation], className)}
      {...props}
    />
  );
}
```

```tsx
import { ScrollArea } from "@/components/markas/ScrollArea";

<ScrollArea label="Releases" className="max-h-56 border border-line">
  <div className="p-4">...</div>
</ScrollArea>;
```

### Vue

Save at `src/components/markas/ScrollArea.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    orientation?: "vertical" | "horizontal" | "both";
    label?: string;
  }>(),
  { orientation: "vertical", label: "Scrollable content" },
);

const base =
  "rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent";
const orientations = {
  vertical: "overflow-x-hidden overflow-y-auto",
  horizontal: "overflow-x-auto overflow-y-hidden",
  both: "overflow-auto",
};
</script>

<template>
  <div
    role="region"
    :aria-label="props.label"
    tabindex="0"
    :data-orientation="props.orientation"
    :class="[base, orientations[props.orientation]]"
  >
    <slot />
  </div>
</template>
```

```vue
<template>
  <ScrollArea label="Releases" class="max-h-56 border border-line">
    <div class="p-4">...</div>
  </ScrollArea>
</template>
```

### Svelte

Save at `src/lib/components/markas/ScrollArea.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const base =
    "rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent";
  const orientations = {
    vertical: "overflow-x-hidden overflow-y-auto",
    horizontal: "overflow-x-auto overflow-y-hidden",
    both: "overflow-auto",
  };

  type Props = HTMLAttributes<HTMLDivElement> & {
    orientation?: keyof typeof orientations;
    /** Accessible name of the scrollable region. */
    label?: string;
    children?: Snippet;
  };

  let {
    orientation = "vertical",
    label = "Scrollable content",
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div
  role="region"
  aria-label={label}
  tabindex="0"
  data-orientation={orientation}
  class={[base, orientations[orientation], className]}
  {...rest}
>
  {@render children?.()}
</div>
```

```svelte
<script lang="ts">
  import ScrollArea from "$lib/components/markas/ScrollArea.svelte";
</script>

<ScrollArea label="Releases" class="max-h-56 border border-line">
  <div class="p-4">...</div>
</ScrollArea>
```

### Astro

Save at `src/components/markas/ScrollArea.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base =
  "rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent";
const orientations = {
  vertical: "overflow-x-hidden overflow-y-auto",
  horizontal: "overflow-x-auto overflow-y-hidden",
  both: "overflow-auto",
};

type Props = HTMLAttributes<"div"> & {
  orientation?: keyof typeof orientations;
  /** Accessible name of the scrollable region. */
  label?: string;
};

const {
  orientation = "vertical",
  label = "Scrollable content",
  class: className,
  ...rest
} = Astro.props;
---

<div
  role="region"
  aria-label={label}
  tabindex="0"
  data-orientation={orientation}
  class:list={[base, orientations[orientation], className]}
  {...rest}
>
  <slot />
</div>
```

```astro
---
import ScrollArea from "@/components/markas/ScrollArea.astro";
---

<ScrollArea label="Releases" class="max-h-56 border border-line">
  <div class="p-4">...</div>
</ScrollArea>
```

### Angular

Save at `src/app/markas/scroll-area.component.ts`. No extra dependencies.

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const base =
  "rounded-lg [scrollbar-color:var(--markas-line)_transparent] [scrollbar-width:thin] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary [&::-webkit-scrollbar]:size-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-line [&::-webkit-scrollbar-track]:bg-transparent";
const orientations = {
  vertical: "overflow-x-hidden overflow-y-auto",
  horizontal: "overflow-x-auto overflow-y-hidden",
  both: "overflow-auto",
};

export type ScrollAreaOrientation = keyof typeof orientations;

/**
 * Attribute selector on a native div, so the user's class and attributes stay on the region.
 * Usage: `<div markas-scroll-area label="Releases">...</div>`.
 */
@Component({
  selector: "div[markas-scroll-area]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    role: "region",
    tabindex: "0",
    "[attr.aria-label]": "label()",
    "[attr.data-orientation]": "orientation()",
    "[class]": "classes()",
  },
  template: `<ng-content />`,
})
export class ScrollArea {
  readonly orientation = input<ScrollAreaOrientation>("vertical");
  /** Accessible name of the scrollable region. */
  readonly label = input("Scrollable content");

  protected readonly classes = computed(
    () => `${base} ${orientations[this.orientation()]}`,
  );
}
```

```ts
import { Component } from "@angular/core";
import { ScrollArea } from "./markas/scroll-area.component";

@Component({
  selector: "app-example",
  imports: [ScrollArea],
  template: `
    <div
      markas-scroll-area
      label="Releases"
      class="max-h-56 border border-line"
    >
      <div class="p-4">...</div>
    </div>
  `,
})
export class Example {}
```

Angular uses an attribute selector on a native `div`, so the size classes stay on the region.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `"vertical" \| "horizontal" \| "both"` | "vertical" | Direction the content can scroll. The other direction is clipped unless you pick both. |
| `label` | `string` | Scrollable content | Accessible name of the region. Describe the content, for example Releases. |
| `class` | `string` | - | Set the size limit here, for example max-h-64 or max-w-sm, plus any border or background. |

## Accessibility

- Uses role=region with aria-label so screen reader users can find and identify the scrollable area.
- Has tabindex=0, so keyboard users can focus it and scroll with the arrow keys, Page Up, and Page Down.
- Shows a visible focus outline and keeps the native scrollbar, only thinner and in token colors.

## Notes

- Set the size limit with a class such as `max-h-56` or `max-w-sm`. Without it nothing scrolls.
- Use `orientation="horizontal"` for a row of cards and `orientation="both"` for wide grids.
- The scrollbar is native, only thinner and in token colors, so no JavaScript is needed.
