Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-autosave

Save form drafts to localStorage as the user types and restore them on the next visit. Skips passwords, hidden inputs, buttons and card or one-time codes. Clears on submit or reset; drafts expire after 24 hours.

Overview

data-autosave on a <form> keeps a draft of what the user has typed. If the page is closed or reloaded, the draft is restored on the next visit and announced. The draft is deleted when the form is submitted or reset, and a draft older than 24 hours is deleted instead of restored.

Drafts are written through VBStore to localStorage. Secrets and server-owned values are never written: see Privacy.

Usage

Give the form a key that is unique across your site. Fill in some fields in the demo, then reload the frame.

What happens:

  1. input and change events on the form schedule a save 500ms after the last one.
  2. The save collects every named, persistable control in form.elements, including form-associated custom elements such as <combo-box>.
  3. On load, a draft younger than 24 hours is written back into those controls. Readonly and disabled controls are left alone.
  4. Each restored control receives input and change events, so data-count, data-grow and data-show-when catch up, then the form fires autosave:restore and the restore is announced.
  5. Submit deletes the draft and cancels any pending save. Reset does the same once the reset has gone through (a cancelled reset keeps the draft).

Options

AttributeOnValuesDefaultEffect
data-autosave <form> string, or empty — Turns autosave on. The value is the storage key. Empty (<form data-autosave>) derives the key from the form's method and resolved action, for example post https://example.com/contact.
data-autosave-retain <form> space-separated field names all fields Allowlist. Only the named fields are saved and restored. The privacy filter still applies to them.
data-autosave-ignore any control, or a container such as <fieldset> boolean — Never save or restore this control (or anything inside the container).
data-label-restored <form> string Draft restored Text of the restore announcement. Set it to localize the message.
data-autosave-init <form> set by the script — Marks a wired form, for styling and tests. Do not set it yourself. It is not the double-binding guard, so a cloned form (which copies the attribute) is still wired.
Fixed behaviour
DetailValue
StoragelocalStorage through VBStore
Storage keyvb:autosave:{key}
Stored value{ "data": { name: value, … }, "timestamp": ms }
Debounce500ms after the last input or change
Expiry24 hours after the last save; an expired draft is deleted on the next load

Values are stored per field name: a string for text-like controls and the checked radio, an array of checked values for checkboxes (so a group sharing one name round-trips), and an array of selected values for <select multiple>.

Events

EventFired onDetailWhen
autosave:restore the form (bubbles) { key, fields } — the storage key and the names of the restored fields After a draft was written into at least one control and the controls received their input/change events.

Saving and clearing fire no events of their own. Use autosave:restore to offer a way out of a restored draft: form.reset() puts every control back to its default and deletes the draft.

Privacy

Drafts sit in localStorage as plain JSON for up to 24 hours. Anyone using the same browser profile can read them, and so can any script running on your origin (including injected script after an XSS bug). Keep that in mind on shared or kiosk devices, and leave autosave off for forms where the risk is not worth the convenience.

Never stored

These controls are skipped when saving and are never written to when restoring, whatever data-autosave-retain says:

  • type="password", type="hidden" (CSRF tokens, record versions and nonces always keep their fresh server value) and type="file"
  • Buttons: <button> and input[type=submit|button|reset|image], so named submit buttons keep their own values
  • <output> and <fieldset>
  • Controls with autocomplete="off"
  • Controls whose autocomplete names a secret: cc-number, cc-csc, cc-exp, cc-exp-month, cc-exp-year, one-time-code, new-password, current-password
  • Controls marked data-autosave-ignore, or inside an element marked with it
  • Unnamed controls

Restore also skips readonly and disabled controls. Everything else that has a name is stored, so mark anything sensitive that the rules above do not catch (a national ID number, a medical note) with data-autosave-ignore, or list only the safe fields in data-autosave-retain. The same filter backs data-wizard-persist.

In this demo only the name is kept; reload after filling every field. The form uses method="dialog" so submitting does not leave the page.

Styling

data-autosave draws nothing and has no public styling hooks. The restore announcement is a <toast-msg> toast when that component is registered; style it through toast-msg. Without it, the announcement goes to a visually hidden role="status" region appended to the form (marked data-autosave-status).

Accessibility

  • A restore is always announced: through the toast's live region when <toast-msg> is registered (variant info, 3 seconds, paused while hovered or focused), otherwise through a polite status region in the form. Users of assistive technology learn that fields were pre-filled.
  • Restored controls receive input and change events, so counters, conditional sections and other enhancements are in the right state before the user reaches them.
  • Fields are filled in place; the layout does not move.
  • Without JavaScript the form is an ordinary form; nothing is stored.

Examples

Derived key and selective retention

Leave the value empty to derive the key from method and action. Two forms posting to the same endpoint then share one draft, which suits duplicate copies of one form but not two different forms on the same action (an inline editor and a full editor, say): give those explicit keys. The second form keeps only the fields listed in data-autosave-retain; the message is not stored. Narrowing the list later does not bring back fields stored under the old list.

Checkbox groups and radios

Checkboxes that share a name are stored as the list of checked values and restored by value. Radios store the checked value.

With other enhancements

Restored values reach data-count, data-grow and data-show-when through the events dispatched on restore. The character limit comes from maxlength; data-count itself only takes "" or "words".

Clearing a draft from script

Submit and reset clear the draft for you. To drop it from script without resetting the form, remove the key through VB.store (the public VBStore). Typing in the form afterwards starts a new draft.

The draft is deleted when the submit event reaches the form, whether or not a handler later cancels the submission. If your handler submits with fetch and the request fails, the draft is already gone; the values are still in the fields, so the user can retry.

Related

  • <form> — the element this attribute goes on.
  • data-wizard — multi-step forms; data-wizard-persist keeps the current step and values with the same privacy filter. Use data-autosave for single-page forms, the wizard's persistence for stepped ones.
  • VBStore — the storage layer, its key format and VB.store.
  • <toast-msg> — shows the restore announcement.
  • data-count, data-grow, data-show-when — enhancements that update after a restore.
  • <form-field> — label, control and messages wrapper used in the examples.
  • autocomplete — the browser's own autofill, and the autocomplete="off" fields autosave skips.
↑