SmarkForm FAQ

Below are answers to common questions about SmarkForm’s behaviour, especially around edge cases or features that might catch you off guard at first.

SmarkForm Essentials

What is SmarkForm?

SmarkForm is a lightweight, markup-driven form controller for the web. You annotate your HTML with data-smark attributes and call new SmarkForm(element) — SmarkForm then enhances the subtree to support:

  • Nested subforms that export plain JSON objects.
  • Variable-length lists that export JSON arrays and can grow/shrink at runtime.
  • Smart import/export of any JSON structure, no matter how deeply nested.
  • Context-driven hotkeys for keyboard-driven interaction.
  • Actions & events for custom behaviour without writing DOM manipulation code.

It has no runtime dependencies and ships as a single minified 116KB bundle (ESM or UMD) before gzip. It is not a React/Vue/Angular component library — it is a DOM-first library that works best with server-rendered or static HTML. See Features for the measurement details.

See the Introduction and Showcase for a fuller picture.

Do I need a <form> tag?

No — but you can. SmarkForm works with any block-level element — <div>, <section>, <fieldset>, a plain <form>, etc. The root element is just the DOM node you pass to new SmarkForm(element).

<!-- All of these are valid SmarkForm roots -->
<div id="myForm">…</div>
<section id="myForm">…</section>
<form id="myForm">…</form>

Using a <form> tag is entirely optional. When you do, SmarkForm automatically intercepts the native submit event and routes it through the submit action — so native <button type="submit"> and <input type="submit"> elements work exactly as expected, and all the HTML5 per-button overrides (formaction, formmethod, etc.) are honoured.

See Can I use a classic HTML form submission? and How do I submit form data to a backend?.

How do I get and include SmarkForm?

The quickest way is a CDN import — no build step needed:

<!-- UMD: adds SmarkForm as a global variable -->
<script src="https://cdn.jsdelivr.net/npm/smarkform/dist/SmarkForm.umd.js"></script>

<!-- or as an ES module -->
<script type="module">
  import SmarkForm from "https://cdn.jsdelivr.net/npm/smarkform/dist/SmarkForm.esm.js";
</script>

For production, pin the CDN URL to a specific release so your app isn’t affected by future changes. See the CDN guide for the current versioned URL.

You can also install from npm and use with any bundler:

npm install smarkform
import SmarkForm from "smarkform";

See Getting SmarkForm for all distribution formats (ESM, UMD, CDN, npm, GitHub clone) and more details.

What is the data-smark attribute?

data-smark is how you tell SmarkForm which HTML elements to manage. Any element inside your root node without this attribute is simply ignored by SmarkForm.

The value can be empty (SmarkForm infers everything from context) or a JSON object with configuration options:

<!-- Empty: type inferred from the <input> tag -->
<input type="text" name="city" data-smark>

<!-- JSON options: explicit type, name, and a default value -->
<div data-smark='{"type":"form","name":"address","value":{"city":"Barcelona"}}'>
  …
</div>

<!-- Trigger: the "action" key marks this as a trigger button -->
<button data-smark-action="export">Save</button>

See Core Concepts — The data-smark attribute for the full syntax reference.

What are the different SmarkForm component types?

Every element with a data-smark attribute becomes a component of a specific type. The main types are:

Structural types (hold collections of fields):

Type Role Exports
form Groups fields into a named subform JSON object
list Variable-length list of repeated items JSON array

Scalar field types (hold a single value):

Type Role Exports
input Default for any <input>, <textarea>, or <select> not otherwise matched String
number Numeric field — imports/exports a JavaScript number, not a string Number or null
color Color picker — allows null (unlike native <input type="color"> which always has a value) #rrggbb string or null
radio Group of radio buttons sharing the same name treated as one field; nullable String or null
date Date field with input sanitisation and standardised output format ISO date string or null
time Time field with input sanitisation and standardised output format ISO time string or null
datetime-local Date-and-time field with the same sanitisation/format guarantees ISO datetime string or null

Non-field component types:

Type Role
trigger Button that invokes an action (no field value)
label Smart label that auto-connects to its sibling field

SmarkForm infers the type from context in most cases:

  • <input>, <textarea>, <select> → input (unless a more specific type is inferred from the element’s type attribute, e.g. type="color" → color)
  • <label> → label
  • A data-smark with an "action" key → trigger
  • Explicit "type":"form" or "type":"list" for container elements

Beyond the built-in types, you can define Mixin Types — reusable component blueprints stored in <template> elements. A mixin reference starts with # (e.g. "type":"#myWidget"). See Can I implement my own component types? for a quick overview.

See Component Types Overview for the full reference.

What are triggers and actions?

A trigger is any SmarkForm component with an "action" option; a button is the common case. Clicking it (or pressing its configured hotkey) invokes the named action on the effective context.

<button data-smark-action="export">Save</button>
<button data-smark-action="clear">Clear</button>
<button data-smark-action="reset">Reset</button>
<!-- List-only actions: -->
<button data-smark-action="addItem">Add row</button>
<button data-smark-action="removeItem">Remove row</button>

Not all actions are available on all component types — for example, addItem and removeItem are exclusive to list components. The common actions (export, import, clear, reset) are available on all field components. You can also define custom actions.

See Core Concepts — Triggers for an introduction to action triggers and their contexts.

What is «context» in SmarkForm?

