Radio

The Radio component presents users with predefined choices and enables them to select only one option. It is commonly used for providing a single-choice selection in forms or surveys.
Installation
API Reference
Spec Doc
This is an illustration of Radio component.
size
isInvalid
isDisabled
<RadioGroup>
<Radio value="change" size="md" isInvalid={false} isDisabled={false}>
<RadioIndicator>
<RadioIcon as={CircleIcon} strokeWidth={1} />
</RadioIndicator>
<RadioLabel>Label</RadioLabel>
</Radio>
</RadioGroup>

Installation

Step 1: Install the following dependencies:

npm i @gluestack-ui/radio

Step 2: Copy and paste the following code into your project.

import React from "react"
import { createRadio } from "@gluestack-ui/radio"
import { Pressable, View, Platform, Text } from "react-native"
import { Circle } from "lucide-react-native"
import {
tva,
withStyleContextAndStates,
withStyleContext,
useStyleContext,
withStates,
} from "@gluestack-ui/nativewind-utils"
import { cssInterop } from "nativewind"
const radioStyle = tva({
base: "flex-row justify-start items-center web:cursor-pointer data-[disabled=true]:web:cursor-not-allowed gap-2",
})
const groupStyle = tva({ base: "gap-2" })
const iconStyle = tva({
base: "fill-background-800 stroke-background-800 rounded-full data-[checked=true]:text-primary-600 data-[checked=true]:data-[hover=true]:text-primary-700 data-[checked=true]:data-[hover=true]:data-[disabled=true]:text-primary-600",
variants: {
size: {
"2xs": "h-3 w-3",
xs: "h-3.5 w-3.5",
sm: "h-4 w-4",
md: "h-4.5 w-4.5",
lg: "h-5 w-5",
xl: "h-6 w-6",
},
},
})
const indicatorStyle = tva({
base: "justify-center items-center bg-transparent border-outline-400 border-2 rounded-full data-[focus-visible=true]:web:outline-2 data-[focus-visible=true]:web:outline-primary-700 data-[focus-visible=true]:web:outline data-[checked=true]:border-primary-600 data-[checked=true]:bg-transparent data-[hover=true]:border-outline-500 data-[hover=true]:bg-transparent data-[hover=true]:data-[checked=true]:bg-transparent data-[hover=true]:data-[checked=true]:border-primary-700 data-[hover=true]:data-[invalid=true]:border-error-700 data-[hover=true]:data-[disabled=true]:opacity-40 data-[hover=true]:data-[disabled=true]:border-outline-400 data-[hover=true]:data-[disabled=true]:data-[invalid=true]:border-error-400 data-[active=true]:bg-transparent data-[active=true]:border-primary-800 data-[invalid=true]:border-error-700 data-[disabled=true]:opacity-40 data-[disabled=true]:data-[checked=true]:border-outline-400 data-[disabled=true]:data-[checked=true]:bg-transparent data-[disabled=true]:data-[invalid=true]:border-error-400",
parentVariants: {
size: {
sm: "h-4 w-4",
md: "h-5 w-5",
lg: "h-6 w-6",
},
},
})
const labelStyle = tva({
base: "text-typography-600 data-[checked=true]:text-typography-900 data-[hover=true]:text-typography-900 data-[hover=true]:data-[disabled=true]:text-typography-600 data-[hover=true]:data-[disabled=true]:data-[checked=true]:text-typography-900 data-[active=true]:text-typography-900 data-[active=true]:data-[checked=true]:text-typography-900 data-[disabled]:opacity-40 web:select-none",
})
const UIRadio = createRadio({
// @ts-ignore
Root:
Platform.OS === "web"
? withStyleContext(View)
: withStyleContextAndStates(Pressable),
Group: View,
Icon: Platform.OS === "web" ? Circle : withStates(Circle),
Indicator: Platform.OS === "web" ? View : withStates(View),
Label: Platform.OS === "web" ? Text : withStates(Text),
})
cssInterop(UIRadio, { className: "style" })
cssInterop(UIRadio.Group, { className: "style" })
cssInterop(UIRadio.Icon, { className: "style" })
cssInterop(UIRadio.Indicator, { className: "style" })
cssInterop(UIRadio.Label, { className: "style" })
const Radio = ({ className, size = "md", ...props }: any) => {
return (
<UIRadio
className={radioStyle({ size, class: className })}
context={{ size }}
/>
)
}
const RadioGroup = ({ className, ...props }: any) => {
return (
<UIRadio.Group className={groupStyle({ class: className })} {...props} />
)
}
const RadioIndicator = ({ className, ...props }: any) => {
const { size } = useStyleContext()
return (
<UIRadio.Indicator
className={indicatorStyle({ parentVariants: { size }, class: className })}
{...props}
/>
)
}
const RadioLabel = ({ className, ...props }: any) => {
const { size } = useStyleContext()
return (
<UIRadio.Label
className={labelStyle({ parentVariants: { size }, class: className })}
{...props}
/>
)
}
const RadioIcon = ({ className, ...props }: any) => {
const { size } = useStyleContext()
return (
<UIRadio.Icon
className={iconStyle({ parentVariants: { size }, class: className })}
{...props}
/>
)
}
Radio.displayName = "Radio"
RadioGroup.displayName = "RadioGroup"
RadioIndicator.displayName = "RadioIndicator"
RadioLabel.displayName = "RadioLabel"
RadioIcon.displayName = "RadioIcon"
export { Radio, RadioGroup, RadioIndicator, RadioLabel, RadioIcon }

