1.0.0 • Published 5 months ago

@darksnow-ui/radio-group v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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-group

Peer Dependencies

npm install react react-dom

Usage

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.

PropTypeDefaultDescription
defaultValuestring-Default selected value
valuestring-Controlled selected value
onValueChangefunction-Called when value changes
namestring-Form name attribute
disabledbooleanfalseDisables all radio items
orientation"horizontal" | "vertical""vertical"Layout orientation
classNamestring-Additional CSS classes

RadioGroupItem

A single radio item within the group.

PropTypeDefaultDescription
valuestring-The value for this radio item
disabledbooleanfalseDisables this radio item
idstring-HTML id attribute
classNamestring-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

  1. Always use labels: Associate each radio item with a descriptive label
  2. Logical grouping: Group related options together
  3. Default selection: Consider providing a sensible default value
  4. Clear options: Make option text clear and distinct
  5. Appropriate sizing: Use consistent spacing between options

Related Components

  • Checkbox - For multiple selections
  • Select - For dropdown selections
  • Label - For labeling form controls