11 · Navigation

Scrollspy

an index that follows the visible section · sliding indicator

A side index that marks the section being read and slides the indicator as the page scrolls.

Scrolling the page with the index following

Dados gerais
Endereço
Fiscal
Financeiro
Observações
.heex
<div class="grid h-[230px] grid-cols-[170px_1fr] gap-4">
  <.scrollspy id="spy1" demo label={nil}>
    <:item href="#" style="--dl:0s">Dados gerais</:item>
    <:item href="#" style="--dl:2s">Endereço</:item>
    <:item href="#" style="--dl:4s">Fiscal</:item>
    <:item href="#" style="--dl:6s">Financeiro</:item>
  </.scrollspy>
  <div class="relative overflow-hidden rounded-xl shadow-[inset_0_0_0_1px_var(--lu-line)]">
    <div class="absolute inset-x-0 top-0 flex animate-[mo-spydoc_8s_cubic-bezier(.4,0,.2,1)_infinite] flex-col gap-2.5 p-3.5">
      <h5 class="mt-1.5 text-sm font-bold">Dados gerais</h5>
      <.skeleton animation="none" class="h-2.5 w-[90%]" />
      <.skeleton animation="none" class="h-2.5 w-[70%]" />
      <.skeleton animation="none" class="h-2.5 w-[80%]" />
      <.skeleton animation="none" class="h-2.5 w-[55%]" />
      <h5 class="mt-1.5 text-sm font-bold">Endereço</h5>
      <.skeleton animation="none" class="h-2.5 w-[90%]" />
      <.skeleton animation="none" class="h-2.5 w-[70%]" />
      <.skeleton animation="none" class="h-2.5 w-[80%]" />
      <.skeleton animation="none" class="h-2.5 w-[55%]" />
      <h5 class="mt-1.5 text-sm font-bold">Fiscal</h5>
      <.skeleton animation="none" class="h-2.5 w-[90%]" />
      <.skeleton animation="none" class="h-2.5 w-[70%]" />
      <.skeleton animation="none" class="h-2.5 w-[80%]" />
      <.skeleton animation="none" class="h-2.5 w-[55%]" />
      <h5 class="mt-1.5 text-sm font-bold">Financeiro</h5>
      <.skeleton animation="none" class="h-2.5 w-[90%]" />
      <.skeleton animation="none" class="h-2.5 w-[70%]" />
      <.skeleton animation="none" class="h-2.5 w-[80%]" />
      <.skeleton animation="none" class="h-2.5 w-[55%]" />
      <h5 class="mt-1.5 text-sm font-bold">Observações</h5>
      <.skeleton animation="none" class="h-2.5 w-[90%]" />
      <.skeleton animation="none" class="h-2.5 w-[70%]" />
      <.skeleton animation="none" class="h-2.5 w-[80%]" />
      <.skeleton animation="none" class="h-2.5 w-[55%]" />
    </div>
  </div>
</div>

Usage

<.scrollspy target="#doc">
  <:item href="#dados">Dados</:item>
  <:item href="#fiscal">Fiscal</:item>
</.scrollspy>

Rules

  • Marks the section whose title passed the top; a click scrolls smoothly to it.
  • A 2px indicator in ink on the left.

API reference

Todos os function components, atributos, valores e slots que este componente cobre.

A list of links to the sections of the page that marks the section in view.

<.scrollspy id="settings-nav" offset={96}>
  <:item href="#profile">Profile</:item>
  <:item href="#security">Security</:item>
</.scrollspy>

The hook LedgerUI.Scrollspy (priv/static/ledger_ui/hooks/scrollspy.js) watches the elements the links point at and moves aria-current="true" to the link of the section at the top of the view (offset pixels from the top, 120 by default). A click scrolls to the section smoothly (instantly with prefers-reduced-motion). The marked link turns ink and a 2px bar on the left edge slides to it: the hook writes --spy-y and --spy-h (the link's offset and height) on the nav and the bar follows them.

The server renders aria-current="true" on an item given active (the initial state); the hook only updates it as the page scrolls.

The root is a <nav>; wrap it in a grid yourself. label: nil omits the aria-label. demo is the animated mock (the marker walks over the links by itself: stagger them with style="--dl:2s"), which does not need the hook. Styled only with Tailwind utilities; the parts are data-part="scrollspy" and "scrollspy-link".

<.scrollspy>

Attr Type Default Values / notes
id (required) :string
label :any "On this page"
offset :integer 120
target :string nil A selector ("#doc") of the element that scrolls the sections, when it is not the page.
demo :boolean false The animated mock: the marker walks over the links, no hook needed.
class :any nil

Any other HTML/phx attribute goes to the root element.

Slots:

  • <:item> (required) - attrs: href, active, class, style, id