The context of a trigger is the component that receives the action. By default, SmarkForm resolves it automatically — the trigger’s “natural context” is its closest ancestor component that implements the action.

When context is specified, its path starts from the trigger’s enclosing field component and must resolve to a component that implements the action; SmarkForm does not fall back to a different ancestor. Activating a trigger with an explicit context that does not implement the action reports UNKNOWN_ACTION. target is separate and resolves from the effective context.

This means:

  • A clear button placed directly inside a list item’s template clears that list item.
  • If the button is inside a nested subform, it clears the subform.
  • If it is outside all lists and subforms, it clears the enclosing component — which might be a form, a list, or even a scalar field like color or number if the button is embedded inside one using the singleton pattern (see What is the Singleton Pattern?).

For the case where no convenient ancestor exists, you can always target any component explicitly with the "context" property and a relative path:

<!-- Clears the sibling field named "email" from this enclosing form -->
<button data-smark='{"action":"clear","context":"email"}'>Clear email</button>

See Core Concepts — Triggers, the Trigger reference, and Form Traversing for the path bases and copy direction.

What is the Singleton Pattern?

The Singleton Pattern is a technique where you wrap a single HTML form field inside a non-field container tag and declare that container as the SmarkForm component. The container becomes the component; the inner <input> (or <textarea> or <select>) is just the rendering surface.

Without the Singleton Pattern, a bare <input> has no room for sibling triggers:

<input type="color" name="bgcolor" data-smark>

With the Singleton Pattern, the container IS the component — triggers placed inside have it as their natural context:

🔗
❓
<div id="myForm">
  <span data-smark='{"type":"color","name":"bgcolor"}'>
    <input data-smark>
    <button data-smark='{"action":"clear"}'>❌ Clear</button>
  </span>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"), {
    "value": {"bgcolor":"#6366f1"}
});

Because the trigger (clear) is now inside the component, its natural context is the color component itself — no explicit context path needed.

The Singleton Pattern is available for all scalar field types (input, number, color, radio, date, time, datetime-local). It is especially useful for:

  • Embedding a clear button directly inside a color or number field.
  • Adding per-item removeItem buttons inside lists of scalars.
  • Grouping a label and its field as a single named component.

See Core Component Types — The Singleton Pattern for a full walkthrough.

How do I add a ‘clear’ button to a color or number field?

Native <input type="color"> always has a value — it cannot be empty — so SmarkForm provides its own color type that can export null. But how do you clear it without the keyboard?

Option A — Singleton Pattern (recommended): wrap the <input> in a container declared as the SmarkForm component, then place the clear trigger inside that container. The trigger’s natural context becomes the color component:

<span data-smark='{"type":"color","name":"bgcolor"}'>
    <input data-smark>
    <button data-smark-action="clear">❌ Clear</button>
</span>

Option B — Explicit context: keep the bare <input> and place the button anywhere else, but specify the target field by name:

<input type="color" name="bgcolor" data-smark>
<button data-smark='{"action":"clear","context":"bgcolor"}'>❌ Clear</button>

Option A is preferred because it is resilient to future renames — you only need to change the name in one place (the container) rather than in both the <input> and the button’s context path.

The same technique applies to number, date, time, and any other scalar type that you want to associate with extra UI controls.

Lists

Why can’t I remove items from my list sometimes?

SmarkForm enforces a min_items limit (default is 1) on variable-length lists to keep them functional. When you hit this minimum, the “remove” button (data-smark='{"action":"removeItem"}') is automatically disabled. Once you add more items, it re-enables.

Check your list container — if it’s at min_items, that’s why. If you rather want to allow empty lists, set {"min_items": 0}.

Add a disabled CSS rule (e.g. button:disabled { opacity: 0.5; }) to make this state more obvious to users.

Why does my «add» button stop working?

Similar to removal, SmarkForm respects a max_items limit if you set one (e.g. {"max_items": 5}). When the list hits this cap, the “add” button (data-smark='{"action":"addItem"}') disables itself until you remove an item.

Style the disabled state with CSS or raise max_items in your config if you need more slots.

My list won’t let me add items until I fill the current ones—is that intended?

SmarkForm does not block adding new items based on whether existing ones are filled. If your “add” button is disabled, check whether you’ve hit max_items or whether a custom event handler is interfering.

Why does my list render with no items?

A list renders one item slot per element of its current value, and min_items is only a lower bound — it never trims items that your value already provides. So a list declared with {"min_items": 0} and "value": [""] still shows that one (empty) slot; to start truly empty, pass "value": [] or omit the value.

When a list does have no items, you can show a friendly placeholder with the empty_list role: SmarkForm injects that template while the list is empty and removes it as soon as the first item appears.

The example below starts with a single (empty) phone slot, keeps export_empties on so blank slots survive a save, and uses an empty_list placeholder:

🔗
❓
<div id="myForm">
  <button data-smark='{"action":"removeItem","context":"phones","target":"*","preserve_non_empty":true}' title="Remove unused fields">🧹</button>
  <button data-smark='{"action":"removeItem","context":"phones","preserve_non_empty":true}' title="Remove phone number">➖</button>
  <button data-smark='{"action":"addItem","context":"phones"}' title="Add phone number">➕</button>
  <strong data-smark="label">Phones:</strong>
  <ul data-smark='{"name":"phones","of":"input","value":[""],"export_empties":true,"sortable":true,"min_items":0,"max_items":5}'>
    <li data-smark-role="empty_list" class="row">(None)</li>
    <li class="row">
      <label data-smark>📞 Telephone
        <span data-smark-action="position">N</span>
      </label>
      <button data-smark='{"action":"removeItem"}' title="Remove this phone number">➖</button>
      <input type="tel" data-smark>
      <button data-smark='{"action":"addItem"}' title="Insert phone number">➕</button>
    </li>
  </ul>
