Connect behavior without storage hooks
The script attribute loads an application
behavior module. Its optional
initialize(element, { signal })
export can connect any form behavior; it doesn’t have to
configure
beforeSave or beforeRestore.
This writing-help module adds a button to show or hide authored writing tips. It uses an abort signal for its listener and returns a cleanup function to restore the original presentation when the component disconnects. Labels and help text come from the HTML.
Use invented details. Drafts are still saved as plaintext in
this browser’s localStorage; this module does not encrypt
them. Without JavaScript, the writing tips stay visible and
the native form remains usable, but drafts aren’t saved. If
the behavior module fails, the tips remain available and
persistence is blocked. Keep a copy before leaving. The
/contact endpoint is a placeholder, not a
submission service.
<form-saver
script="./writing-help.js"
storage-key="demo:behavior"
>
<form action="/contact" method="post">
<label for="message">Message</label>
<textarea id="message" name="message"></textarea>
<button type="button" hidden
data-toggle-help aria-controls="writing-tips"
aria-expanded="true"
data-show-label="Show writing tips"
data-hide-label="Hide writing tips">
Hide writing tips
</button>
<p id="writing-tips">
Describe what you tried and what you expected.
Don’t include passwords or other sensitive details.
</p>
<button type="submit">Send</button>
</form>
</form-saver>
// writing-help.js (excerpt)
export function initialize(saver, { signal }) {
const button = saver.querySelector('[data-toggle-help]');
// The full module also sets labels and restores the
// authored presentation in its cleanup function.
button.addEventListener('click', toggleHelp, { signal });
}