InputsReact
A two-state button that can be on or off.
Rendered by React 19.3.1
Loading React…62Tests passing in React · 0 failing
5.6 KiBSize
0.60 msCommit
29.0 msFrame
11.0 msMutations
3.00 msMutatedElements
Not measured yetMemory
productdevbook and 13 upstream authors12 commits · last
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<Toggle />Examples
The demo at the top is the one example this component has so far.
API reference
6 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.
ToggleToggle
A two-state button that can be on or off. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
valueA unique string that identifies the toggle when used | TValue | — |
defaultPressedWhether the toggle button is currently pressed. | boolean | false |
pressedWhether the toggle button is currently pressed. | boolean | — |
onPressedChangeCallback fired when the pressed state is changed. | ((pressed: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void) | — |
nativeButtonWhether the component renders a native <button> element when replacing it | boolean | true |
disabledWhether the component should ignore user interaction. | boolean | false |
| Data attribute | Description |
|---|---|
data-pressed | Present when the toggle button is pressed. |
data-disabled | Present when the toggle button is disabled. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
Toggle has no keys of its own; as a native <button> it takes Enter and Space from the browser.
Accessibility
| Part | Sets | So that |
|---|---|---|
| Toggle | aria-pressed from pressed | a screen reader says whether it is on |
| Toggle | renders a <button> while nativeButton is on | it is reached and activated like any button |
Focus order
- Toggle is one tab stop; inside a ToggleGroup the group decides which toggle takes it.