</div>
#myForm ul li { list-style-type: none; }
const myForm = new SmarkForm(document.getElementById("myForm"));

👉 Try it: press 🧹 to drop the empty slot and watch the (None) placeholder appear, then use ➕ to add phone numbers.

Touch devices may need a drag-and-drop polyfill such as DragDropTouch. See it in action in the Showcase Kanban.

How do I add animations to list items?

SmarkForm deliberately provides no built-in animation engine — transitions are a design concern that belongs to your CSS. The mechanism is simple: use SmarkForm’s lifecycle events to add and remove CSS classes on list items, and let CSS transition do the rest.

  • afterRender fires after a new item is inserted into the DOM. Add an initial CSS class that hides or offsets the element, then — after a minimal delay to let the browser paint the initial state — add a second class that transitions it to its final visible position.
  • beforeUnrender fires before an item is removed. Remove the “visible” class and return a Promise that resolves after the transition duration. SmarkForm awaits that promise, so the element stays in the document long enough for the exit animation to complete.

The following example demonstrates the full technique:

🔗
❓
<div id="myForm">
  <ul data-smark='{"type":"list","name":"items","min_items":1}'>
    <li>
      <input data-smark type="text" name="value" placeholder="Item…">
      <button data-smark='{"action":"removeItem"}'>✕</button>
    </li>
  </ul>
  <button data-smark='{"action":"addItem","context":"items"}'>➕ Add item</button>
</div>
.animated_item {
    transform: translateX(-100%);
    opacity: 0;
    transition: transform 200ms ease, opacity 200ms ease;
}
.animated_item.ongoing {
    transform: translateX(0);
    opacity: 1;
}
const myForm = new SmarkForm(document.getElementById("myForm"), {
    "value": {"items":[{"value":"First item"},{"value":"Second item"}]}
});const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

myForm.onAll("afterRender", async (ev) => {
    if (ev.context.parent?.options.type !== "list") return;
    const item = ev.context.targetNode;
    item.classList.add("animated_item");
    await delay(1); // yield one tick so the browser paints the hidden state
    item.classList.add("ongoing");
});

myForm.onAll("beforeUnrender", async (ev) => {
    if (ev.context.parent?.options.type !== "list") return;
    const item = ev.context.targetNode;
    item.classList.remove("ongoing");
    await delay(150); // wait for the CSS transition to finish
});

See the Animations guide for detailed notes and runnable examples.

I try to select text in a field inside a sortable list, but it starts dragging instead

This happens because sortable: true makes each list item draggable at the DOM level. When the entire item is draggable, the browser intercepts mouse click-drag gestures to start a drag instead of performing a text selection inside the input.

The fix: give each list item a clear drag handle. Use a SmarkForm label component or assign role: "drag-handle" with data-smark-role.

When upgrading from SmarkForm 0.26.0, replace the removed data-sf-drag-handle marker with data-smark-role="drag-handle" (or data-smark='{"role":"drag-handle"}'). The SmarkForm role option is not the HTML role attribute.

SmarkForm detects rendered label components (<label data-smark>, <legend data-smark>, or another element with data-smark='{"type":"label"}') and explicit drag-handle roles. A role-only annotation does not create a SmarkForm component. When a list item has one or more handles:

  1. The item root remains the native drag source, but dragging may start only from a designated handle.
  2. An interactive element that is itself a handle (such as a menu button) may start a drag; interactive descendants inside a handle wrapper remain blocked.
  3. Without a label or explicit marker, the whole item is the fallback handle, while interactive controls remain excluded as drag-start targets.

Minimal example:

<ul data-smark='{"type":"list","name":"items","sortable":true}'>
  <li>
    <button type="button" data-smark-role="drag-handle" aria-label="Drag to reorder">☰</button>
    <input data-smark name="value" placeholder="…">
    <button data-smark-action="removeItem">✕</button>
  </li>
</ul>

Apply cursor: grab to the marked handle via CSS:

[data-smark-role="drag-handle"] { cursor: grab; }

SmarkForm automatically adds data-smark-label to rendered label handles; use that selector when using labels instead of an explicit role.

Design notes:

  • The handle’s visual content is up to you — a hamburger icon (☰), drag dots, or an <img> can make the affordance clear. SmarkForm labels do not allow text selection by default, which is ideal for a drag handle.
  • An interactive control can also be the drag handle when that exact control has the drag-handle role; interactive descendants inside the handle remain excluded.
  • List items that consist of a single input field and no designated handle fall back to the original “entire item is draggable” behaviour. The recommended pattern for single-field sortable lists is to use the Singleton pattern with a label handle in the wrapping item template.

Touch devices may need a drag-and-drop polyfill such as DragDropTouch. See it in action in the Showcase Kanban.

For more details see the sortable option documentation and the SmarkForm Roles guide.

Data: Import, Export & Reset

My exported JSON is missing some fields—what’s up?

By default, SmarkForm skips empty items in variable-length lists when exporting to JSON. If you want those empty items included (e.g. "" or null), set {"export_empties": true} in your list config.

