Checkbox
tetra-ui/checkboxFreeComponent
A control that allows users to toggle between checked and unchecked states.
Live preview
live · rendered by the registry
Rendered by tetra-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://tetra-ui.com/r/checkbox.jsonSource
1import { useEffect } from "react";2import { Text, type View } from "react-native";3import Animated, {4 interpolateColor,5 useAnimatedStyle,6 useSharedValue,7 withTiming,8} from "react-native-reanimated";9import { useCSSVariable } from "uniwind";10import { cn } from "../lib/utils";11import { CheckIcon } from "./icons";12import { InputPressable } from "./input";1314// Constants15const ANIMATION_DURATION = 180;1617// Types18export type CheckboxProps = Omit<19 React.ComponentProps<typeof View>,20 "children"21> & {22 checked?: boolean;23 invalid?: boolean;24};2526export type CheckboxInputProps = Omit<27 React.ComponentProps<typeof InputPressable>,28 "children"29> & {30 checked?: boolean;31 invalid?: boolean;32 children: React.ReactNode;33};3435// Components36export const Checkbox = ({37 className,38 checked,39 invalid,40 ...props41}: CheckboxProps) => {42 const primaryColor = useCSSVariable("--color-primary") as string;4344 const checkedSharedValue = useSharedValue(checked ? 1 : 0);4546 useEffect(() => {47 checkedSharedValue.value = withTiming(checked ? 1 : 0, {48 duration: ANIMATION_DURATION,49 });50 }, [checked, checkedSharedValue]);5152 const animatedStyle = useAnimatedStyle(() => {53 const backgroundColor = interpolateColor(54 checkedSharedValue.value,55 [0, 1],56 ["transparent", primaryColor]57 );5859 return {60 backgroundColor,61 };62 });6364 return (65 <Animated.View66 {...props}67 accessibilityState={{ checked }}68 className={cn(69 "size-6 shrink-0 items-center justify-center rounded-lg border border-input shadow-xs dark:bg-input/30",70 checked && "border-primary",71 invalid && "border-destructive",72 className73 )}74 style={animatedStyle}75 >76 {checked ? (77 <CheckIcon className="size-4 text-primary-foreground" />78 ) : null}79 </Animated.View>80 );81};8283export const CheckboxInput = ({84 className,85 checked,86 invalid,87 children,88 ...props89}: CheckboxInputProps) => {90 return (91 <InputPressable {...props} className={cn(className)} invalid={invalid}>92 <Checkbox checked={checked} invalid={invalid} />93 <Text className="text-base text-foreground">{children}</Text>94 </InputPressable>95 );96};
What it pulls in
npm packages
Other registry items
Files it writes
More from tetra-ui
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | tetra-ui | Components | Free | 1 dep | |
| Action Inputaction-input | tetra-ui | Components | Free | no deps | |
| Alertalert | tetra-ui | Components | Free | no deps | |
| Avataravatar | tetra-ui | Components | Free | no deps | |
| Badgebadge | tetra-ui | Components | Free | no deps | |
| Bottom Sheetbottom-sheet | tetra-ui | Components | Free | 4 deps · 7 files | |
| Buttonbutton | tetra-ui | Components | Free | no deps | |
| Cardcard | tetra-ui | Components | Free | no deps |
