Components

Radio

A radio button that is selected exclusively within a radio group.

Best apple

Usage guidelines#

  • Place radios in a Radio Group. The group owns the selected value, the form name, and arrow-key navigation between its radios.
  • Form controls must have an accessible name. It can be created using <label> elements, or the Field and Fieldset components. See Labeling a radio.

Anatomy#

Import the component and assemble its parts inside a radio group:

Anatomytsx
import { Radio } from '@egoist/solid-base/radio';
import { RadioGroup } from '@egoist/solid-base/radio-group';

<RadioGroup>
  <Radio.Root>
    <Radio.Indicator />
  </Radio.Root>
</RadioGroup>;

Examples#

Labeling a radio#

An enclosing <label> is the simplest labeling pattern:

Using an enclosing label to label a radio buttontsx
<label>
  <Radio.Root value="ssd">
    <Radio.Indicator />
  </Radio.Root>
  SSD
</label>

Rendering as a native button#

By default, <Radio.Root> renders a <span> element to support enclosing labels. Prefer rendering each radio as a native button when using sibling labels (for/id):

Storage type

Native buttons with wrapping labels are supported by using the render callback to avoid invalid HTML, so the hidden input is placed outside the label:

Render callbacktsx
<Radio.Root
  value="ssd"
  nativeButton
  render={(buttonProps) => (
    <label>
      <button {...buttonProps} />
      SSD
    </label>
  )}
/>

Animating the indicator#

<Radio.Indicator> is only mounted while its radio is checked. It receives [data-starting-style] and [data-ending-style] while it enters and leaves, so it can be animated with CSS transitions. Pass keepMounted to keep it in the DOM instead:

.Indicator {
  transition: scale 150ms;

  &[data-starting-style],
  &[data-ending-style] {
    scale: 0;
  }
}

API reference#

<Radio.Root>

Represents the radio button itself. Renders a <span> element and a hidden <input> beside.

PropTypeDefault
idstring—

The id of the hidden input element.

When nativeButton is true, the id is applied to the root element.

value*Value—

The unique identifying value of the radio in a group.

disabledboolean—

Whether the component should ignore user interaction.

requiredboolean—

Whether the user must choose a value before submitting a form.

readOnlyboolean—

Whether the user should be unable to select the radio button.

inputRefRefProp<HTMLInputElement>—

A ref to access the hidden input element.

nativeButtonbooleanfalse

Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-checked

Present when the radio is checked.

data-unchecked

Present when the radio is not checked.

data-disabled

Present when the radio is disabled.

data-readonly

Present when the radio is readonly.

data-required

Present when the radio is required.

data-valid

Present when the radio is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the radio is in an invalid state (when wrapped in Field.Root).

data-touched

Present when the radio has been touched (when wrapped in Field.Root).

data-dirty

Present when the radio's value has changed (when wrapped in Field.Root).

data-filled

Present when the radio is checked (when wrapped in Field.Root).

data-focused

Present when the radio is focused (when wrapped in Field.Root).

<Radio.Indicator>

Indicates whether the radio button is selected. Renders a <span> element.

PropTypeDefault
keepMountedbooleanfalse

Whether to keep the HTML element in the DOM when the radio button is inactive.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-checked

Present when the radio is checked.

data-unchecked

Present when the radio is not checked.

data-disabled

Present when the radio is disabled.

data-readonly

Present when the radio is readonly.

data-required

Present when the radio is required.

data-valid

Present when the radio is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the radio is in an invalid state (when wrapped in Field.Root).

data-touched

Present when the radio has been touched (when wrapped in Field.Root).

data-dirty

Present when the radio's value has changed (when wrapped in Field.Root).

data-filled

Present when the radio is checked (when wrapped in Field.Root).

data-focused

Present when the radio is focused (when wrapped in Field.Root).