See the List reference for template roles, export_empties, and worked list examples.

Why are my nested form fields named weirdly in the JSON?

SmarkForm reflects the nesting structure in the exported JSON. A subform named "address" containing a field "city" will export as {"address": {"city": "…"}}.

If you see unexpected nesting, double-check the name values in your data-smark attributes.

Why does my form export null values? How do I map them to HTML fields?

SmarkForm uses null to represent “the user has not provided a value” — this is intentional and distinct from an empty string "".

Why null exists:

  • Native <input type="color"> can’t be empty; SmarkForm’s color type returns null when cleared.
  • Date, time, number, and similar fields also return null when empty rather than "" or 0, to avoid ambiguity in the exported JSON.
  • Radio buttons return null when no option is selected. They also can be de-selected (by clicking the selected option again or by pressing Delete key).

Mapping null to HTML fields on import: when a null value is imported into a field, SmarkForm clears the field to its native empty state. The clear action does the same thing programmatically.

Is there a notNull attribute (or required option)?

No — and there intentionally never will be one at the SmarkForm level.

Here is why: SmarkForm cannot tell whether a field is null because the user deliberately left it empty or because it was never touched. Enforcing non-null at export time would therefore silently coerce data that might be legitimately absent.

The correct approach is validation before submission: if a field must not be empty, prevent the user from triggering the export action until they have filled it in. You can do this with a BeforeAction_export event handler:

myForm.on("BeforeAction_export", ({ data, preventDefault }) => {
    if (data.color === null) {
        alert("Please pick a color before saving.");
        preventDefault(); // cancel the export
    }
});

HTML5’s own required attribute can also help for plain text inputs — those participate in the browser’s built-in constraint validation, which SmarkForm does not interfere with.

How do I submit form data to a backend?

Option A — Native form submission (recommended when using a <form> tag):

Wrap your SmarkForm in a <form> element and add a submit trigger or a plain <button type="submit">. SmarkForm intercepts the browser’s submit event and routes it through the submit action, which flattens the SmarkForm JSON into URL-encoded pairs and submits them natively:

<form id="myForm" method="post" action="/api/submit">
  …
  <button type="submit">💾 Save</button>
</form>

For JSON payloads, set enctype="application/json" and enable JSON encoding on the root SmarkForm instance:

<form id="myForm" method="post" action="/api/submit"
      enctype="application/json">
  …
  <button type="submit">💾 Save</button>
</form>
const myForm = new SmarkForm(document.querySelector('#myForm'), {
    enable_json_encoding: true,  // opt in — disabled by default
});

See submit action for encoding options, attribute resolution, and event hooks.

Option B — Manual fetch (works with any root element):

  1. Add an export trigger button to your form.
  2. Listen to AfterAction_export to receive the data and send it to your server.
<button data-smark-action="export">💾 Save</button>
myForm.on("AfterAction_export", async ({ data }) => {
    await fetch("/api/submit", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data),
    });
});

See Event Handling — Common Patterns for more detail.

Can I use a classic HTML form submission instead of JSON export?

Yes. Wrap your SmarkForm in a real <form> element and SmarkForm will intercept the native submit event automatically — there’s nothing extra to wire up.

<form id="myForm" method="post" action="/submit">
  <input data-smark type="text" name="name">
  <input data-smark type="email" name="email">
  <button type="submit">Submit</button>
</form>

<script>
  new SmarkForm(document.getElementById("myForm"));
</script>

Any <button type="submit"> or <input type="submit"> inside the form works as expected: clicking it (or pressing Enter while it is focused via Tab) submits the SmarkForm data using the form’s method, action, and enctype — just as native HTML would, but populated with SmarkForm’s exported values.

For a JSON API, set enctype="application/json" and SmarkForm sends the data as a JSON object via fetch() instead of URL-encoding it.

See submit action for the full reference, including encoding options and event hooks.

Does pressing Enter in a text field submit the form?

No. In SmarkForm, Enter moves focus to the next field — like Tab but more natural to type. This applies everywhere inside the form container, including non-enhanced elements such as a <select> used for UI purposes (e.g. a theme switcher) that happens to be inside the <form> tag.

The rule is straightforward:

Scenario Result
Enter in a text / textarea / select / radio / checkbox Navigates to next field — does not submit
Mouse click on a submit button (or any child element inside it) Submits
Tab to a submit button then Enter Submits (browser fires a click on the focused button first)
Focused SmarkForm trigger + Enter Fires the trigger’s action — does not double-submit

This design is intentional: keyboard users navigate forms freely with Enter and only submit when they explicitly activate a submit button.

Do I need to enhance submit buttons as SmarkForm triggers?

No. Plain <button type="submit"> and <input type="submit"> elements work without any data-smark enhancement. SmarkForm intercepts clicks on them automatically and invokes the submit action with the button as the submitter.

If you want a submit button to also participate in the SmarkForm trigger system (for example, to use BeforeAction_submit / AfterAction_submit events or to set a different context), you can optionally add data-smark='{"action":"submit"}' — but this is never required.

<!-- Plain submit button — works automatically -->
<button type="submit">Send</button>

<!-- SmarkForm trigger submit — identical behaviour, just more explicit -->
<button type="submit" data-smark-action="submit">Send</button>

What happens to the submit button’s name and value when the form submits?

This depends on the encoding:

