Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

RepeatController

Steps once per press and keeps stepping while the press is held, as the arrows of a number input do.

Reach for it for a stepper or a scroll arrow. The stepper buttons of RuiNumberField use it.

Import

import { RepeatController, createRepeatController } from '@ecopages/radiant-ui/controllers';

Try it

Press a button once to step by one. Hold it, and after half a second it steps every 50 ms until you release; slide off the button and the steps pause until you come back. At 0 and 100 the step returns false, which stops the repeat for that press. With the keyboard, Enter and Space step on every keydown, so holding the key repeats at the system's rate.

50
<div data-example="repeat">
	<button type="button" data-decrement aria-label="Decrease">-</button>
	<span data-value>50</span>
	<button type="button" data-increment aria-label="Increase">+</button>
</div>
import { RepeatController } from '@ecopages/radiant-ui/controllers';

const MIN = 0;
const MAX = 100;

export function mount(root: HTMLElement): () => void {
	const value = root.querySelector('[data-value]')!;
	let count = 50;

	const stepper = (button: HTMLElement, delta: number) =>
		new RepeatController({
			getTarget: () => button,
			onStep: () => {
				const next = Math.min(MAX, Math.max(MIN, count + delta));
				if (next === count) {
					return false;
				}
				count = next;
				value.textContent = String(count);
			},
		});
	const steppers = [
		stepper(root.querySelector('[data-decrement]')!, -1),
		stepper(root.querySelector('[data-increment]')!, 1),
	];
	steppers.forEach((controller) => controller.attach());
	return () => steppers.forEach((controller) => controller.destroy());
}

onStep takes no argument, so each direction has its own controller. With getHandle, a controller can listen on the host and step for every button of its direction, a button rendered after connect included, as RuiNumberField does with getTarget: () => this and getHandle: (target) => target?.closest('[data-number-field-action="increment"]').

mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.

Config

OptionDescription
getTargetWhere presses, keys and clicks are listened for, resolved on every attach().
getHandle(target) => element: the button that steps, resolved on every press, key and click from the element it reached; see HoldController. Default: the target.
delayMilliseconds a press must last before it repeats. Default 500.
repeatMilliseconds between repeated steps. Default 50.
tolerancePixels the pointer may move before the repeat starts without canceling the press. Default 10.
canStart(event) => boolean: refuses a press. The primary button and the handle are already required.
onStepTakes one step. Return false when nothing changed, such as at a bound, to stop repeating for this press.

Behavior

  • A mouse or pen press steps at once. A touch press steps when it lifts over the handle, or once held over it, so a touch that turns into a scroll or slides off the button does not step.
  • A press held past delay steps every repeat ms until it ends, is canceled (the HoldController rules: moving past tolerance before the repeat starts, a scroll, Escape, the window losing focus) or a step returns false.
  • Steps are skipped while the pointer is outside the handle and resume when it comes back.
  • Enter and Space step on every keydown, so a held key repeats at the system's key repeat rate, and their default is prevented so the button does not also click.
  • A click that comes from no pointer press (detail 0: assistive technology or click()) steps once. The click that follows a pointer press does not, since the press already stepped.

Lifecycle

attach() listens for presses, keys and clicks on the target. destroy() removes them and stops a repeat in progress. With a host, createRepeatController(host, config) does the same on connect and disconnect; see bindController.

Accessibility: what the controller does and what you must provide

The controller keeps the button operable without a pointer: Enter, Space and an assistive-technology click all step. It sets no ARIA. Give each button a name (aria-label="Increase"), and make the value it changes something a screen reader can read, such as the input of a number field, which RuiNumberField is. On touch, set touch-action (such as manipulation), user-select: none and -webkit-touch-callout: none on the buttons, as this page does, so a long press does not select text or open a callout.

Protected hooks

None. Change behavior through the config.