Save bar

"You have unsaved changes — Discard, Save": a bar that appears at the foot of a form once something in it has changed, and goes again when it is saved, discarded or changed back.

Save bar

Needs Alpine and the package's script. See getting started. Without the script it is an ordinary row of Save and Discard at the end of the form, always shown.

Use it

Put it last inside the form:

<form id="settings" method="POST" action="/settings">
    @csrf
    <x-ds::input name="name" label="Workspace name" :value="old('name', $workspace->name)" />
    <x-ds::switch name="public" label="Public" :checked="$workspace->public" />

    <x-ds::save-bar form="settings" :error="$errors->first()" />
</form>

Save is the form's own submit button and Discard its own reset button, so both work with no script at all.

Livewire, or any wire:model form: the bar cannot see those fields (they post nothing by name), so it would never come up. Pass :dirty="false" and let Livewire say when the form is dirty: see With Livewire. This is the one setup to get right first.

Props

PropTypeDefaultWhat it does
messagestringYou have unsaved changesWhat the bar says, and its region's name.
saveLabelstringSave
discardLabelstringDiscard
formstring—The id of the form it saves and resets. Left out, the form it sits in.
dirtybool or nullnullnull: the script watches the form. true / false: you say, and the script does not watch.
savingboolfalseSave shows its spinner and takes no second press; Discard is disabled.
errorstring—Why the last save failed. Keeps the bar up and is read out at once.
positionbottom topbottomThe edge it sticks to.
guardboolfalseAsk before the page is left while there are changes.

How it knows

With the script, on load it notes what the form would post, and after every input, change and reset compares. It reads the form the way the browser posts it, so checkboxes, radios, multiple selects and the hidden inputs that other components post through (a colour picker, a tag input) all count — including a hidden input a script writes, which fires no event. Change a field back and the bar goes.

Discard resets the form natively, then dispatches discard from the form. A native reset puts typed fields back; it cannot put back what a script wrote. Listen for discard to reset those:

<form id="settings" x-on:discard="$refs.colour.value = @js($workspace->colour)">

With Livewire

wire:model fields post nothing by name, so let Livewire say when the form is dirty and tell the bar not to watch. Livewire's wire:dirty.attr sets the attribute the bar reads:

<form wire:submit="save">
    <x-ds::input label="Name" wire:model="name" />

    <x-ds::save-bar :dirty="false" wire:dirty.attr="data-ds-dirty" wire:target="name"
                    :saving="$saving" />
</form>

Discard is still a reset button: add x-on:discard="$wire.$refresh()" (or call a cancel method) to put the server's values back. wire:loading can drive saving too: wire:loading.attr="aria-busy" on Save, or a $saving property set while the action runs.

Where it sits

It sticks to the foot of the form's scroll area while the form is longer than the window, and sits at the end of the form when the end is in view. It stays in the form's column, beside a sidebar or inside a panel. While it is up it takes its room at the end of the form, so it never covers the last field; while there is nothing to save it takes none, so a form keeps no gap at its end.

Several forms on one page (a settings page of sections) each have their own bar, and each sticks only within its own form: two forms with changes never stack their bars, the upper one stays at the end of its form as you scroll past it.

A field higher up can still be under it as you Tab down the page. Tell the browser to keep that much clear:

html { scroll-padding-bottom: 5rem; }

On a phone it stays clear of the home indicator (env(safe-area-inset-bottom)).

The guard

guard asks the browser to confirm before leaving the page while there are changes, and stays quiet for the form's own submit. Browsers show their own words, not yours, and only ask once the reader has interacted with the page. It is off by default: a prompt on every stray link is worse than the work it saves on most forms.

Accessibility

  • A region named by message, so a screen reader can find it.
  • Its appearance is said once, politely, by a status inside it; it never takes focus from the field being edited.
  • Hidden while clean with visibility, so it is out of the tab order and the accessibility tree, not merely transparent.
  • With reduced motion it appears and goes at once instead of sliding.

Don't

  • Don't put it outside the form without form. It saves and resets the form it is in, or the one form names.
  • Don't auto-save and show a save bar. One or the other.
  • Don't turn on guard for search or filter forms.

More in specs/save-bar.md.


From the package repo: edit this page on GitHub.

Was this page helpful?