# Scroll top

A floating button that appears once the page has scrolled down and takes the reader back to the top. Use it on long pages and long scrolling panels.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-scroll-top`
- Source: [libs/ui/src/lib/molecules/scroll-top/scroll-top.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/scroll-top/scroll-top.ts)

```typescript title="Import"
import { ScrollTop } from '@vigdx/ui/scroll-top';
```

## Page

Place one `vig-scroll-top` anywhere in the app; it watches the page and shows past `threshold` pixels (default 400), fixed at the bottom inline-end corner. `variant` colors it.

```typescript title="scroll-top-window.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ScrollTop } from '@vigdx/ui/scroll-top';

@Component({
  selector: 'app-scroll-top-window',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ScrollTop],
  template: `
    <p class="text-sm text-muted-foreground">
      This page has one: scroll down past 400 pixels and it appears in the bottom corner.
    </p>
    <vig-scroll-top variant="primary" [labels]="{ ariaLabel: 'Back to top of the page' }" />
  `,
})
export class ScrollTopWindowDemo {}
```

## Scroll container

`target="parent"` watches the element the button is a direct child of; the button sticks to its bottom.

```typescript title="scroll-top-container.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ScrollTop } from '@vigdx/ui/scroll-top';

@Component({
  selector: 'app-scroll-top-container',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ScrollTop],
  template: `
    <div
      tabindex="0"
      role="region"
      aria-label="Release notes"
      class="h-64 w-full max-w-xl overflow-auto rounded-card border border-border p-4 text-sm"
    >
      @for (n of paragraphs; track n) {
        <p class="mb-4">
          Release note {{ n }}. Scroll down: the button appears once this box scrolled past 120
          pixels, and takes you and the focus back to the top.
        </p>
      }
      <vig-scroll-top target="parent" [threshold]="120" />
    </div>
  `,
})
export class ScrollTopContainerDemo {
  protected readonly paragraphs = Array.from({ length: 12 }, (_, i) => i + 1);
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="scroll-top"][data-part="root"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="scroll-top"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `vig-scroll-top` host (fixed, or sticky with `parent`). |   |
| `trigger` | The button. | `visible, hidden` |
| `icon` | The arrow icon. |   |

## Accessibility

- A native `<button>` named by `labels.ariaLabel` (locale default "Scroll to top"). While hidden it is `visibility: hidden`: out of the tab order and the accessibility tree.
- After scrolling, focus moves to the top of the content so keyboard and screen-reader users continue from there: `focusTarget`, else the page's `<main>` (the usual skip-link target) or the scroll container, given `tabindex="-1"` for that move when it is not focusable (removed once focus leaves it). Focus moves the same way when the button hides while focused.
- Scrolling is smooth, and instant for users who prefer reduced motion.
- The page is watched by one IntersectionObserver per threshold on a 1px sentinel that every scroll-top shares (appended to `<body>`), not by a scroll listener. In a cross-origin iframe, which ignores the observer's margin, one shared passive scroll listener answers instead. When `<body>` rather than the document scrolls, place a `target="parent"` scroll-top inside the scrolling element. Nothing runs on the server, where the button renders hidden.

## API

### ScrollTop

Selector: `vig-scroll-top` · Kind: component

A floating button that appears once the page (or, with `target="parent"`, the scrolling element it is placed in) has scrolled more than `threshold` pixels. It scrolls back to the top — smoothly, instantly for users who prefer reduced motion — and moves focus to the top of the content, so keyboard and screen-reader users continue from there: `focusTarget`, else the page's `<main>` (or the scrolling element), made focusable with `tabindex="-1"` when it is not.

**Example :**

```html
<vig-scroll-top />
```

The page is watched with an IntersectionObserver on one sentinel that every scroll-top shares (no scroll listener; in a cross-origin iframe, which ignores the observer's margin, one shared passive scroll listener); a parent with a passive scroll listener on that element. A page whose `<body>` is the scrolling element needs `target="parent"` inside it. Hidden on the server and until the first check. When the button hides while it has focus (Home pressed on it), focus moves the same way.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `focusTarget` | `HTMLElement \| undefined` | `undefined` | Where focus goes after scrolling up; defaults to `<main>` (page) or the scrolling element. |
| `labels` | `Partial<ScrollTopLabels>` | — | Texts (i18n): the button's accessible name; locale default "Scroll to top". |
| `target` | `ScrollTopTarget` | `'window'` | `window` (default): the page; `parent`: the element the scroll-top is a direct child of. |
| `threshold` | `number` | `400` | Pixels scrolled before the button appears. |
| `variant` | `Variant` | `'secondary'` | — |

#### Methods

| Signature | Description |
| --- | --- |
| `scrollToTop(): void` | Scrolls the page (or the parent) to the top and moves focus there. |
