Hotkeys
π Table of Contents
Overview
SmarkForm supports keyboard shortcuts (hotkeys) for trigger components. Hotkeys let users activate any trigger β addItem, removeItem, export, clear, or any custom action β without reaching for the mouse.
Hotkeys are:
- Opt-in β only triggers that declare a
hotkeyoption participate. - Context-sensitive β the same key can mean different things depending on which component currently has keyboard focus.
- Discoverable β pressing and holding
Ctrlreveals all active hotkeys as visual hints on their trigger buttons.
Defining a Hotkey
Add the hotkey property to a triggerβs data-smark object:
<!-- Add a new list item with Ctrl+Plus -->
<button data-smark='{"action":"addItem","context":"phones","hotkey":"+"}'>
β Add Phone
</button>
<!-- Remove the current item with Ctrl+Minus -->
<button data-smark='{"action":"removeItem","hotkey":"-"}'>
β Remove
</button>
The value of hotkey must match the browserβs KeyboardEvent.key string for the desired key (e.g. "+", "-", "s", "Enter", β¦). Single printable characters are the most portable and least likely to conflict with browser or OS shortcuts.
Hotkey Reveal (Ctrl Discovery)
When the user presses and holds Ctrl, SmarkForm:
- Finds all trigger components that have a
hotkeyoption and whose context is an ancestor (or sibling ancestor) of the currently focused element. - Sets the
data-hotkeyattribute on each matching trigger button to the configured key character.
Your CSS can use this attribute to show a visual hint. SmarkFormβs bundled sample CSS shows a small floating badge β you can style it any way you like:
/* Example: show hotkey badge on the button */
[data-hotkey]::after {
content: "Ctrl+" attr(data-hotkey);
position: absolute;
/* β¦ your positioning and style β¦ */
}
When Ctrl is released, the data-hotkey attributes are removed and the hints disappear.
Pressing a key while Ctrl is held activates the matching trigger (if one was revealed).
Holding
Ctrl+Altreveals a second level of hotkeys. This is useful when the same key is used for multiple actions at different nesting levels β pressingCtrlshows the innermost one,Ctrl+Altshows an outer one.
Context Sensitivity
Hotkeys are scoped to the focus context. The same key binding can be re-used in different parts of the form without conflict:
<div id="myForm">
<ul data-smark='{"type":"list","name":"users"}'>
<li>
<input data-smark type="text" name="name" placeholder="User name">
<ul data-smark='{"type":"list","name":"phones"}'>
<li>
<input type="tel" data-smark name="phone" placeholder="Phone">
<!-- Ctrl+- removes a phone when focus is inside the phones list -->
<button data-smark='{"action":"removeItem","hotkey":"-"}'>β Phone</button>
</li>
</ul>
<button data-smark='{"action":"addItem","context":"phones","hotkey":"+"}'>β Add phone</button>
<!-- Ctrl+- removes a user when focus is at the user level (outside phones) -->
<button data-smark='{"action":"removeItem","hotkey":"-"}'>β Remove user</button>
</li>
</ul>
<button data-smark='{"action":"addItem","context":"users","hotkey":"+"}'>β Add user</button>
</div>
/* Hold Ctrl to reveal hotkey hints */
button { position: relative; }
[data-hotkey]::after {
content: "Ctrl+" attr(data-hotkey);
position: absolute;
top: -1.6em;
left: 50%;
transform: translateX(-50%);
font-size: 0.7em;
background: #333;
color: #fff;
padding: 1px 4px;
border-radius: 3px;
white-space: nowrap;
}
const myForm = new SmarkForm(document.getElementById("myForm"), {
"value": {"users":[{"name":"Alice","phones":[{"phone":"555-1234"}]},{"name":"Bob","phones":[{"phone":"555-5678"}]}]}
});
Every example in this section comes with many of the following tabs:
- HTML: HTML source code of the example.
- CSS: CSS applied (if any).
- JS: JavaScript source code of the example.
- Preview: Live, sandboxed rendering of the example β fully isolated from the page styles.
- Notes: Additional notes and insights for better understanding. Don't miss itβΌοΈ
π οΈ Between the tab labels and the content there is always an edit toolbar:
βοΈ Editβ activates edit mode: each source tab turns into a syntax-highlighted code editor (powered by Ace) pre-filled with the full, merged source. Changes are sandboxed β the original example is not affected.π Include playground editorβ (only visible in edit mode) controls whether the JSON playground editor is included in the preview. When toggled, the HTML and JS editors update instantly so you can see exactly what code is needed to add or remove it. Disabled for this example.βΆοΈ Runβ (only visible in edit mode) re-renders the Preview from the current editor contents and switches to the Preview tab.
When the focus is inside the phones list, pressing Ctrl+- removes a phone. When focus is at the user level (but not inside phones), Ctrl+- removes the user instead.
Conflict Resolution
When two triggers share the same hotkey at the same context level, SmarkForm uses the following priority rules to decide which one fires:
- Distance from focus β triggers whose context is a closer ancestor of the focused element are preferred.
- Containment β when distance is equal, a trigger whose
targetcontains the focused element takes precedence over a sibling trigger.
Only the highest-priority trigger for each key is revealed and activated. Lower-priority duplicates are silently ignored.
A trigger that is disabled at the moment the hotkey is pressed will not fire, even if it was revealed. The
data-hotkeyattribute is also omitted from disabled buttons so they do not appear in the visual hints.
Accessibility Considerations
- Avoid browser-reserved combinations β
Ctrl+C,Ctrl+V,Ctrl+Z,Ctrl+A,F1βF12,Alt+F4, etc. are used by browsers and operating systems. Prefer punctuation characters (+,-,n,d, β¦) that are not typically claimed. - Provide visible alternatives β hotkeys are a convenience feature. Every action that has a hotkey should also be reachable with the mouse so that users on touch devices or those who do not discover the hotkeys can still operate the form.
- Label your buttons β use
titleattributes or visible text on trigger buttons so their purpose is clear even without hotkey hints.
Further Examples
The Showcase contains exhaustive real-world examples that demonstrate hotkeys in context, including nested lists, multi-level hotkey levels, and mixed trigger layouts.