XY pad
Two numbers set with one drag across a field, or with the arrow keys.
Demo
Usage
import { XYPad, type XYPadValue } from '@tuttn/slant'
// Controlled: pass value and onValueChange, which gets { x, y } as numbers.
const [light, setLight] = useState<XYPadValue>({ x: 30, y: 70 })
<XYPad label="Key light position" value={light} onValueChange={setLight}
x={{ format: value => `${value}%` }} y={{ format: value => `${value}%` }} />
// Uncontrolled: leave value out; it starts from defaultValue, or the centre
// of each axis. Each axis has its own range, step, name and format.
<XYPad label="Camera" size="sm"
x={{ min: -45, max: 45, label: 'Pan', format: value => `${value}°` }}
y={{ min: -30, max: 30, step: 5, label: 'Tilt', format: value => `${value}°` }} />
// In a form: a name submits both values, as light-x and light-y.
<XYPad label="Key light position" name="light" defaultValue={{ x: 30, y: 70 }} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | XYPadValue | — | Both values. Pass it to control the pad; leave it out to let the pad keep its own. |
defaultValue | XYPadValue | the centre of each axis, snapped to its step | Where an uncontrolled pad starts. |
onValueChange | (value: XYPadValue) => void | — | Called with both values, as numbers, whenever a drag or a key moves the thumb. |
x | XYPadAxis | — | The horizontal axis: its range, step, name and format. It runs from the inline start, so it mirrors under RTL. |
y | XYPadAxis | — | The vertical axis: its range, step, name and format. The top of the pad is its `max`. |
label | string | — | The pad's accessible name, on the group that holds both axes. |
largeStep | number | 10 × each axis's step | The step Shift+arrow and Page Up/Page Down take. |
disabled | boolean | false | Dims the pad and ignores input. A disabled pad is not submitted with its form. |
name | string | — | Submits both values with a form, as `${name}-x` and `${name}-y`. |
form | string | — | The id of the form the values belong to, when the pad sits outside it. |
size | 'sm' | 'md' | 'lg' | 'md' | The pad's side: 140, 200 or 260px. It shrinks to fit a narrower container. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root, the group that holds the field and the readout. |
An axis is { min?, max?, step?, label?, format? }, from 0 to 100 in steps of 1 by default. Every value is clamped to its axis and snapped to its step, whether it comes from a key or the pointer. The top of the pad is y's max.
Keyboard
Tab reaches the pad once, on the axis used last (X at first). Left and Right move x, Up and Down move y, and focus follows the axis that changed. Shift multiplies the step to largeStep (ten steps by default), Page Up and Page Down move y by it, and Home and End send x to its ends. Under <Slant dir="rtl"> the pad mirrors, so Left and Right swap.
Pointer
Pressing anywhere on the field moves the thumb there, and dragging carries it, even past the edges (the values clamp). The press focuses the X input without scrolling, so the arrow keys carry on from where the pointer left off.
Screen readers
The pad is a group named by label holding two native range inputs, one per axis, each named by its axis's label and described as a “2D slider” (messages.xyPad). Each reads its own value, through format when given, so moving the light reads “X, 31%”. The readout under the field shows the same values and is hidden from assistive technology. With a name, the inputs submit as name-x and name-y; a disabled pad submits nothing.
Motion
Key steps glide the thumb and crosshair over --sl-duration-fast; a drag follows the pointer exactly. Under reduced motion every move lands at once, and the thumb always rests where the value is, on the server too.