Knob is a form component to define number inputs with a dial.
import { Knob } from 'primereact/knob';
<Knob>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
import { Knob } from 'primereact/knob';
export default function BasicDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50}>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
Boundaries are configured with the min and max values whose defaults are 0 and 100 respectively.
import { Knob } from 'primereact/knob';
export default function MinMaxDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={10} min={-50} max={50}>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
Step factor is 1 by default and can be customized with step option.
import { Knob } from 'primereact/knob';
export default function StepDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50} step={10}>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
import { KnobTextInstance } from '@primereact/types/shared/knob';
import { Knob } from 'primereact/knob';
export default function TemplateDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50}>
<Knob.Range />
<Knob.Value />
<Knob.Text>
{(instance: KnobTextInstance) => {
const { knob } = instance;
return <>{knob?.state.value}%</>;
}}
</Knob.Text>
</Knob>
</div>
);
}
The border size is specified with the stroke property as a number in pixels.
import { Knob } from 'primereact/knob';
export default function StrokeDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={40}>
<Knob.Range strokeWidth={5} />
<Knob.Value strokeWidth={5} />
<Knob.Text />
</Knob>
</div>
);
}
Diameter of the knob is defined in pixels using the size property.
import { Knob } from 'primereact/knob';
export default function BasicDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={60} size={200}>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
Each of the three components (Knob.Range, Knob.Value, Knob.Text) accepts a color prop to customize their appearance. In addition, strokeWidth is used to determine the width of the stroke of range and value sections.
import { Knob } from 'primereact/knob';
export default function ColorDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50}>
<Knob.Range color="MediumTurquoise" />
<Knob.Value color="PeachPuff" />
<Knob.Text />
</Knob>
</div>
);
}
Knob can be controlled with custom controls as well.
import { Button } from 'primereact/button';
import { Knob } from 'primereact/knob';
import * as React from 'react';
export default function ReactiveDemo() {
const [value, setValue] = React.useState(0);
return (
<div className="card flex flex-col items-center gap-2">
<Knob value={value} size={150} readOnly>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
<div className="flex gap-2">
<Button onClick={() => setValue(value + 1)} disabled={value === 100}>
<i className="pi pi-plus" />
</Button>
<Button onClick={() => setValue(value - 1)} disabled={value <= 0}>
<i className="pi pi-minus" />
</Button>
</div>
</div>
);
}
When readOnly is present, value cannot be edited.
import { Knob } from 'primereact/knob';
export default function ReadOnlyDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50} readOnly>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
When disabled is present, a visual hint is applied to indicate that the Knob cannot be interacted with.
import { Knob } from 'primereact/knob';
export default function DisabledDemo() {
return (
<div className="card flex justify-center">
<Knob defaultValue={50} disabled>
<Knob.Range />
<Knob.Value />
<Knob.Text />
</Knob>
</div>
);
}
Knob element component uses slider role in addition to the aria-valuemin, aria-valuemax and aria-valuenow attributes. Value to describe the component can be defined using aria-labelledby and aria-label props.
Key | Function |
---|---|
tab | Moves focus to the knob. |
left/down arrow | Decrements the value. |
right/up arrow | Increments the value. |
home | Set the minimum value. |
end | Set the maximum value. |
page up | Increments the value by 10 steps. |
page down | Decrements the value by 10 steps. |