Components / More
Choice
The row a choice control sits in: the control, its label and an optional description, the whole row clickable. Checkbox, Switch and each RadioGroup option are Choice rows that draw their own control.
When to useLink to "When to use"
- Building a new kind of choice control (a star, a coloured chip) that needs the same label, description, forms and touch behaviour as a checkbox.
Use something else when:
- you need a checkbox: Checkbox
- you need an on/off setting: Switch
- one option of several: RadioGroup or SegmentedControl
UsageLink to "Usage"
import { Choice, ChoiceBase } from "@arshad-shah/cynosure";
<Choice
label="Show hidden files"
description="Dotfiles and system folders."
onChange={setShown}
control={(input) => (
<span className="my-star">
<ChoiceBase {...input} type="checkbox" />
</span>
)}
/>control receives the input's props: its id, aria-labelledby, aria-describedby, checked or defaultChecked, onChange, and name, value, required and disabled. Spread them onto ChoiceBase, which hides the native input over your drawing. The input must be a direct child of the element control returns. onChange on Choice receives the new checked boolean.
Done styleLink to "Done style"
strikeWhenChecked strikes through and mutes the label while the input is checked (and not indeterminate), and mutes the description with it. An input placed inside the label does not count.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
controlRequired | Draws the control, given the props for its native input. The input must be a direct child of the element this returns (the label's strike follows it). | (input: ChoiceInput) => ReactNode | |
labelRequired | The control's label. Pressing it toggles the control. | ReactNode | |
strikeWhenChecked | While the control is checked (and not mixed), the label is struck through and muted, and the description muted, so a done task reads as done. Default false. | boolean | false |
className | Added to the root element (the label that wraps the row), for layout. | string | |
style | Layout only, applied to the root element. | CSSProperties | |
disabled | It cannot be changed and is not submitted with a form. | boolean | |
name | Name it is submitted under in a form, when on. | string | |
value | The value submitted with name when on. The browser sends "on" when this is not set. | string | |
defaultChecked | On at first, when uncontrolled. | boolean | |
id | Id of the native input; one is generated when not set. | string | |
onChange | Called when the person turns it on or off, with the new state. | ((checked: boolean) => void) | |
description | Help text shown under the label and read with the control. | ReactNode | |
checked | Controlled: whether it is on. | boolean | |
required | It must be on for the form to submit. | boolean | |
size | Size of the control and its text. Default "md". | Size | "md" |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
Behaviour and accessibilityLink to "Behaviour and accessibility"
| Input | Result |
|---|---|
| Click or tap the control, label or description | Toggles the input |
| Space on the focused input | Toggles it |
- Name and description: the label names the input and the description is linked with
aria-describedby. - Forms: the input is native, so
FormData, reset andrequiredwork as the browser does them. - Disabled: the input is disabled and the row is dimmed with a not-allowed cursor.
- Done style: the checked state is what is announced; the strike only shows it. In forced colours the strike stays, in the system text colour; the colour change stops under reduced motion.
TouchLink to "Touch"
The row is at least 24px tall, and 44px on coarse pointers, where the control is centred on the text.
ThemingLink to "Theming"
| Token | Controls |
|---|---|
--cy-choice-gap | Space between the control and the text. Default --cy-space-4. |
--cy-choice-done-color | Label and description colour of a checked strikeWhenChecked row. Default --cy-color-muted. Checkbox sets it from --cy-checkbox-done-color. |
--cy-choice-strike-width | Thickness of the strike. Default: the border width, or 1/16 of the text size when that is larger. Checkbox sets it from --cy-checkbox-strike-width. |
createTheme({ components: { choice: { gap: "12px" } } });RelatedLink to "Related"
Checkbox · Switch · RadioGroup · ChoiceBase
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.