Skeleton swap

Skeletons, then the content they stood for, in the same place and without the box jumping.

Skeleton swap

No script for the swap itself. Only event needs Alpine.

Use it

<x-ds::skeleton-swap :loaded="$report !== null">
    <x-slot:placeholder>
        <x-ds::skeleton :lines="3" :shimmer="true" />
    </x-slot:placeholder>

    @if ($report)
        <x-reports.summary :report="$report" />
    @endif
</x-ds::skeleton-swap>

The placeholder and the content share one grid cell, so the box is as tall as the taller of the two from the first paint, and the swap is a crossfade with the motion tokens. Without a placeholder slot it shows three skeleton lines.

Props

PropTypeDefaultWhat it does
loadedboolfalseThe content is ready. False renders aria-busy="true" and the placeholder.
eventstring—A window event that shows the content, report-ready. Needs Alpine.
Slot
defaultThe content.
placeholderSkeletons shaped like it.

What flips it

The face on show is read from aria-busy on the root, the attribute a screen reader needs anyway, so there is one state and nothing to keep in step. Anything that sets or removes it swaps the faces:

  • The server, through loaded. A Livewire re-render that turns loaded true removes the attribute in the morph, and the content fades in.
  • Livewire's wire:loading, with no script of ours: while a request is running, Livewire adds the attribute, and the skeletons come back.
<x-ds::skeleton-swap :loaded="true" wire:loading.attr="aria-busy" wire:target="filter">
    <x-slot:placeholder><x-ds::skeleton :lines="4" /></x-slot:placeholder>
    @foreach ($rows as $row) … @endforeach
</x-ds::skeleton-swap>
  • Alpine state you already have: x-bind:aria-busy="loading ? 'true' : null" on the tag.
  • An event, with event="report-ready" (needs Alpine): dispatching report-ready on the window removes the attribute. $this->dispatch('report-ready') from Livewire does that.

Livewire lazy components need none of this: the placeholder() method's view is swapped for the component when it loads. Return a ds::skeleton from it, shaped like the component.

Size the placeholder like the content

The box keeps the size of the larger face, so a placeholder much taller than the content leaves a gap when it fades, and one much shorter makes the page below move as the content arrives. Match the content's shape roughly: the same number of lines, an avatar where an avatar will be.

Accessibility

  • The placeholder is aria-hidden: a picture of content, with nothing to say.
  • The region is aria-busy="true" while loading, so a screen reader holds off reading it until it is complete.
  • The hidden face is visibility: hidden once its fade has run, which keeps it out of the tab order, the accessibility tree and find-in-page: content not yet shown cannot be reached, as inert would have it, without a script.

Don't

  • Don't use it for an empty result. A placeholder that never resolves looks like a hung page; render an empty state and loaded.
  • Don't wrap a whole page. Swap the part that is waiting.
  • Don't flip it for a request under ~200ms. Use wire:loading.delay.

More in specs/skeleton-swap.md.


From the package repo: edit this page on GitHub.

Was this page helpful?