Save Bars
<div class="save-bar" role="region" aria-label="Unsaved changes">
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="button" class="btn">Save Changes</button>
</div>
</div>
The bar names its form with data-form
data-form on the bar is the id of the form it watches. The bar goes after the form and starts with hidden.
<form id="product-form">
<input class="input" type="text" name="title" value="Magic Sheets" />
</form>
<div class="save-bar" data-form="product-form" role="region" aria-label="Unsaved changes" hidden>
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="submit" form="product-form" class="btn">Save Changes</button>
</div>
</div>
The bar shows on a change, hides when it is undone, and stays at the bottom
<div class="scrollbar bg-background h-72 overflow-y-auto rounded-lg border">
<div class="flex flex-col gap-6 p-6">
<form class="flex flex-col gap-6" id="demo-save-bar-form" onsubmit="event.preventDefault()">
<div class="field">
<label for="demo-save-bar-title">Title</label>
<input class="input" type="text" id="demo-save-bar-title" name="title" value="Magic Sheets" aria-describedby="demo-save-bar-title-help" />
<p id="demo-save-bar-title-help">Change this text and the bar appears. Type it back to "Magic Sheets" and the bar goes.</p>
</div>
<div class="field">
<label for="demo-save-bar-meta">Meta description</label>
<textarea class="textarea" id="demo-save-bar-meta" name="meta" rows="8"></textarea>
</div>
</form>
<div class="save-bar" data-form="demo-save-bar-form" role="region" aria-label="Unsaved changes" hidden>
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="submit" form="demo-save-bar-form" class="btn">Save Changes</button>
</div>
</div>
</div>
</div>
Every kind of field is compared: a checkbox, a native select, a styled select and a file
<div class="flex flex-col gap-6">
<form class="grid gap-6 sm:grid-cols-2" id="demo-save-bar-kinds" onsubmit="event.preventDefault()">
<div class="field" data-orientation="horizontal">
<input type="checkbox" class="input" id="demo-save-bar-public" name="public" checked />
<label for="demo-save-bar-public">Is Public</label>
</div>
<div role="group" class="field">
<label for="demo-save-bar-status">Status</label>
<select class="select w-full" id="demo-save-bar-status" name="status">
<option>Draft</option>
<option selected>Active</option>
<option>Archived</option>
</select>
</div>
<div role="group" class="field">
<label id="demo-save-bar-colour-label">Colour</label>
<div id="demo-save-bar-colour" class="select" data-placeholder="Choose a colour">
<button type="button" id="demo-save-bar-colour-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="demo-save-bar-colour-listbox" aria-labelledby="demo-save-bar-colour-label"><span></span></button>
<div id="demo-save-bar-colour-popover" data-popover aria-hidden="true">
<div role="listbox" id="demo-save-bar-colour-listbox" aria-orientation="vertical" aria-labelledby="demo-save-bar-colour-label">
<div role="option" data-value="blue" aria-selected="true">Blue</div>
<div role="option" data-value="white">White</div>
<div role="option" data-value="grey">Grey</div>
</div>
</div>
<input type="hidden" name="colour" value="blue" />
</div>
</div>
<div class="field">
<label for="demo-save-bar-file">Size chart</label>
<input class="input" type="file" id="demo-save-bar-file" name="chart" />
</div>
</form>
<div class="save-bar" data-form="demo-save-bar-kinds" role="region" aria-label="Unsaved changes" hidden>
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="submit" form="demo-save-bar-kinds" class="btn">Save Changes</button>
</div>
</div>
</div>
A bar written without hidden stays up until the form is saved: a new record, or a save that failed
data-warn-on-leave asks before the page is left with unsaved changes
<div class="flex flex-col gap-6">
<form id="demo-save-bar-warn" onsubmit="event.preventDefault()">
<div class="field">
<label for="demo-save-bar-warn-title">Title</label>
<input class="input" type="text" id="demo-save-bar-warn-title" name="title" value="Timeless Watch" aria-describedby="demo-save-bar-warn-help" />
<p id="demo-save-bar-warn-help">Change this, then reload the page or follow a link: the browser asks first. Discard and Save Changes do not ask.</p>
</div>
</form>
<div class="save-bar" data-form="demo-save-bar-warn" data-warn-on-leave role="region" aria-label="Unsaved changes" hidden>
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="submit" form="demo-save-bar-warn" class="btn">Save Changes</button>
</div>
</div>
</div>
The page hears dirty and clean on the form, and calls markClean() after saving without a page load
No changes.
<div class="flex flex-col gap-4">
<form id="demo-save-bar-events" onsubmit="event.preventDefault()">
<div class="field">
<label for="demo-save-bar-events-title">Title</label>
<input class="input" type="text" id="demo-save-bar-events-title" name="title" value="Bollard Lamp" />
</div>
</form>
<div class="flex flex-wrap items-center gap-3">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-save-bar-events-bar').markClean()">markClean()</button>
<p class="text-muted-foreground text-sm" id="demo-save-bar-events-result">No changes.</p>
</div>
<div class="save-bar" id="demo-save-bar-events-bar" data-form="demo-save-bar-events" role="region" aria-label="Unsaved changes" hidden>
<p>Unsaved Changes</p>
<div>
<a href="" class="btn" data-variant="ghost">Discard</a>
<button type="submit" form="demo-save-bar-events" class="btn">Save Changes</button>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("demo-save-bar-events");
const result = document.getElementById("demo-save-bar-events-result");
form.addEventListener("dirty", () => { result.textContent = "dirty: the form has unsaved changes."; });
form.addEventListener("clean", () => { result.textContent = "clean: the form matches its saved values."; });
});
</script>
API Reference
Attributes
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
| data-form | .save-bar | the id of a form | none | The form the bar watches. Without it the script leaves the bar as written. |
| hidden | .save-bar | present or absent | set by the script | Write it so the bar starts hidden. A bar written without it shows until the form is saved. Use that for a new record, or after a save that failed. |
| data-warn-on-leave | .save-bar | present or absent | absent | The browser asks before the page is left with unsaved changes. Discard and Save Changes do not ask. |
| data-dirty | .save-bar | "true", "false" | set by the script | Whether the form holds unsaved changes. |
| href="" | the Discard link | empty | none | Loads the page again, which brings back the saved values. |
| type="submit" form="id" | the Save Changes button | the id of the form | none | Submits the form from outside it. |
Methods and properties
| Member | Type | Description |
|---|---|---|
| dirty | property, read only | true while the form differs from its saved values. |
| check() | method | Compares the form now and shows or hides the bar. Call it after a script has changed a field's value without an input or change event. |
| markClean() | method | Takes the form as it stands as the saved values and hides the bar. Call it after saving without loading a page. |
Events
| Event | On | Detail | Description |
|---|---|---|---|
| dirty | the form, and it bubbles | none | Sent when the form first differs from its saved values. |
| clean | the form, and it bubbles | none | Sent when the form matches its saved values again, by undoing the changes or after a save. |
A checkbox or radio is compared by whether it is ticked, a file field by its files, any other field by its value.
Not compared: buttons, fields inside a dialog, and the filter field of a select.
A rich text editor is asked whether it has changed. Undoing every edit in it does not make it clean again; saving does.
A row added or removed counts as a change. The script that does it must send an input event on the form.
A submit the page handles itself (preventDefault) counts as saved and hides the bar. A submit that loads a page keeps the bar up until that page arrives.
Once the script has started it, the element carries data-save-bar-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.