Components
Radio button group

Radio Button Group

Allows single selection from multiple options.

Usage

import * as RadioButtonGroup from '~/components/ui/radio-button-group'

export const Demo = (props: RadioButtonGroup.RootProps) => {
  const options = [{ value: 'S' }, { value: 'M' }, { value: 'L', disabled: true }, { value: 'XL' }]

  return (
    <RadioButtonGroup.Root defaultValue="M" {...props}>
      {options.map((option, id) => (
        <RadioButtonGroup.Item key={id} value={option.value} disabled={option.disabled} px="0">
          <RadioButtonGroup.ItemControl />
          <RadioButtonGroup.ItemText>{option.value}</RadioButtonGroup.ItemText>
        </RadioButtonGroup.Item>
      ))}
    </RadioButtonGroup.Root>
  )
}

Installation

1

Add Component

Insert code snippet into your project. Update import paths as needed.

import { RadioGroup as ArkRadioButtonGroup } from '@ark-ui/react/radio-group'
import type { ComponentProps } from 'react'
import { styled } from 'styled-system/jsx'
import { radioButtonGroup } from 'styled-system/recipes'
import { createStyleContext } from '~/lib/create-style-context'

const { withProvider, withContext } = createStyleContext(radioButtonGroup)

export const Root = withProvider(styled(ArkRadioButtonGroup.Root), 'root')
export const Indicator = withContext(styled(ArkRadioButtonGroup.Indicator), 'indicator')
export const Item = withContext(styled(ArkRadioButtonGroup.Item), 'item')
export const ItemControl = withContext(styled(ArkRadioButtonGroup.ItemControl), 'itemControl')
export const ItemText = withContext(styled(ArkRadioButtonGroup.ItemText), 'itemText')
export const Label = withContext(styled(ArkRadioButtonGroup.Label), 'label')

export interface RootProps extends ComponentProps<typeof Root> {}
export interface IndicatorProps extends ComponentProps<typeof Indicator> {}
export interface ItemProps extends ComponentProps<typeof Item> {}
export interface ItemControlProps extends ComponentProps<typeof ItemControl> {}
export interface ItemTextProps extends ComponentProps<typeof ItemText> {}
export interface LabelProps extends ComponentProps<typeof Label> {}
2

Add Recipe

This step is necessary only if you do not use any of the Park UI plugins.

import { radioGroupAnatomy } from '@ark-ui/anatomy'
import { defineSlotRecipe } from '@pandacss/dev'

export const radioButtonGroup = defineSlotRecipe({
  className: 'radioButtonGroup',
  slots: radioGroupAnatomy.keys(),
  base: {
    root: {
      colorPalette: 'accent',
      display: 'flex',
      flexWrap: 'wrap',
    },
    item: {
      alignItems: 'center',
      appearance: 'none',
      borderColor: 'border.default',
      borderRadius: 'l2',
      borderWidth: '1px',
      color: 'fg.default',

      cursor: 'pointer',
      display: 'inline-flex',
      fontWeight: 'semibold',
      justifyContent: 'center',
      outline: 'none',
      position: 'relative',
      transitionDuration: 'normal',
      transitionProperty: 'background, border-color, color, box-shadow',
      transitionTimingFunction: 'default',
      userSelect: 'none',
      verticalAlign: 'middle',
      whiteSpace: 'nowrap',
      _hover: {
        background: 'gray.a2',
      },
      _checked: {
        cursor: 'default',
      },
      _disabled: {
        borderColor: 'border.disabled',
        color: 'fg.disabled',
        cursor: 'not-allowed',
        _hover: {
          background: 'initial',
          borderColor: 'border.disabled',
          color: 'fg.disabled',
        },
      },
    },
    itemText: {
      display: 'inline-flex',
      alignItems: 'center',
    },
  },
  defaultVariants: {
    size: 'md',
    variant: 'solid',
  },
  variants: {
    variant: {
      solid: {
        item: {
          _checked: {
            background: 'colorPalette.default',
            borderColor: 'colorPalette.default',
            color: 'colorPalette.fg',
            _hover: {
              color: 'colorPalette.fg',
              background: 'colorPalette.default',
            },
          },
        },
      },
      outline: {
        item: {
          _checked: {
            borderColor: 'colorPalette.default',
            boxShadow: '0 0 0 1px var(--colors-color-palette-default)',
            _hover: {
              background: 'initial',
            },
          },
        },
      },
    },
    size: {
      sm: {
        root: {
          gap: '2',
        },
        item: {
          h: '9',
          minW: '9',
          textStyle: 'sm',
          px: '3.5',
          '& svg': {
            width: '4.5',
            height: '4.5',
          },
        },
        itemText: {
          gap: '2',
        },
      },
      md: {
        root: {
          gap: '3',
        },
        item: {
          h: '10',
          minW: '10',
          textStyle: 'sm',
          px: '4',
          '& svg': {
            width: '5',
            height: '5',
          },
        },
        itemText: {
          gap: '2',
        },
      },
      lg: {
        root: {
          gap: '3',
        },
        item: {
          h: '11',
          minW: '11',
          textStyle: 'md',
          px: '4.5',
          '& svg': {
            width: '5',
            height: '5',
          },
        },
        itemText: {
          gap: '2',
        },
      },
      xl: {
        root: {
          gap: '3',
        },
        item: {
          h: '12',
          minW: '12',
          textStyle: 'md',
          px: '5',
          '& svg': {
            width: '5',
            height: '5',
          },
        },
        itemText: {
          gap: '2.5',
        },
      },
    },
  },
})

On this page