Form-encoded (URL-encoded, multipart, plain-text): If the submit button has a name attribute, its name/value pair is appended to the submitted payload — exactly matching native browser behaviour for multi-action forms:

<button type="submit" name="intent" value="save">Save</button>
<button type="submit" name="intent" value="draft">Save as Draft</button>

The server receives intent=save (or intent=draft) alongside the form fields. This lets you tell which button triggered the submission.

This extra entry is not part of the SmarkForm export data. It is only added to the form-encoded payload, not to the JSON object returned by export().

JSON encoding (enctype="application/json"): JSON encoding must be explicitly enabled via enable_json_encoding: true on the root SmarkForm instance. The submitter name/value is not automatically added to the JSON body. You have full control over the payload; access the submitter element via options.submitter inside a BeforeAction_submit handler and add it however you like:

myForm.on("BeforeAction_submit", ({ submitter, data }) => {
    if (submitter?.name) {
        data[submitter.name] = submitter.value; // add to JSON body
        // or append to the URL: new URL(action).searchParams.set(...)
    }
});

Can I use action='mailto:…' to send form data by email?

Yes — with the default (form-encoded) encoding. When SmarkForm submits via a temporary <form> element and the action is a mailto: URL, the browser handles it natively: it opens the user’s email client with the form data pre-filled in the message body (subject, body, etc. depend on the browser and the URL format).

<form id="myForm" action="mailto:contact@example.com" method="get">
  <input data-smark type="text" name="subject">
  <textarea data-smark name="body"></textarea>
  <button type="submit">Send Email</button>
</form>

mailto: is not supported with enctype="application/json". JSON submission uses fetch(), which cannot handle non-HTTP URLs. SmarkForm will throw a clear error if you combine the two. Use the default enctype for mailto: actions.

How do default values and reset work?

The value property in a component’s data-smark options sets its defaultValue. Calling reset() (or clicking a Reset trigger) restores the component to that default.

<!-- Name field defaults to "Alice" -->
<input data-smark='{"name":"name","value":"Alice"}' type="text">

For a form or list, you can set the default on the container:

<div data-smark='{"name":"demo","value":{"name":"Alice"}}'>
  <input data-smark type="text" name="name">
</div>

import() updates the default by default. When you call import(data), the imported data becomes the new default for subsequent reset() calls. This means “Load” and “Reset” naturally work together: after loading new data, Reset brings you back to that loaded data.

// After this import, reset() will restore {name: "Bob"}
await myForm.import({name: "Bob"});

// Temporarily change value
await myForm.import({name: "Temp"}, {set_default: false}); // won't update default

// Reset restores {name: "Bob"}, not the original initialization default
await myForm.reset();

To prevent an import from changing the default, pass set_default: false:

await myForm.import(someData, {set_default: false});
// reset() will still restore the previous default

The same option works from HTML triggers:

<button data-smark='{"action":"import","set_default":false}'>Preview</button>

Importing undefined (which is what reset() does internally) never updates the default — it simply restores the current default value.

How the new default is computed: When set_default: true and data is not undefined, SmarkForm performs a silent export with export_empties: true and stores the result as the new defaultValue. This guarantees that the stored default is a normalized, consistent representation of the imported state — including empty list items that would otherwise be stripped.

Events & Actions

I added an event listener, but it’s not firing—why?

SmarkForm supports events like AfterAction_export, AfterAction_addItem, AfterAction_removeItem, and more. Check:

  1. The event name — it must match the action exactly, e.g. "AfterAction_addItem" (not "AfterAction_add").
  2. Scoping — myForm.on(…) listens to all descendants; myForm.onLocal(…) listens only on that specific component.
  3. Timing — handlers registered after the action fires are too late; register them during initialization.

See the Event Handling page for the full reference.

How do I add custom actions?

Register a custom action on the SmarkForm class before constructing forms that will use it. The action name can then be referenced by a trigger:

SmarkForm.registerCustomAction("myAction", async function(data, options) {
    console.log("Custom action triggered with data:", data);
    console.log("Root form:", this);
});

const myForm = new SmarkForm(document.getElementById("myForm"));
<button data-smark-action="myAction">Run Custom Action</button>

The handler’s this is bound to the root form. Registered custom actions do not automatically emit the built-in BeforeAction_* / AfterAction_* events. The former customActions constructor option is no longer supported; see The SmarkForm Constructor for the current API.

How do I add keyboard shortcuts (hotkeys)?

Add a hotkey property to any trigger’s data-smark object. The value is the KeyboardEvent.key string for the desired key (single printable characters are most portable). Hotkeys are activated with Ctrl + the key:

<button data-smark='{"action":"addItem","hotkey":"+"}'>Add</button>
<button data-smark='{"action":"removeItem","hotkey":"-"}'>Remove</button>
<button data-smark='{"action":"export","hotkey":"s"}'>Save</button>

Hotkey reveal: when the user presses and holds Ctrl, SmarkForm sets a data-hotkey attribute on each currently active trigger button. You add CSS to turn that attribute into a visible badge — SmarkForm intentionally leaves the visual style entirely up to you:

/* Show a small "Ctrl+X" badge on each active hotkey trigger */
[data-hotkey] { position: relative; }
[data-hotkey]::after {
    content: "Ctrl+" attr(data-hotkey);
    position: absolute;
    top: 0.75em;
    left: 0;
    font-size: 0.7em;
    background: #333;
    color: #fff;
    padding: 1px 4px;
    border-radius: 3px;
    white-space: nowrap;
}

