// fork of radix // https://github.com/radix-ui/primitives/tree/main/packages/react/checkbox/src/Checkbox.tsx import type { CheckedState, CheckboxExtraProps as HeadlessCheckboxExtraProps, } from '@tamagui/checkbox-headless' import { isIndeterminate, useCheckbox } from '@tamagui/checkbox-headless' import type { GetProps, NativeValue, StylePiece } from '@tamagui/core' import { createStyledHOC, isWeb, shouldRenderNativePlatform, styled, View, withStaticProperties, } from '@tamagui/core' import { registerFocusable } from '@tamagui/focusable' import { useControllableState } from '@tamagui/use-controllable-state' import React, { useMemo } from 'react' import { CheckboxStyledContext } from './CheckboxStyledContext' const INDICATOR_NAME = 'CheckboxIndicator' export const CheckboxIndicatorFrame = styled(View, { // use Checkbox for easier themes displayName: INDICATOR_NAME, context: CheckboxStyledContext, }) const CHECKBOX_NAME = 'Checkbox' export const CheckboxFrame = styled(View, { displayName: CHECKBOX_NAME, render: 'button', context: CheckboxStyledContext, alignItems: 'center', justifyContent: 'center', variants: { disabled: { true: { pointerEvents: 'none', userSelect: 'none', }, }, } as const, }) type CheckboxExtraProps = HeadlessCheckboxExtraProps & { native?: NativeValue<'web'> } type CheckboxFrameActiveStyleProps = { activeStyle?: StylePiece activeTheme?: string | null } type CheckboxIndicatorActiveStyleProps = { activeStyle?: StylePiece } export type CheckboxProps = GetProps & CheckboxExtraProps & CheckboxFrameActiveStyleProps type CheckboxIndicatorExtraProps = { /** * Used to force mounting when more control is needed. Useful when * controlling animation with React animation libraries. */ forceMount?: boolean } export type CheckboxIndicatorProps = GetProps & CheckboxIndicatorExtraProps & CheckboxIndicatorActiveStyleProps export const CheckboxContext = React.createContext<{ checked: CheckedState disabled?: boolean }>({ checked: false, disabled: false, }) const CheckboxComponent = createStyledHOC( CheckboxFrame, function Checkbox( extraProps: CheckboxExtraProps & CheckboxFrameActiveStyleProps, forwardedRef ) { const propsIn: CheckboxProps = extraProps const { checked: checkedProp, defaultChecked, onCheckedChange, native, activeStyle, activeTheme, ...props } = propsIn const [checked = false, setChecked] = useControllableState({ prop: checkedProp, defaultProp: defaultChecked!, onChange: onCheckedChange, }) const { checkboxProps, checkboxRef, bubbleInput } = useCheckbox( props, [checked, setChecked], forwardedRef ) if (process.env.TAMAGUI_TARGET === 'native') { React.useEffect(() => { if (!props.id) return if (props.disabled) return return registerFocusable(props.id, { focusAndSelect: () => { setChecked?.((value) => !value) }, focus: () => {}, }) }, [props.id, props.disabled]) } const renderNative = shouldRenderNativePlatform(native) if (renderNative === 'web') { return ( ) } const memoizedContext = useMemo( () => ({ checked, disabled: checkboxProps.disabled, }), [checked, checkboxProps.disabled] ) const isActive = !!checked const disabled = checkboxProps.disabled return ( {props.children} {bubbleInput} ) } ) const CheckboxIndicator = createStyledHOC( CheckboxIndicatorFrame, ( propsIn: CheckboxIndicatorExtraProps & CheckboxIndicatorActiveStyleProps, forwardedRef ) => { const props: CheckboxIndicatorProps = propsIn const { children, forceMount, activeStyle, ...indicatorProps } = props const { active } = CheckboxStyledContext.useStyledContext() const context = React.useContext(CheckboxContext) if (forceMount || isIndeterminate(context.checked) || context.checked === true) { return ( {children} ) } return null } ) export const Checkbox = withStaticProperties(CheckboxComponent, { Indicator: CheckboxIndicator, })