13 · Estrutura e shell

ScrollTarget

rola até o bloco quando o servidor muda o target · scope · scroll_to

Rola suavemente até um bloco [data-block] quando o servidor muda o target.

O servidor muda o target e o painel rola (e scroll_to)

target
scroll_to

Contratos

Cláusulas, vigência e reajuste.

Alternativas

Fornecedores comparados lado a lado.

Histórico

Compras anteriores e variação de preço.

Anexos

Propostas, e-mails e planilhas.

.heex
<%!-- o servidor guarda o target; o hook rola o painel até [data-block="<target>"] a cada mudança.
     scroll_to: LedgerUI.scroll_to(socket, ~s(#doc [data-block="historico"]), block: "start") --%>
<.live_component module={LedgerShowcaseWeb.Demos.ScrollDemo} id="st-demo" />

Uso

<.scroll_target id="detail-st" target={@focus} block="start" scope="#detail-doc" />

Regras

  • Rola uma vez a cada mudança de target (também ao montar com um); o mesmo valor não rola de novo.
  • scope limita a busca a um contêiner, para dois painéis com os mesmos nomes de bloco.
  • LedgerUI.scroll_to/3 rola até qualquer seletor, uma vez, sem componente.
  • Com prefers-reduced-motion: reduce a rolagem é instantânea.

Referência da API

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

scroll_target/1: scrolls smoothly to a block when the server changes target.

<.scroll_target id="purchase-detail-1" target={@focus} block="nearest" />

<section data-block="contracts">...</section>
<section data-block="alternatives">...</section>

Renders a hidden element; the hook LedgerUI.ScrollTarget (priv/static/ledger_ui/hooks/scroll_target.js) scrolls to [data-block="<target>"] once each time target changes (also when it mounts with one). A re-render with the same target does not scroll; nil or "" does nothing. With prefers-reduced-motion: reduce the scroll is instant. A target with no matching element does nothing.

block is where the element lands (start, center, end, nearest).

Scope

By default the block is searched in the whole document (the first [data-block=...]). Set scope to a CSS selector to search only inside that container, so two panels that use the same block names do not clash:

<.scroll_target id="st-1" target={@focus} scope="#purchase-detail-1" />

From the server

LedgerUI.scroll_to(socket, selector, opts) scrolls to any selector, once, with no component (block: and scope: as above):

{:noreply, LedgerUI.scroll_to(socket, ~s([data-block="contracts"]), block: "start")}

<.scroll_target>

Attr Type Default Values / notes
id (required) :string Needed by the hook.
target :string nil Value of data-block to scroll to; scrolls on change.
block :string "nearest" start, center, end, nearest
scope :string nil Selector of the container searched; the whole document when nil.

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