When Ctrl is released the data-hotkey attributes are removed and the hints disappear automatically.

Context sensitivity: When a hotkey combination is pressed, SmarkForm searches for matching triggers across the focused component, all its ancestors, and the siblings of those ancestors — ordered by proximity to the focused element. The nearest match wins.

This means you can re-use the same key at different nesting levels: Ctrl+- can remove a phone when focus is inside the phones list, and remove a whole user when focus is at the user level. SmarkForm picks the right trigger automatically based on where the keyboard focus is.

See Hotkeys for full details and examples.

What if I want to reach an outer action with the same hotkey?

Use Alt+Ctrl instead of Ctrl to reach the next matching trigger up the hierarchy.

Following the previous example, if you have Ctrl+- to remove a phone and you want to remove the whole user instead, press Alt+Ctrl+- to skip the first match and trigger the next one.

This is well explained of the end-user guide — 🚀 feel free to link it in your UI or documentation to help your users discover this kind of features.

Can one trigger act on several components at once?

Yes — when the context (and optionally target) of a trigger is a wildcard path (e.g. "*" or "foo/*"), SmarkForm resolves matching components and dispatches the action in sequence to those that implement it. This is the multi-context edge case of a trigger.

A single context:"*" button can therefore affect many independent components across the form in one click:

<!-- Removes empty items from EVERY list in the form at once -->
<button data-smark='{"action":"removeItem","context":"*","target":"*","preserve_non_empty":true}'>🧹 Clean all lists</button>

A few things to keep in mind:

  • Only components that implement the requested action receive the call; others are skipped silently (no error).
  • If no matched component implements the action, the trigger is a silent no-op.
  • Each matched context gets its own dispatch with its own resolved target — so target:"*" operates on the children of each context individually.
  • Actions run sequentially and are awaited one by one.
  • For a normal (non-wildcard) context, behaviour is unchanged: the action fires on a single component as always.

See Wildcard context Multi-Dispatch for full details.

Behaviour & Troubleshooting

Where’s the error message when something goes wrong?

SmarkForm favors silent prevention over loud errors. For example, hitting min_items disables “remove” instead of throwing an error. You can hook into events (e.g. AfterAction_removeItem) to add custom feedback.

myForm.find('/foo/bar') returns null but the field exists

This almost always means find() was called before the form finished rendering.

SmarkForm builds its internal map of field components asynchronously during render(). Before rendering is complete, find() will return null (or undefined) for every path — even valid ones.

Solution: await the rendered promise before calling find():

const myForm = new SmarkForm(document.getElementById("myForm"));
await myForm.rendered; // wait for full render

const field = myForm.find("/foo/bar"); // now safe

Inside event handlers (e.g. AfterAction_*) this is not necessary because those events are only emitted after rendering is complete.

Never call find() in the same synchronous tick as the SmarkForm constructor — even for the root form’s own fields. Always await myForm.rendered first.

What does await myForm.rendered do?

The rendered property is a Promise that resolves once SmarkForm has finished building its internal component tree from the DOM. Rendering is asynchronous because SmarkForm processes data-smark attributes asynchronously to keep the browser responsive for large forms.

Any code that needs to access components via find(), or that needs to programmatically import() initial data, must wait for this promise:

const myForm = new SmarkForm(document.getElementById("myForm"));

// Wait before touching the component tree
await myForm.rendered;

// Now safe to use
const field = myForm.find("/address/city");
await myForm.import({ address: { city: "Paris" } });

Event handlers registered via myForm.on(…) are always called after rendering is complete, so you do not need to await myForm.rendered inside them.

My masked field gets focused unexpectedly after form construction

Some third-party mask libraries (most notably Inputmask) focus the input field as a side effect of their initialization — typically via setTimeout(focus, 0). After SmarkForm constructs the form, you may find that a masked field has keyboard focus even though nothing in your code requested it.

How SmarkForm handles this:

SmarkForm’s _applyMask() saves document.activeElement before calling the factory and restores it afterward. If the library focuses the field synchronously (e.g. IMask.js), the restore works out of the box.

If the library focuses the field asynchronously (e.g. Inputmask via setTimeout), the factory must await a macrotask yield so that the deferred focus fires before the factory returns:

SmarkForm.registerMask("price", async (node) => {
  Inputmask({ alias: "numeric", ... }).mask(node);

  // Yield one macrotask so Inputmask's deferred focus fires.
  // SmarkForm will then restore focus to its previous owner.
  await new Promise((r) => setTimeout(r, 0));

  return {
    get unmaskedValue() { return node.inputmask?.unmaskedvalue(); },
    set unmaskedValue(v) { node.inputmask?.setValue(v); },
  };
});

Without this yield, Inputmask would focus the field after SmarkForm restores focus, leaving the field unexpectedly focused.

Known limitation: if the library re-focuses the field in its blur event handler (as Inputmask does), the programmatic blur() in SmarkForm’s focus restore may be undone. In that case, the only reliable fix is to prevent the library from focusing the field in the first place — or to accept the focus as a library-specific side effect.

See Field Masking — Focus and Mask Factories for the full documentation.

Why does my singleton wrapper raise NOT_A_SINGLETON or SINGLETON_TYPE_MISMATCH?

