import * as CheckboxPrimitive from "@radix-ui/react-checkbox"; import { cva, VariantProps } from "class-variance-authority"; import React from "react"; import { CheckIcon, DashIcon } from "@sparkle/icons/app"; import { cn } from "@sparkle/lib/utils"; import { Icon } from "./Icon"; import { Label } from "./Label"; import { Tooltip } from "./Tooltip"; export const CHECKBOX_SIZES = ["xs", "sm"] as const; export type CheckboxSizeType = (typeof CHECKBOX_SIZES)[number]; const checkboxStyles = cva( cn( "s-shrink-0 s-peer s-border s-transition s-duration-200 s-ease-in-out", "s-border-border-dark dark:s-border-border-dark-night s-bg-background dark:s-bg-background-night", "s-text-foreground dark:s-text-foreground-night", "focus-visible:s-ring-ring s-ring-offset-background focus-visible:s-outline-none focus-visible:s-ring-2 focus-visible:s-ring-offset-2", "hover:s-border-highlight hover:s-bg-highlight-50 dark:hover:s-bg-highlight-100-night hover:dark:s-border-highlight", "disabled:s-cursor-not-allowed disabled:s-opacity-50 disabled:s-border-border-dark disabled:dark:s-border-border-dark-night disabled:s-bg-background dark:disabled:s-bg-background-night" ), { variants: { checked: { true: "data-[state=checked]:s-bg-primary dark:data-[state=checked]:s-bg-primary-night data-[state=checked]:s-text-white data-[state=checked]:s-border-primary", partial: "data-[state=indeterminate]:s-bg-primary dark:data-[state=indeterminate]:s-bg-primary-night data-[state=indeterminate]:s-text-white data-[state=indeterminate]:s-border-primary", false: "", }, size: { xs: "s-h-4 s-w-4 s-rounded", sm: "s-h-5 s-w-5 s-rounded-md", }, }, defaultVariants: { size: "sm", checked: false, }, } ); type CheckBoxStateType = boolean | "partial"; interface CheckboxProps extends Omit< React.ComponentPropsWithoutRef, "checked" | "defaultChecked" >, VariantProps { checked?: CheckBoxStateType; tooltip?: string; } const Checkbox = React.forwardRef< React.ElementRef, CheckboxProps >(({ className, size, checked, id, tooltip, ...props }, ref) => { const checkbox = ( ); return tooltip ? ( ) : ( checkbox ); }); Checkbox.displayName = CheckboxPrimitive.Root.displayName; interface CheckboxWithTextProps extends CheckboxProps { text: string; } function CheckboxWithText({ text, tooltip, id, ...props }: CheckboxWithTextProps) { const content = (
); return tooltip ? : content; } interface CheckboxWithTextAndDescriptionProps extends CheckboxWithTextProps { description: string; } function CheckBoxWithTextAndDescription({ text, description, tooltip, id, ...props }: CheckboxWithTextAndDescriptionProps) { const content = (

{description}

); return tooltip ? : content; } export { Checkbox, CheckboxWithText, CheckBoxWithTextAndDescription }; export type { CheckboxProps };