13 · Structure and shell

Sticky

a header that sticks while scrolling · offset · shadow when detached

Keeps a header or bar visible while the content scrolls underneath; gains a shadow when there is content behind it.

Scrolling with the header stuck

Pedidos de hoje12
.heex
<.sticky id="sticky-demo" preview body_class="animate-[mo-scroll_8s_ease-in-out_infinite]" body_style="--sy:-160px">
  <:header>
    <.icon name="cart" size="md" />Pedidos de hoje<span class="min-w-0 flex-1"></span><.badge canvas>12</.badge>
  </:header>
  <i style="width:90%"></i>
  <i style="width:70%"></i>
  <i style="width:85%"></i>
  <i style="width:60%"></i>
  <i style="width:95%"></i>
  <i style="width:75%"></i>
  <i style="width:80%"></i>
  <i style="width:65%"></i>
  <i style="width:88%"></i>
  <i style="width:72%"></i>
  <i style="width:90%"></i>
  <i style="width:66%"></i>
</.sticky>

Usage

<.sticky offset="64px">
  <.table_toolbar … />
</.sticky>

Rules

  • A translucent background with blur so the content shows through behind it.
  • The shadow only appears when the content passes underneath.

API reference

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

A block that sticks to the top of the page and takes classes while the page is scrolled: a header that gets an edge, a bar that gets a shadow.

<.sticky id="page-bar" stuck_class="shadow-md">...</.sticky>

The hook LedgerUI.Sticky (priv/static/ledger_ui/hooks/sticky.js) adds stuck_class and sets data-stuck once the page is scrolled past offset pixels, and takes them off when it is back. The block is sticky top-0; without a stuck_class it draws a 1px bottom line while stuck.

Preview

preview draws the canvas picture of the behavior: a frame with the :header slot on a translucent bar and the content scrolling under it (body_class / body_style go to the content, for the scrolling animation). No hook. For the documentation and the showcase only.

<.sticky id="demo" preview body_class="animate-[mo-scroll_8s_ease-in-out_infinite]" body_style="--sy:-160px">
  <:header>Orders today</:header>
  <i style="width:90%"></i>
</.sticky>

<.sticky>

Attr Type Default Values / notes
id (required) :string
offset :integer 0 Pixels the page scrolls before the block counts as stuck.
stuck_class :string nil Classes added while stuck. Without it the hook draws a 1px bottom line.
preview :boolean false The canvas picture of a sticky header.
body_class :string nil With preview: extra classes of the content.
body_style :string nil With preview: the style of the content.
class :any nil

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

Slots:

  • <:header> - With preview: the content of the bar.
  • <:inner_block> (required)