This is intentional, not a bug. A singleton — a scalar component wrapped in a non-form tag such as a <span> or <div> — must contain exactly one inner field. If the wrapper contains more than one field (or none), SmarkForm raises NOT_A_SINGLETON at render time. If the inner field’s type does not match the scalar type declared on the wrapper, it raises SINGLETON_TYPE_MISMATCH instead.

Both errors usually point to a malformed template. A missing closing tag, swapped open/close tags, etc… may cause differences from what the HTML looks like and the actual DOM structure SmarkForm sees.

See Restrictions and Errors in the Singleton Pattern chapter for the full details.

API & JavaScript

Can I have multiple independent SmarkForm forms on a page?

Yes, with a few guidelines:

  • Do not nest one SmarkForm root inside another. SmarkForm forms should be independent siblings in the DOM.
    <!-- Two independent root forms on the same page -->
    <div id="formA">…</div>
    <div id="formB">…</div>
    
    <script>
    const formA = new SmarkForm(document.getElementById("formA"));
    const formB = new SmarkForm(document.getElementById("formB"));
    </script>
    
  • One root with named subforms is often a better pattern than multiple roots — use data-smark='{"type":"form","name":"billing"}' to create logically separate sections within a single root.
    <!-- One root form with named subforms -->
    <div id="outerForm">
      <div data-smark='{"name":"formA"}'>…</div>
      <div data-smark='{"name":"formB"}'>…</div>
    </div>
    
    <script>
    (async ()=>{
      const outerForm = new SmarkForm(document.getElementById("outerForm"));
      await outerForm.rendered;
      const formA = outerForm.find("/formA");
      const formB = outerForm.find("/formB");
    })();
    </script>
    
  • With the second pattern, absolute paths start from the real root. But this is more an advantage than a limitation since both forms can access the data of each other (if needed).

What’s this «API interface» I keep hearing about?

The API interface is a planned future feature for dynamic data — think fetching options for a <select> component from a server, or loading list items on demand. It is not yet implemented.

See the Roadmap for the current proposal and status.

Is SmarkForm’s API stable? Will upgrading break my code?

Short answer: SmarkForm is in early development (version 0.x.y) and breaking changes can occur in any minor release (0.x.0). We do not provide backward-compatibility guarantees at this stage because we are not yet aware of production deployments that depend on API stability.

What this means in practice:

A change in the patch digit (0.x.y → 0.x.y+1) is a safe upgrade — only bug fixes and documentation updates, no new features, no breaking changes. A change in the minor digit (0.x.0 → 0.(x+1).0) may introduce breaking changes; read the CHANGELOG (or the GitHub release notes) carefully before upgrading.

Recommended npm version range for SmarkForm dependencies:

"smarkform": "~0.15.0"

The ~ (tilde) operator in npm restricts upgrades to patch releases only (0.15.0, 0.15.1, … but not 0.16.0), giving you automatic bug-fix updates while protecting you from breaking changes.

If you are using SmarkForm in production, we would love to hear from you! Open a GitHub discussion or reach out on Telegram. When we know someone is relying on a particular API in production we are happy to provide backward-compatibility assurances and migration paths — we only skip that work because we do not yet know anyone who needs it.

Once SmarkForm reaches 1.0.0 we will adopt Semantic Versioning in full, with no breaking changes between minor releases.

Mixin Types

Can I implement my own component types?

Yes — Mixin Types are the built-in extensibility mechanism.

A mixin type is an HTML <template> element that bundles a component’s markup, styles, and behaviour in one place. Instead of a native type keyword ("form", "list", …) you write the template’s id prefixed with #:

The example below wraps two #labeledInput usages inside a nested form component ("name":"person") so the exported data is grouped under a person key — a common pattern for sub-objects within a larger form. The mixin template itself uses the Singleton Pattern (<span data-smark='{"type":"input"}'>) as its root so that both the <input> and the <label> are natural children of the named component without requiring an extra wrapper:

🔗
❓
<div id="myForm">
  <div data-smark='{"type":"form","name":"person"}'>
    <div data-smark='{"type":"#labeledInput","name":"firstName"}' data-label="First name"></div>
    <div data-smark='{"type":"#labeledInput","name":"lastName"}'  data-label="Last name"></div>
</div>

<template id="labeledInput">
    <span data-smark='{"type":"input"}'>
        <label>
            <span class="li-label"></span>
            <input data-smark type="text">
        </label>
    </span>
    <style>
        .li-label { font-weight: 600; display: block; margin-bottom: 0.2em; }
    </style>
    <script>
        // `this` is the SmarkForm component instance — use its API to personalise
        // each instance. Show the per-usage data-label, falling back to the
        // component's own name when no label is supplied.
        const labelEl = this.targetNode.querySelector('.li-label');
        if (labelEl) labelEl.textContent = this.targetNode.dataset.label ?? this.name;
    </script>
</template>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"), {"smark_mixin_allow_local_scripts":"allow",
    "value": {"person":{"firstName":"Alice","lastName":"Smith"}}
});

The <style> sibling is injected into <head> once (deduplicated), and the <script> sibling runs once per instance with this bound to the SmarkForm component — so you get full API access for event listeners, value manipulation, and anything else the component API exposes.

For a complete reference including external templates, option merge semantics, and error codes, see Mixin Types.

Are mixin styles isolated / scoped?

No — styles injected by a mixin are added to the page’s <head> and are therefore global (just like any other <style> tag you write by hand).