Step 3: Update the import paths to match your project setup.

API Reference

To use this component in your project, include the following import statement in your file.
import { Radio } from "@gluestack-ui/themed"
export default () => (
<RadioGroup>
<Radio>
<RadioIndicator>
<RadioIcon />
</RadioIndicator>
<RadioLabel />
</Radio>
</RadioGroup>
)

Component Props

This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.

Radio

Contains all Radio related layout style props and actions. It inherits all the properties of React Native's View component.
Prop
Type
Default
Description
value
string
-
The value to be used in the radio input. This is the value that will be returned on form submission.
onChange
function
-
Function called when the state of the radio changes.
isDisabled
bool
false
To manually set disable to the radio.
isInvalid
bool
false
To manually set invalid to the radio.
isHovered
bool
false
To manually set hover to the radio.
isFocusVisible
bool
false
To manually set focus visible state to the radio.
isIndeterminate
bool
false
To manually set indeterminate to the radio.

RadioIndicator

Contains all Indicator related layout style props and actions. It inherits all the properties of React Native's View component.

RadioIcon

Contains all Icon related layout style props and actions. It inherits all the properties of gluestack Style's AsForwarder component.

RadioLabel

Contains all Label related layout style props and actions. It inherits all the properties of React Native's Text component.

RadioGroup

Contains all Group related layout style props and actions. It inherits all the properties of React Native's View component.
Prop
Type
Default
Description
value
string
-
The value of the radio group.
onChange
function
-
The callback fired when any children Radio is checked or unchecked.
isReadOnly
bool
false
To manually set read-only to the radio group.

Features

  • Keyboard support for actions.
  • Support for hover, focus and active states.

Accessibility

We have outlined the various features that ensure the Radio component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards. Adheres to the WAI-ARIA design pattern.

Keyboard

  • Tab: Moves focus to the next focusable element.
  • Shift + Tab: Moves focus to the previous focusable element.
  • Space: To check or uncheck focused radio.

Screen Reader

  • VoiceOver: When the radio is focused, the screen reader will announce it's a radio and it's current state (check or uncheck) and it's label.

Focus Management

  • The onFocus and onBlur props manage focus states and provide visual cues to users. This is especially important for users who rely on keyboard navigation.

States

  • In error state, aria-invalid will be passed to indicate that the radio input has an error, and providing support for an aria-errormessage to describe the error in more detail.
  • In disabled state, aria-hidden will be passed to make radio input not focusable.
  • In required state, aria-required will be passed to indicate that the radio input is required.

Props

Radio component is created using Pressable component from react-native. It extends all the props supported by React Native Pressable.
Edit this page on GitHub