code/tamagui.dev/data/docs/components/checkbox/1.85.0.mdx
<Description>Toggle state in forms.</Description>
<YStack className="is-sticky" /> <Tabs id="type" defaultValue="styled"> <Tabs.List> <TooltipSimple label="With Tamagui's default styles"> <Tabs.Tab value="styled">Styled</Tabs.Tab> </TooltipSimple> <TooltipSimple label="No default styles, easy to customize"> <Tabs.Tab value="unstyled">Unstyled</Tabs.Tab> </TooltipSimple> <TooltipSimple label="No dependency on Tamagui's core"> <Tabs.Tab value="headless" label="No styles and no dependency on Tamagui's styling">Headless</Tabs.Tab> </TooltipSimple> </Tabs.List> <HeroContainer showAnimationDriverControl> <Tabs.Content value="styled" justifyContent="center" alignItems="center" width="100%"> <CheckboxDemo /> </Tabs.Content> <Tabs.Content value="unstyled" justifyContent="center" alignItems="center" width="100%"> <CheckboxUnstyledDemo /> </Tabs.Content> <Tabs.Content value="headless" justifyContent="center" alignItems="center" width="100%"> <CheckboxHeadlessDemo /> </Tabs.Content> </HeroContainer><Tabs.Content value="styled">
</Tabs.Content> <Tabs.Content value="unstyled">
</Tabs.Content> <Tabs.Content value="headless">
</Tabs.Content>
<Highlights
features={[
Supports indeterminate state.,
Accessible, easy to compose and customize.,
Sizable & works controlled or uncontrolled.,
Ability to opt-out to native checkbox on web.,
]}
/>
<Tabs.Content value="styled">
Checkbox is already installed in tamagui, or you can install it independently:
npm install @tamagui/checkbox
</Tabs.Content>
<Tabs.Content value="unstyled">
Checkbox is already installed in tamagui, or you can install it independently:
npm install @tamagui/checkbox
</Tabs.Content>
<Tabs.Content value="headless">
To use the headless switch, you want to import it from the
@tamagui/switch-headless package. This package has no dependency on
@tamagui/core, but still works off the react-native APIs.
This means can bring your own style library.
npm install @tamagui/switch-headless
</Tabs.Content>
<Tabs.Content value="styled">
import { Check } from '@tamagui/lucide-icons-2'
import { Checkbox } from 'tamagui'
export default () => (
<Checkbox size="$4">
<Checkbox.Indicator>
<Check />
</Checkbox.Indicator>
</Checkbox>
)
</Tabs.Content>
<Tabs.Content value="unstyled">
Use the createCheckbox export to create a fully custom checkbox that still
uses the Tamagui styling system. This is similar to setting unstyled, but goes
a bit further. It doesn't add any types for size or unstyled, and it won't
automatically apply the active theme. If does pass the checked prop down as
indicated in the types of createCheckbox.
</Tabs.Content>
<Tabs.Content value="headless">
Using the useCheckbox API, you can make your own Checkbox from scratch.
</Tabs.Content>
Checkbox extend ThemeableStack inheriting all the
props, plus:
<PropsTable
data={[
{
name: 'labeledBy',
type: 'string',
description: Set aria-labeled-by.,
},
{
name: 'name',
type: 'string',
description: Equivalent to input name.,
},
{
name: 'value',
type: 'string',
description: Give it a value (for use in HTML forms).,
},
{
name: 'checked',
type: 'boolean',
description: Control the input.,
},
{
name: 'defaultChecked',
type: 'boolean',
description: Uncontrolled default value.,
},
{
name: 'required',
type: 'boolean',
description: Sets aria-required.,
},
{
name: 'native',
type: 'boolean',
description: Renders native checkbox input on web.,
default: false,
},
{
name: 'onCheckedChange',
type: '(checked: boolean | "indeterminate") => void',
description: 'Callback that fires when the checkbox state is changed.',
},
{
name: 'sizeAdjust',
type: 'number',
description: Adjust the checkbox size scaling by this number.,
},
{
name: 'scaleIcon',
type: 'number',
description: Scale the indicator icon more than usual by this number.,
},
{
name: 'scaleSize',
type: 'number',
default: '0.5',
description: The Tamagui size tokens should map to the height of a button at any given step. This means you want somewhat smaller checkboxes typically.,
},
{
name: 'unstyled',
required: false,
type: boolean,
description: Removes all default Tamagui styles.,
},
]}
/>
Checkbox.Indicator extend ThemeableStack inheriting all the
props, plus:
<PropsTable
data={[
{
name: 'forceMount',
required: false,
type: boolean,
description: Used to force mounting when more control is needed.,
},
{
name: 'disablePassStyles',
required: false,
type: 'boolean',
description: Used to disable passing styles down to children.,
},
]}
/>