This is by design: SmarkForm keeps the library simple and avoids the complexity of Shadow DOM scoping or CSS Modules. In practice it is easy to work around:

  • Use sufficiently specific CSS class names — e.g. a sf- or myapp- prefix keeps mixin styles from clashing with the rest of the page.
  • Use child selectors (.my-mixin-root > .label) rather than bare descendant selectors to reduce the chance of collisions.
  • Keep mixin CSS focused on the component — background colours, layout, typography — rather than page-wide resets.

The deduplication guarantee (same style block never appears twice in <head>) means you can use a mixin many times on the page without the CSS accumulating.

Can I pass parameters to a mixin?

Yes, in two complementary ways:

Via data-smark options (placeholder overrides template defaults):

<!-- Template defines a default of min_items:1 -->
<template id="tagList">
  <span data-smark='{"type":"list","min_items":1,"max_items":10}'> … </span>
</template>

<!-- Usage site overrides to min_items:3 -->
<div data-smark='{"type":"#tagList","name":"skills","min_items":3}'></div>

Any option in the placeholder’s data-smark wins over the template default. The mixin type reference ("type":"#tagList") is consumed and not forwarded — the resolved type (list) from the template root is used instead.

Via HTML attributes (data-*, aria-*, class, style):

<div data-smark='{"type":"#labeledInput","name":"city"}' data-label="City"></div>

data-* attributes from the placeholder are merged into the expanded node (placeholder wins), so mixin scripts can read this.targetNode.dataset.label to pick up per-instance values.

Can a mixin template reference another mixin?

Yes — nested mixin expansion is fully supported. A template can itself use a "type":"#innerId" reference, and SmarkForm will expand them in order. Circular references (A → B → A) are detected and throw a MIXIN_CIRCULAR_DEPENDENCY error before any infinite loop can occur.

Can mixins load from external files?

Yes. Prefix the #id fragment with a relative or absolute URL:

<div data-smark='{"type":"./widgets.html#scheduleRow","name":"hours"}'></div>

SmarkForm fetches the file once, parses it, and caches the resulting document for all subsequent references to the same URL on the same page. <style> and <script> handling, option merging, and all other semantics work identically for external and local templates.

External template loading is blocked by default. You must opt in via the smark_mixin_allow_external option on the root SmarkForm instance:

// Allow same-origin external templates only
new SmarkForm(el, { smark_mixin_allow_external: 'same-origin' });

Script execution in external templates is also blocked by default; see the Mixin Types reference for the full security model.

Integration & Deployment

Which browsers has SmarkForm been tested on?

SmarkForm follows web standards and avoids browser-specific code, aiming for compatibility with any standard-compliant browser. The test suite currently exercises the following engines via Playwright:

Browser Engine
Chrome / Edge Chromium (desktop)
Chrome for Android / Brave Chromium Mobile (Pixel 5 emulation)
Firefox Gecko
Safari / iOS Safari WebKit (desktop + iPhone 12 emulation)

These are not “the only supported browsers” — they are simply the ones covered by the automated test matrix. SmarkForm is expected to work in any browser that supports modern ES2017+ JavaScript and the standard DOM APIs it relies on.

Can I use SmarkForm in React (or Vue, Angular, etc.) projects?

SmarkForm is not designed for virtual-DOM frameworks and the fit is awkward.

SmarkForm is a DOM-first library. It reads data-smark attributes from real DOM nodes at initialisation time and keeps its own internal component tree in sync with those nodes. That model sits at odds with how React, Vue, and Angular work.

If you want to use SmarkForm inside a framework application, keep it on a static or server-rendered region and avoid letting the framework reconcile the nodes SmarkForm manages. See When SmarkForm Fits for the trade-offs, or give us feedback on your experience.

Where does SmarkForm really shine?

SmarkForm fits best when you already own the HTML and want structured form behaviour without adopting a UI framework: server-rendered applications, static sites, CMS pages, progressively enhanced legacy pages, small internal tools, and widgets that need a form model without a build pipeline.

It is a poor fit when the form UI itself must be generated from a schema, when form state has to participate fully in a virtual-DOM application’s state model, or when you need a built-in validation rule engine.

See When SmarkForm Fits for the fuller comparison, common environments, and an illustrative worked example.

Why does clear restore my default value instead of emptying the field?

On a scalar field, clear imports that field type’s empty value and leaves its default unchanged. On a form, clear imports {} and passes undefined to each omitted child. The result is type-specific: generic input, number, and color fields use it as a reset signal; current date/time/datetime-local fields become null, and radio groups deselect. A child’s local value or HTML value can reappear for types that honor it even though the form-level default was ignored. A list imports []; rows above min_items are removed, and retained minimum rows reset to their own defaults. See the Form and List references for details.

reset imports the current defaultValue; clear does not update it. To empty a particular scalar regardless of its local default, call clear on that field (for example, through {"action":"clear","context":"fieldName"}).

<!-- Clears the form-level value; child-local defaults may reappear -->
<button data-smark='{"action":"clear"}'>❌ Clear form</button>

<!-- Clears only the "colour" field to its type-level empty value -->
<button data-smark='{"action":"clear","context":"colour"}'>❌ Clear colour</button>

Have a question not covered here?

You have a question that isn’t answered here? You found a confusing edge case? You want to know if SmarkForm is a good fit for your project?

Open a discussion on GitHub or reach out at the Telegram community group. We’re happy to help you out and may even add your question to this FAQ if it’s a common one!