HoldController
Reports a press held for a delay, with the cancel rules of a long press and the state to draw its progress.
Reach for it for hold to confirm and for a long-press menu. RepeatController is built on it.
Import
import {
HoldController,
createHoldController,
type HoldEnd,
type HoldEndReason,
} from '@ecopages/radiant-ui/controllers';Try it
Press and hold the button for a second. A bar fills along its bottom edge over --hold-delay while it has data-holding, and the hold deletes. Release early and the click asks you to hold; move the pointer away, scroll or press Escape before the second is up and the press is canceled. With the keyboard, focus the button and press Enter or Space.
Nothing deleted yet
<div data-example="hold">
<button type="button">Hold to delete</button>
<p data-status>Nothing deleted yet</p>
</div>import { HoldController } from '@ecopages/radiant-ui/controllers';
export function mount(root: HTMLElement): () => void {
const button = root.querySelector('button')!;
const status = root.querySelector('[data-status]')!;
const hold = new HoldController({
getTarget: () => button,
delay: 1000,
onStart: () => (status.textContent = 'Keep holding'),
onHold: () => (status.textContent = 'Deleted'),
onEnd: ({ reason, held }) => {
if (reason === 'cancel' && !held) {
status.textContent = 'Canceled';
}
},
});
hold.attach();
const listeners = new AbortController();
button.addEventListener(
'click',
(event) => {
status.textContent = event.detail === 0 ? 'Deleted' : 'Hold the button to delete';
},
{ signal: listeners.signal },
);
return () => {
listeners.abort();
hold.destroy();
};
}The fill is CSS on the state the controller sets:
button {
background-image: linear-gradient(var(--primary), var(--primary));
background-position: left bottom;
background-repeat: no-repeat;
background-size: 0 3px;
touch-action: manipulation;
user-select: none;
-webkit-touch-callout: none;
}
button[data-holding] {
background-size: 100% 3px;
transition: background-size var(--hold-delay) linear;
}mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.
Config
| Option | Description |
|---|---|
getTarget | Where presses are listened for, resolved on every attach(). |
getHandle | (target) => element: the element a press must start in and that the hold is on, resolved on every press from the element pressed. Default: the target. Lets the target be the host while one of several buttons holds, such as (target) => target?.closest('[data-step]'); null starts no press. |
delay | Milliseconds a press must last to be a hold. Default 500. |
tolerance | Pixels the pointer may move before the hold fires without canceling the press. Default 10. |
canStart | (event) => boolean: refuses a press. The primary button and the handle are already required. |
onStart | Called with the pointer event of the accepted press. |
onHold | Called once the press has lasted delay, with the last pointer event of the press. |
onEnd | Called once per press, however it ends, with { reason, held, event }: reason is release or cancel, held whether onHold had run, and event the pointer event, the Escape keydown, the scroll or the blur that ended it. |
Behavior
- Before the hold fires, moving more than
tolerancepx cancels the press. At any point, ascrollanywhere in the document, Escape, the window losing focus or a pointer cancel cancels it. The window losing focus also drops the press, since its release may happen in another window. - A press that was canceled, or that became a hold, does not click: the pointer
clickafter its release is stopped on the target. Aclickwithdetail0(keyboard, assistive technology,click()) passes, which is how the example takes Enter. A quick press that ends beforedelayclicks as usual. - While pressed the handle has
data-holdingand--hold-delay(the delay in ms, for a transition), and once helddata-held. All three are removed when the press ends or is canceled. holdingis whether a press is under way and not canceled, andinsidewhether its pointer is over the handle, for an owner that pauses while it is not.
Lifecycle
attach() listens for presses and clicks on the target; the Escape, scroll and blur listeners are added for each press and removed when it ends. destroy() removes the listeners. With a host, createHoldController(host, config) does the same on connect and disconnect; see bindController.
Accessibility: what the controller does and what you must provide
A hold is pointer only. Give what it does a keyboard path (WCAG 2.1.1), such as the example's Enter, which arrives as a click with detail 0, or a confirm step, and say that a hold is needed in the visible text or the accessible description. The controller sets no ARIA. On touch, set touch-action (such as manipulation), user-select: none and -webkit-touch-callout: none on the handle, so the browser does not claim the press for a scroll, a selection or a callout.
Protected hooks
None. Change behavior through the config callbacks.