@darksnow-ui/radio-group
v1.0.0
Published
radio-group component for DarkSnow UI
Maintainers
Readme
Radio Group
A set of checkable buttons—known as radio buttons—where no more than one of the buttons can be checked at a time. Built on top of Radix UI Radio Group primitive.
Installation
npm install @darksnow-ui/radio-groupPeer Dependencies
npm install react react-domUsage
import { RadioGroup, RadioGroupItem } from "@darksnow-ui/radio-group"
import { Label } from "@darksnow-ui/label"
export function RadioGroupExample() {
return (
<RadioGroup defaultValue="comfortable">
<div className="flex items-center space-x-2">
<RadioGroupItem value="default" id="r1" />
<Label htmlFor="r1">Default</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="comfortable" id="r2" />
<Label htmlFor="r2">Comfortable</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="compact" id="r3" />
<Label htmlFor="r3">Compact</Label>
</div>
</RadioGroup>
)
}Components
RadioGroup
The root container for radio group items.
| Prop | Type | Default | Description | |--------------|-------------------|---------|--------------------------------| | defaultValue | string | - | Default selected value | | value | string | - | Controlled selected value | | onValueChange| function | - | Called when value changes | | name | string | - | Form name attribute | | disabled | boolean | false | Disables all radio items | | orientation | "horizontal" | "vertical" | "vertical" | Layout orientation | | className | string | - | Additional CSS classes |
RadioGroupItem
A single radio item within the group.
| Prop | Type | Default | Description | |-----------|-------------------|---------|--------------------------------| | value | string | - | The value for this radio item | | disabled | boolean | false | Disables this radio item | | id | string | - | HTML id attribute | | className | string | - | Additional CSS classes |
Examples
Basic Radio Group
<RadioGroup defaultValue="option-one">
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>Controlled Radio Group
function ControlledRadioGroup() {
const [value, setValue] = useState("option-one")
return (
<RadioGroup value={value} onValueChange={setValue}>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>
)
}Disabled Radio Group
<RadioGroup disabled defaultValue="option-one">
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>Horizontal Layout
<RadioGroup orientation="horizontal" defaultValue="option-one" className="flex space-x-4">
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>Accessibility
- Full keyboard navigation support
- Screen reader accessible with proper ARIA attributes
- Focus management between radio items
- Arrow key navigation within groups
- Space key to select items
- Proper labeling support
Styling
The Radio Group components use DarkSnow UI design tokens:
- Container: Grid layout with configurable gap
- Items: Circular border with accent colors
- Indicator: Filled circle when selected
- Focus: Ring outline on focus
- Disabled: Reduced opacity and disabled cursor
Best Practices
- Always use labels: Associate each radio item with a descriptive label
- Logical grouping: Group related options together
- Default selection: Consider providing a sensible default value
- Clear options: Make option text clear and distinct
- Appropriate sizing: Use consistent spacing between options
