Swap button

A ds::button whose face swaps to confirm what it did: "Save" to "Saved", "Follow" to "Following". The swap mechanics are ds::copy-button's, shared.

Anatomy

┌──────────────────┐
│  +  Follow       │   ← ds::button[data-ds-swap-button].group/swap
└──────────────────┘     one grid cell: face at rest, face swapped
  [sr-only role=status]   ← sibling of the button, inside the scope wrapper
  • Scope wrapper: span.contents[data-ds-swap] carrying x-data for dsSwap, so the button lays out in the host's row as a ds::button would.
  • Button: ds::button with variant (default secondary), size, and every host attribute (type, href, wire:click). It carries data-ds-swapped when swapped, rendered by the server and bound to dsSwapped.
  • Faces: the default slot and the swapped slot, each an inline-flex span (gap-2, icon and words), stacked in one grid cell with justify-items-center by the swap recipe (partials/swap.php, Support\Swap::recipe()). The cell is as wide as the wider face from the first paint.
  • Status: sr-only, role="status", outside the button. Empty at first paint; after each change, the text of the face now showing.

States

StateTreatment
at restDefault face; swapped face invisible opacity-0 translate-y-1, aria-hidden, inert
swappedThe reverse: rest face fades up and out, swapped face fades up in
swapped from the server (active)Drawn swapped before Alpine; with revert > 0 the timer starts on init
toggle (revert="0", swap on click)As above, plus aria-pressed
disabled, hover, focus, loadingAs ds::button

The swap is motion-safe: opacity, translate and visibility over duration-popover-enter ease-enter. Visibility is in the transition so the leaving face stays visible until its fade ends. With reduced motion it swaps at once.

Behaviour (dsSwap)

OptionFromDoes
swappedswapped attributeInitial dsSwapped
revertrevert prop, default 2000ms until it swaps back by itself (shared/flag.js timedFlag, as dsClipboard); 0 never
clickswapOnClick, default true unless eventA press swaps it; with revert 0, toggles it. A press while swapped restarts the time
eventeventA window listener: the event swaps it

The press is never prevented: a submit still submits, a link still navigates. The status region is written from a $watch, so the first paint says nothing.

The server state is the active prop and the second face the swapped slot. They shared the name swapped during 2.0's build, which forced the state to be read from the attribute bag before @props (Blade hands a named slot to the view under a same-named prop's variable). A swapped attribute now throws, naming active.

Accessibility

Rolebutton (or link, with href), as ds::button
NameThe face showing. A toggle: the face at rest, by aria-labelledby, whatever is showing
Statearia-pressed on a toggle only
Hidden facearia-hidden="true" and inert, and invisible once faded
AnnouncementPolite role="status" outside the button
KeyboardEnter and Space, as any button
FocusStays on the button through the swap

The APG button pattern: a toggle button's name must not change with its state, or "Following, pressed" reads as two claims. The faces still change for sight.

Do not

  • Do not write a third swap. Faces go through the swap recipe; the timer is timedFlag.
  • Do not swap before the work is done. Use event for a save that can fail.
  • Do not put the status inside the button.
  • Do not let the button change width. That is what the one cell is for.
  • Do not give a prop a slot's name. swapped is the slot only; the state is active.

From the package repo: edit this page on GitHub.

Was this page helpful?