useKnob
Hook that manages circular dial state, SVG arc computation, and keyboard/mouse/touch interaction.
Usage#
import { useKnob } from '@primereact/headless/knob';const { rootProps, svgProps, rangeProps, valueProps, textProps, state } = useKnob({
defaultValue: 50
});
<div {...rootProps}>
<svg {...svgProps}>
<path {...rangeProps} />
<path {...valueProps} />
<text {...textProps}>{state.value}</text>
</svg>
</div>;useKnob manages numeric value state with SVG arc path computation and pointer/keyboard interaction. See Primitive for a component-based API.
Features#
- SVG arc geometry —
rangePathandvaluePathstrings are computed fromvalue,min,max,size, andstrokeWidth - Pointer and keyboard input — mouse drag, touch drag, click-to-set, plus arrows, Home/End, PageUp/PageDown
- Configurable geometry —
size,strokeWidth,rangeColor,valueColor, andtextColorcontrol the visual output - Range and step —
min,max, andstepdefine the value domain with precise rounding - Disabled and read-only modes — interaction blocked via
disabledor locked to display viareadOnly - Reactive state —
state.valueexposes the current number for custom labels or badges
Working with callbacks#
Controlled value#
Use value and onValueChange for full programmatic control. The callback receives { originalEvent, value }.
const [value, setValue] = React.useState(50);
const knob = useKnob({
value,
onValueChange: (e) => setValue(e.value)
});Custom range and step#
Set min, max, and step to define a non-default value domain — for example, a bipolar control centered on zero.
const knob = useKnob({
min: -50,
max: 50,
step: 5,
defaultValue: 0
});Styling the arc#
Override size, strokeWidth, and the three color props to match your design system without writing custom SVG.
const knob = useKnob({
size: 200,
strokeWidth: 8,
rangeColor: '#e0e0e0',
valueColor: '#22c55e',
textColor: '#22c55e',
defaultValue: 75
});Read-only displays#
Use readOnly to show a value without allowing changes while keeping the knob focusable and announced to assistive tech.
const knob = useKnob({ readOnly: true, defaultValue: cpuUsage });Styling with data attributes#
Every prop object includes data-scope="knob" and a data-part attribute. State-dependent attributes are added automatically.
[data-scope='knob'][data-part='range'] {
fill: none;
stroke: #e0e0e0;
stroke-linecap: round;
}
[data-scope='knob'][data-part='value'] {
fill: none;
stroke: var(--p-primary-color);
stroke-linecap: round;
}
[data-scope='knob'][data-part='text'] {
font-size: 1rem;
fill: var(--p-text-color);
}
[data-scope='knob'][data-part='root'][data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}API#
useKnob#
| Name | Type | Default |
|---|---|---|
defaultValue | number | — |
| Value of the knob. | ||
value | number | — |
| The default value of the knob. | ||
size | number | 100 |
| Size of the component in pixels. | ||
step | number | 1 |
| Step factor to increment/decrement the value. | ||
min | number | 0 |
| Mininum boundary value. | ||
max | number | 100 |
| Maximum boundary value. | ||
strokeWidth | number | 14 |
| Width of the knob stroke. | ||
rangeColor | string | — |
| Background color of the range arc. | ||
valueColor | string | — |
| Background color of the value arc. | ||
textColor | string | — |
| Color of the value text. | ||
tabIndex | number | 0 |
| Index of the element in tabbing order. | ||
invalid | boolean | — |
| When present, it specifies that the component should have invalid state style. | ||
disabled | boolean | — |
| When present, it specifies that the component should be disabled. | ||
readOnly | boolean | — |
| When present, it specifies that the component value cannot be edited. | ||
ariaLabelledby | string | — |
| Establishes relationships between the component and label(s) where its value should be one or more element IDs. | ||
ariaLabel | string | — |
| Used to define a string that labels the element. | ||
onValueChange | (event: useKnobChangeEvent) => void | — |
| Callback fired when the knob's value changes. | ||
Accessibility#
Arrow keys step the value, Home/End jump to min/max, and mouse/touch drag updates the angle. See Primitive for full WAI-ARIA compliance details.