Radio

Lets the user pick one option from a short list.

Usage

Code
import { RadioGroup } from '@leverui/components';
Code
<RadioGroup.Root>
  <RadioGroup.Item>
    <RadioGroup.ItemHiddenInput />
    <RadioGroup.ItemIndicator />
    <RadioGroup.ItemText />
  </RadioGroup.Item>
</RadioGroup.Root>

Want one component that takes props? See the snippet below.

Examples

Controlled

Pass the value and onValueChange props to the RadioGroup.Root component to control the selected radio button.

Colors

Pass the colorPalette prop to the RadioGroup.Root component to change the color scheme of the component.

gray

red

green

blue

teal

pink

purple

cyan

orange

yellow

Sizes

Pass the size prop to the RadioGroup.Root component to change the size of the radio component.

Variants

Pass the variant prop to the RadioGroup.Root component to change the appearance of the radio component.

Disabled

Pass the disabled prop to the RadioGroup.Item component to make the radio disabled.

Hook Form

Use a Solid signal to control the radio group within a form.

Delivery method

Closed Component

Wraps the parts of one radio item in one component.

Code
import { Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { RadioGroup as LeveruiRadioGroup, type RadioGroupItemProps } from '@leverui/components';

export type RadioProps = RadioGroupItemProps & {
  inputProps?: JSX.InputHTMLAttributes<HTMLInputElement>;
};

export function Radio(props: RadioProps) {
  const item = omit(props, 'children', 'inputProps');
  return (
    <LeveruiRadioGroup.Item {...item}>
      <LeveruiRadioGroup.ItemHiddenInput {...props.inputProps} />
      <LeveruiRadioGroup.ItemIndicator />
      <Show when={props.children}>
        <LeveruiRadioGroup.ItemText>{props.children}</LeveruiRadioGroup.ItemText>
      </Show>
    </LeveruiRadioGroup.Item>
  );
}

export const RadioGroup = LeveruiRadioGroup.Root;
Code
<RadioGroup>
  <Radio />
</RadioGroup>

Guides

Forms and state

Set name on the root to send the selected value with a form. required makes people choose an option before the browser submits. disabled on the root or an item blocks selection and leaves the input out of FormData. readOnly keeps the selection but disables the native inputs, so the value is also left out of FormData. To submit it, add a hidden input with the same name and value. invalid marks the group invalid for assistive technology and CSS.

Every item needs an ItemHiddenInput and readable text, in ItemText or a native label. RootProvider takes the accessor from your own useRadioGroup call. PropsProvider sets default props, which can be reactive. Context and ItemContext give you the current state. Use ItemControl for the visible control when ItemIndicator does not give the look you want.

Orientation

orientation sets the ARIA orientation only. Lay out the items in your own CSS. Arrow keys work in either orientation.

Props

Root

PropDefaultTypeDescription
colorPalettegraystringColor palette, built-in or your own.
variantsolid'outline' | 'subtle' | 'solid'Radio style. Responsive.
sizemd'xs' | 'sm' | 'md' | 'lg'Radio size. Responsive.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the root's props.
unstyledfalsebooleanTurn off the Lever UI styles.
defaultValue—stringSelected value on first render, when uncontrolled.
disabled—booleanDisable the radio group.
form—stringID of the form the inputs belong to.
id—stringUnique ID for the radio group.
ids—Element ID mapYour own IDs for the elements, replacing the generated ones.
invalid—booleanMark the radio group invalid.
name—stringInput name submitted with the form.
onValueChange—(details) => voidCalled when a radio is checked.
orientation—'horizontal' | 'vertical'Group orientation.
readOnly—booleanMake the radio group read-only.
required—booleanMark the radio group required.
value—stringSelected value, when controlled.

Customize with CSS

Load your stylesheet after @leverui/components/styles.css.

Code
.my-radio-group {
  display: flex;
  gap: 1rem;
  --leverui-radio-group-item-gap: 0.75rem;
}

.leverui-radio-group__itemControl {
  border-radius: 0.5rem;
}

The root and every visible part have a .leverui-radio-group__* class. The control also has .leverui-radiomark and shares its state CSS. class and style work on the root and every part. unstyled turns off the default styles and keeps the part hooks.