- ClayJS
- Reference
- API
- Attributes
Attributes
The HTML surface: rich text, saved form values, autosave, regions and owner-only behavior.
The HTML surface: rich text, saved form values, autosave, regions and owner-only behavior.
Attributes#
| Attribute | Where | What it does |
|---|---|---|
editable | any element except a custom element | Rich text editing with a floating toolbar (richclay, default on). Tokens combine: editable="single-line no-toolbar toolbar-on-select". A hyphenated tag is skipped, because editable is a common boolean property on a component and means something else there; such an element opts in with clay-editable instead. Native contenteditable also works for plain text. |
persist | form controls | Writes the control’s current value into the HTML so it survives the save. password, hidden and file inputs are always skipped, so a secret cannot be written into the file by adding one attribute. |
trigger-save | buttons | Clicking it calls clay.save(). Put it on a <button type="button">: on a link the browser follows the href as well, and the navigation can outrun the save. If you want a link, give it an onclick instead: <a href="#" onclick="event.preventDefault(); clay.save()">. |
autosave | <html> | Save automatically after edits settle. Debounced and throttled. ⌘S / Ctrl+S always works too. |
demo-key | <html>, demo plugin | Names the browser storage key demo saves go to: clay:demo:NAME. Without it the key falls back to the page’s pathname. Bump the value whenever the page’s authored content changes, or a returning visitor’s older saved body morphs over the new page. |
clay="…" | any element | Region control: no-save, no-snapshot, no-trigger-autosave, no-dirty, no-watch, no-undo, no-data, editor-ui, freeze, live. Full reference. |
merge="name" | JSON script tags | Multi-writer safety for JSON stored in a script tag (sync plugin). Incoming saves three-way merge the JSON per key against the last synced version instead of replacing the blob, so unsaved local keys survive. Arrays merge by identity (id-style fields; name your own with merge-key="taskId", or per list path with merge-key="players:uid, items:sku"); lists without an id merge by position when both sides kept the same length. A key both sides changed takes the incoming value and records a json conflict in the ledger. Bodies may use relaxed JSON: unquoted keys, single quotes, trailing commas, comments. Requires a JSON type. |
merge-tokens="data-a data-b" / merge-max="data-n" | any element, usually a container | Declares how live sync merges the named data-* attributes on every element inside it. merge-tokens treats the value as a set of space-separated words, so additions and removals from both sides apply. merge-max treats it as a number that only grows, and the larger value wins. Neither records a conflict. data-id is never covered. See live sync. |
editmode:contenteditable | any element | Editable for the owner, inert for visitors. |
editmode:onclick | any element | The onclick runs only for the owner; it’s saved inert so it never runs for visitors. |
add-from / add-to | any element, usually a button | In edit mode, a click clones the <template> named by add-from, gives the clone a fresh data-id, appends it to the element named by add-to, and focuses its first editable field. No script needed: <button add-from="#task-tpl" add-to="#tasks">Add task</button>. |
ephemeral | any element | Per-tab state, as space-separated words: ephemeral="selected". Never saved, synced, undone or counted as a change. Edit with clay.ephemeral.toggle(el, "selected"); style with [ephemeral~="selected"] or Tailwind’s ephemeral-selected:. See Ephemeral state below. |
ephemeral:NAME | any element | Makes the element’s own NAME attribute per-tab: <details ephemeral:open>. The marker is saved; NAME is removed from the saved file. open on <dialog> is per-tab with no marker. See Ephemeral state below. |
clay-mode-switch="live" | <html> | Declares that this document can switch modes without a reload, so clay.setMode runs the live session instead of saving and reloading. Without it the switch saves, writes the receipt and reloads. See switching without a reload. |
opens-in="edit" | <html> | Starts this tab in edit mode, for a tool page. Read once at boot and never rewritten. See how the mode is decided. |
clay-mode-toggle | the page’s own toggle | Marks the control the page gives a person for switching modes. When one is present at boot, the one-time note that tells a person to press the mode key is skipped. The menu plugin marks its own toggle with it. |
editmode:resource | style, link, script | Active for the owner, saved with an inert type so visitors never load it. |
viewmode:disabled / viewmode:readonly | form controls | Disabled or read-only for visitors, live for the owner. |
onbeforesave / onbeforesnapshot / onaftersave | any element | Inline hooks into the save. onbeforesave and onbeforesnapshot run on the clone for every change check, not only for saves that ship, so keep them pure and repeatable: no counters, no logging, no network. onbeforesnapshot also runs for live-sync broadcasts. Example: <div onbeforesave="this.removeAttribute('data-temp')">…</div> Details. |
refetch-on-save | link/style | Reload a stylesheet that is generated on save, without a flash: <link rel="stylesheet" href="style.css" refetch-on-save>. |
canedit | <html>, set by clayjs | canedit="true" while clay.canEdit is true, and absent otherwise. It follows clay:capabilitychange, and it is removed from every save. |
savestatus | <html>, set by clayjs | Read-only state for your CSS: saving, saved, error, offline, conflict. See below. |
unsaved | <html>, set by clayjs | unsaved="true" exactly while the dirty gate says the page holds work a save has not written, and absent once a save has accounted for it. See below. |
The editmode: and viewmode: rows are applied by the session’s controls lease (core/admin-attrs.js) at each commit: entering edit turns each served control into its active form, and the exit hands every one of them back with the value the file gave it. The save also writes the inert form into the file, so a copy a visitor loads carries it already. See attributes across a mode change.
savestatus#
savestatus is removed from every save, like editmode and pageowner, so before clayjs boots none of them is present: key CSS on html:not([editmode="true"]), not html[editmode="false"].
unsaved#
It is what a status chip needs that savestatus cannot say: savestatus reports what the last save did, unsaved whether there is anything since, so html[unsaved="true"]:not([savestatus="error"]):not([savestatus="offline"]) .chip-unsaved { display: inline } shows unsaved work without hiding a saved document’s status CSS. Tab-local like savestatus: removed from every save and never carried to or from a peer. Draft mode sets it the same way; html[draftmode="true"] is what says nothing is being saved.
Ephemeral state#
Per-tab state on a saved element: never saved, never sent to peers, never undone, never counted as a change (so onmutation does not fire), and never written or removed by an incoming merge on an element the tab already has.
ephemeral="selected dragging"— words a script sets for this tab. Edit withclay.ephemeral.add(el, word),remove,toggle(el, word, force)andhas. Style with[ephemeral~="selected"], or the Tailwind variantephemeral-selected:(@plugin "@panphora/clayjs/tailwind";).ephemeral:NAME— makes the element’s ownNAMEattribute tab-only:<details ephemeral:open>. The marker is saved;NAMEis removed from the saved file. Never applies toclass,id,data-id,value,checked,selectedor region attributes (clay,no-save,freezeand the rest).openon<dialog>is tab-only with no marker.