Checkbox
A control that allows the user to toggle between checked and not checked.
Installation
bash npx @moe/cli add checkbox Install the following dependency:
npx expo install @rn-primitives/checkboxCopy/paste the following code to @/components/ui/checkbox.tsx
import * as CheckboxPrimitive from "@rn-primitives/checkbox";
import * as React from "react";
import { Platform } from "react-native";
import { Check } from "@/lib/icons/Check";
import { cn } from "@/lib/utils";
const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
>(({ className, ...props }, ref) => {
return (
<CheckboxPrimitive.Root
ref={ref}
className={cn(
"web:peer native:h-[20px] native:w-[20px] native:rounded h-4 w-4 shrink-0 rounded-sm border border-primary web:ring-offset-background web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring web:focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
props.checked && "bg-primary",
className,
)}
{...props}
>
<CheckboxPrimitive.Indicator
className={cn("h-full w-full items-center justify-center")}
>
<Check
size={12}
strokeWidth={Platform.OS === "web" ? 2.5 : 3.5}
className="text-primary-foreground"
/>
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
);
});
Checkbox.displayName = CheckboxPrimitive.Root.displayName;
export { Checkbox };Usage
import { Checkbox } from "@moe/registry/ui/checkbox";
import { Label } from "@moe/registry/ui/label";
import { View } from "react-native";
export function CheckboxDemo() {
return (
<View className="flex-row items-center gap-2">
<Checkbox id="terms" />
<Label htmlFor="terms">Accept terms and conditions</Label>
</View>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | false | The controlled checked state |
onCheckedChange | (checked: boolean) => void | - | Event handler called when the checked state changes |
disabled | boolean | false | When true, prevents the user from interacting |