Checkbox

Whether you're building a simple form or a complex data collection system, the Checkbox component offers a user-friendly way for users to select multiple options from a list.
Installation
API Reference
Spec Doc
This is an illustration of Checkbox component.
size
isInvalid
isDisabled
<Checkbox size="md" isInvalid={false} isDisabled={false}>
<CheckboxIndicator>
<CheckboxIcon as={CheckIcon} />
</CheckboxIndicator>
<CheckboxLabel>Label</CheckboxLabel>
</Checkbox>

Installation

Step 1: Install the following dependencies:

npm i @gluestack-ui/checkbox

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

import React from "react"
import { createCheckbox } from "@gluestack-ui/checkbox"
import { View, Pressable, Text } from "react-native"
import {
cn,
withStates,
withStyleContextAndStates,
useStyleContext,
tva,
withStyleContext,
} from "@gluestack-ui/nativewind-utils"
import { Platform } from "react-native"
import { Check } from "lucide-react-native"
import { cssInterop } from "nativewind"
const UICheckbox = createCheckbox({
// @ts-ignore
Root:
Platform.OS === "web"
? withStyleContext(View)
: withStyleContextAndStates(Pressable),
Group: Platform.OS === "web" ? View : withStates(View),
Icon: Platform.OS === "web" ? Check : withStates(Check),
Label: Platform.OS === "web" ? Text : withStates(Text),
Indicator: Platform.OS === "web" ? View : withStates(View),
})
cssInterop(UICheckbox, { className: "style" })
cssInterop(UICheckbox.Group, { className: "style" })
cssInterop(UICheckbox.Icon, { className: "style" })
cssInterop(UICheckbox.Label, { className: "style" })
cssInterop(UICheckbox.Indicator, { className: "style" })
const checkboxIndicator = tva({
base: "justify-center items-center border-outline-400 rounded-sm data-[focus=true]:outline-none data-[focus-visible=true]:ring-2 data-[focus-visible=true]:ring-primary-700 data-[focus-visible=true]:ring-offset-1 overflow-hidden data-[checked=true]:border-primary-600",
parentVariants: {
size: {
lg: "w-6 h-6 border-4",
md: "w-5 h-5 border-2",
sm: "w-4 h-4 border-2",
},
},
})
const checkboxLabel = tva({
base: "text-typography-600",
parentVariants: {
size: {
lg: "text-lg",
md: "text-md",
sm: "text-sm",
},
},
})
const CheckboxGroup = UICheckbox.Group
const Checkbox = React.forwardRef(
({ className, size = "sm", ...props }: any, ref) => {
return (
<UICheckbox
className={cn("flex-row items-center justify-start gap-2", className)}
context={{
size,
}}
ref={ref}
/>
)
}
)
const CheckboxIndicator = React.forwardRef(
({ className, ...props }: any, ref) => {
const { size: parentSize } = useStyleContext()
return (
<UICheckbox.Indicator
className={checkboxIndicator({
parentVariants: {
size: parentSize,
},
class: className,
})}
{...props}
ref={ref}
/>
)
}
)
const CheckboxLabel = React.forwardRef(({ className, ...props }: any, ref) => {
const { size: parentSize } = useStyleContext()
return (
<UICheckbox.Label
className={checkboxLabel({
parentVariants: {
size: parentSize,
},
class: className,
})}
{...props}
ref={ref}
/>
)
})
const CheckboxIcon = React.forwardRef(({ className, ...props }: any, ref) => {
return (
<UICheckbox.Icon
className={cn(
"w-full h-full bg-primary-600 stroke-typography-0 color-typography-0",
className
)}
{...props}
ref={ref}
/>
)
})
// Assign display names
Checkbox.displayName = "Checkbox"
CheckboxIndicator.displayName = "CheckboxIndicator"
CheckboxLabel.displayName = "CheckboxLabel"
CheckboxIcon.displayName = "CheckboxIcon"
export {
Checkbox,
CheckboxIndicator,
CheckboxLabel,
CheckboxIcon,
CheckboxGroup,
}

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 { Checkbox } from "@/components/ui/Checkbox"
export default () => (
<CheckboxGroup>
<Checkbox>
<CheckboxIndicator>
<CheckboxIcon />
</CheckboxIndicator>
<CheckboxLabel />
</Checkbox>
</CheckboxGroup>
)

Component Props

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

Checkbox

Contains all Checkbox 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 checkbox input. This is the value that will be returned on form submission.
onChange
(value: boolean) => void
-
Function called when the state of the checkbox changes.
defaultIsChecked
bool
false
If true, the checkbox will be initially checked.
isChecked
bool
false
When true, the checkbox will be checked. You'll need to pass onChange to update it's value (since it's now controlled).
isDisabled
bool
false
To manually set disable to the checkbox.
isInvalid
bool
false
To manually set invalid to the checkbox.
isReadOnly
bool
false
To manually set read-only to the checkbox.
isHovered
bool
false
To manually set hover to the checkbox.
isFocusVisible
bool
false
To manually set focus visible state to the checkbox.
isIndeterminate
bool
false
To manually set indeterminate to the checkbox.

CheckboxIndicator

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

CheckboxIcon

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

CheckboxLabel

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

CheckboxGroup

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 checkbox group.
onChange
(values: Array<string>) => void
-
The callback fired when any children Checkbox is checked or unchecked.
isDisabled
bool
false
To manually set disable to the checkbox.
isInvalid
bool
false
To manually set invalid to the checkbox.
isReadOnly
bool
false
To manually set read-only to the checkbox.

Features

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

Accessibility

We have outlined the various features that ensure the Checkbox 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 checkbox.

Screen Reader

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

Focus Management

  • The onFocus and onBlur props to 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

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