'use client' /** * A button that stays down. * * One piece of state and one ARIA attribute: `aria-pressed` tells a screen * reader this is a toggle rather than an action, and the styling reads the same * value, so the two cannot disagree. * * Controlled through `pressed`, or uncontrolled through `defaultPressed` with * `onPressedChange` to hear about it. */ import { XStack } from '@hanzo/gui' import { useState, type ComponentProps, type ReactNode } from 'react' export type ToggleVariant = 'default' | 'outline' export type ToggleSize = 'sm' | 'default' | 'lg' /** Height, inline padding and the floor that keeps a lone icon square. */ const SIZE: Record = { sm: { height: 36, px: 10, minWidth: 36 }, default: { height: 40, px: 12, minWidth: 40 }, lg: { height: 44, px: 20, minWidth: 44 }, } export type ToggleProps = Omit, 'onPress'> & { readonly pressed?: boolean readonly defaultPressed?: boolean readonly onPressedChange?: (pressed: boolean) => void readonly disabled?: boolean readonly variant?: ToggleVariant readonly size?: ToggleSize readonly children?: ReactNode } export function Toggle({ pressed, defaultPressed = false, onPressedChange, disabled, variant = 'default', size = 'default', children, ...rest }: ToggleProps) { const [own, setOwn] = useState(defaultPressed) const on = pressed ?? own const dim = SIZE[size] return ( { const next = !on if (pressed === undefined) setOwn(next) onPressedChange?.(next) }} {...rest} > {children} ) } export default Toggle