UNPKG

@spark-ui/components

Version:

Spark (Leboncoin design system) components.

1 lines 13.8 kB
{"version":3,"sources":["../../src/switch/Switch.tsx","../../src/switch/SwitchInput.tsx","../../src/switch/SwitchInput.styles.ts","../../src/switch/SwitchLabel.styles.ts","../../src/switch/SwitchLabel.tsx"],"sourcesContent":["import { useFormFieldControl } from '@spark-ui/components/form-field'\nimport { cx } from 'class-variance-authority'\nimport { useId } from 'react'\n\nimport { SwitchInput, SwitchInputProps } from './SwitchInput'\nimport { SwitchLabel } from './SwitchLabel'\n\nexport type SwitchProps = SwitchInputProps\n\nconst ID_PREFIX = ':switch'\n\nexport const Switch = ({\n size = 'md',\n children,\n className,\n id,\n disabled,\n reverse = false,\n ref,\n ...rest\n}: SwitchProps) => {\n const field = useFormFieldControl()\n\n const labelID = `${ID_PREFIX}-label-${useId()}`\n const innerID = `${ID_PREFIX}-input-${useId()}`\n const fieldID = field.id || id || innerID\n\n const switchLabel = children && (\n <SwitchLabel disabled={disabled} htmlFor={fieldID} id={labelID}>\n {children}\n </SwitchLabel>\n )\n\n const switchInput = (\n <SwitchInput\n ref={ref}\n size={size}\n id={fieldID}\n disabled={disabled}\n /**\n * If the switch doesn't have any direct label (children) then we should try to\n * get an eventual alternative label from FormField.\n * On last resort, we shouldn't forget to define an aria-label attribute.\n */\n aria-labelledby={children ? labelID : field.labelId}\n {...rest}\n />\n )\n\n const content = reverse ? (\n <>\n {switchLabel}\n {switchInput}\n </>\n ) : (\n <>\n {switchInput}\n {switchLabel}\n </>\n )\n\n return (\n <div\n data-spark-component=\"switch\"\n className={cx('gap-md text-body-1 flex items-center', className)}\n >\n {content}\n </div>\n )\n}\n\nSwitch.displayName = 'Switch'\n","import { useFormFieldControl } from '@spark-ui/components/form-field'\nimport { useCombinedState } from '@spark-ui/hooks/use-combined-state'\nimport { Check } from '@spark-ui/icons/Check'\nimport { Close } from '@spark-ui/icons/Close'\nimport { Switch as RadixSwitch } from 'radix-ui'\nimport { type ComponentPropsWithRef, type ReactNode } from 'react'\n\nimport { Slot } from '../slot'\nimport {\n styles,\n type StylesProps,\n thumbCheckSVGStyles,\n thumbStyles,\n thumbWrapperStyles,\n} from './SwitchInput.styles'\n\nexport interface SwitchInputProps\n extends StylesProps,\n Omit<ComponentPropsWithRef<'button'>, 'value'> {\n /**\n * The state of the switch when it is initially rendered. Use when you do not need to control its state.\n */\n defaultChecked?: boolean\n /**\n * The controlled state of the switch. Must be used in conjunction with `onCheckedChange`.\n */\n checked?: boolean\n /**\n * When true, prevents the user from interacting with the switch.\n */\n /**\n * Event handler called when the state of the switch changes.\n */\n onCheckedChange?: (checked: boolean) => void\n /**\n * When `true`, prevents the user from interacting with the switch.\n */\n disabled?: boolean\n /**\n * When true, indicates that the user must check the switch before the owning form can be submitted.\n */\n required?: boolean\n /**\n * The name of the switch. Submitted with its owning form as part of a name/value pair.\n */\n name?: string\n /**\n * The value given as data when submitted with a name.\n */\n value?: string\n /**\n * Icon shown inside the thumb of the Switch whenever it is checked\n */\n checkedIcon?: ReactNode\n /**\n * Icon shown inside the thumb of the Switch whenever it is unchecked\n */\n uncheckedIcon?: ReactNode\n /**\n * When true, the label will be placed on the left side of the Switch\n */\n reverse?: boolean\n}\n\nexport const SwitchInput = ({\n checked,\n checkedIcon = <Check />,\n defaultChecked,\n intent: intentProp,\n uncheckedIcon = <Close />,\n size = 'md',\n value = 'on',\n onCheckedChange,\n className,\n required,\n ref,\n ...rest\n}: SwitchInputProps) => {\n const [isChecked, setIsChecked] = useCombinedState(checked, defaultChecked)\n const { name, description, state, isRequired, isInvalid } = useFormFieldControl()\n const intent = state ?? intentProp\n\n const handleCheckedChange = (updatedValue: boolean): void => {\n setIsChecked(updatedValue)\n onCheckedChange?.(updatedValue)\n }\n\n return (\n <RadixSwitch.Root\n ref={ref}\n className={styles({ intent, size, className })}\n value={value}\n checked={checked}\n defaultChecked={defaultChecked}\n onCheckedChange={handleCheckedChange}\n name={name}\n required={required || isRequired}\n aria-invalid={isInvalid}\n aria-describedby={description}\n {...rest}\n >\n <span className={thumbWrapperStyles({ checked: isChecked })}>\n <RadixSwitch.Thumb className={thumbStyles({ size, checked: isChecked })}>\n {isChecked && checkedIcon && (\n <Slot className={thumbCheckSVGStyles({ size })}>{checkedIcon}</Slot>\n )}\n {!isChecked && uncheckedIcon && (\n <Slot className={thumbCheckSVGStyles({ size })}>{uncheckedIcon}</Slot>\n )}\n </RadixSwitch.Thumb>\n </span>\n </RadixSwitch.Root>\n )\n}\n\nSwitchInput.displayName = 'SwitchInput'\n","import { makeVariants, tw } from '@spark-ui/internal-utils'\nimport { cva, VariantProps } from 'class-variance-authority'\n\nexport const styles = cva(\n tw([\n 'relative shrink-0 self-baseline',\n 'cursor-pointer',\n 'rounded-full border-transparent',\n 'hover:ring-4',\n 'transition-colors duration-200 ease-in-out',\n 'disabled:hover:ring-transparent disabled:cursor-not-allowed disabled:opacity-dim-3',\n 'focus-visible:u-outline',\n 'data-[state=unchecked]:bg-on-surface/dim-4',\n 'u-shadow-border-transition',\n 'overflow-x-hidden',\n ]),\n {\n variants: {\n /**\n * Size of the switch input.\n */\n size: makeVariants<'size', ['sm', 'md']>({\n sm: tw(['h-sz-24', 'w-sz-40', 'border-md']),\n md: tw(['h-sz-32', 'w-sz-56', 'border-[4px]']),\n }),\n /**\n * Color scheme of the switch input.\n */\n intent: makeVariants<\n 'intent',\n ['main', 'support', 'accent', 'basic', 'success', 'alert', 'error', 'info', 'neutral']\n >({\n main: ['data-[state=checked]:bg-main', 'hover:ring-main-container', 'text-main'],\n support: [\n 'data-[state=checked]:bg-support',\n 'hover:ring-support-container',\n 'text-support',\n ],\n accent: ['data-[state=checked]:bg-accent', 'hover:ring-accent-container', 'text-accent'],\n basic: ['data-[state=checked]:bg-basic', 'hover:ring-basic-container', 'text-basic'],\n success: [\n 'data-[state=checked]:bg-success',\n 'hover:ring-success-container',\n 'text-success',\n ],\n alert: ['data-[state=checked]:bg-alert', 'hover:ring-alert-container', 'text-alert'],\n error: ['data-[state=checked]:bg-error', 'hover:ring-error-container', 'text-error'],\n info: ['data-[state=checked]:bg-info', 'hover:ring-info-container', 'text-info'],\n neutral: [\n 'data-[state=checked]:bg-neutral',\n 'hover:ring-neutral-container',\n 'text-neutral',\n ],\n }),\n },\n defaultVariants: {\n intent: 'basic',\n size: 'sm',\n },\n }\n)\n\nexport type StylesProps = VariantProps<typeof styles>\n\nexport const thumbWrapperStyles = cva(\n [\n 'pointer-events-none absolute inset-0 flex items-center',\n 'transition-all duration-200 ease-in-out',\n ],\n {\n variants: {\n checked: {\n true: 'translate-x-full',\n false: 'translate-x-0',\n },\n },\n }\n)\n\nexport const thumbStyles = cva(\n [\n 'absolute left-0 top-0 flex items-center justify-center',\n 'bg-surface',\n 'rounded-full',\n 'ring-0',\n 'transition-all duration-200 ease-in-out',\n ],\n {\n variants: {\n size: makeVariants<'size', ['sm', 'md']>({\n sm: ['h-sz-20', 'w-sz-20'],\n md: ['h-sz-24', 'w-sz-24'],\n }),\n checked: {\n true: '-translate-x-full',\n false: 'translate-x-0 text-on-surface/dim-4',\n },\n },\n defaultVariants: {\n size: 'sm',\n checked: false,\n },\n }\n)\n\nexport const thumbCheckSVGStyles = cva(['transition-opacity duration-200'], {\n variants: {\n size: makeVariants<'size', ['sm', 'md']>({\n sm: ['h-sz-10 w-sz-10'],\n md: ['h-sz-12 w-sz-12'],\n }),\n },\n defaultVariants: {\n size: 'sm',\n },\n})\n","import { cva, VariantProps } from 'class-variance-authority'\n\nexport const labelStyles = cva('', {\n variants: {\n disabled: {\n true: ['text-neutral/dim-2', 'cursor-not-allowed'],\n false: ['cursor-pointer'],\n },\n },\n defaultVariants: {\n disabled: false,\n },\n})\n\nexport type LabelStylesProps = VariantProps<typeof labelStyles>\n","import { Label, LabelProps } from '../label'\nimport { labelStyles, LabelStylesProps } from './SwitchLabel.styles'\n\nexport interface SwitchLabelProps extends LabelStylesProps, LabelProps {\n /**\n * Change the component to the HTML tag or custom component of the only child.\n */\n asChild?: boolean\n /**\n * The id of the element the label is associated with.\n */\n htmlFor?: string\n /**\n * When true, prevents the user from interacting with the switch item.\n */\n disabled?: boolean\n}\n\nexport const SwitchLabel = ({ className, disabled, ...others }: SwitchLabelProps) => (\n <Label className={labelStyles({ disabled, className })} {...others} />\n)\n"],"mappings":";;;;;;;;AAAA,SAAS,uBAAAA,4BAA2B;AACpC,SAAS,UAAU;AACnB,SAAS,aAAa;;;ACFtB,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,aAAa;AACtB,SAAS,aAAa;AACtB,SAAS,UAAU,mBAAmB;;;ACJtC,SAAS,cAAc,UAAU;AACjC,SAAS,WAAyB;AAE3B,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAAA,EACD;AAAA,IACE,UAAU;AAAA;AAAA;AAAA;AAAA,MAIR,MAAM,aAAmC;AAAA,QACvC,IAAI,GAAG,CAAC,WAAW,WAAW,WAAW,CAAC;AAAA,QAC1C,IAAI,GAAG,CAAC,WAAW,WAAW,cAAc,CAAC;AAAA,MAC/C,CAAC;AAAA;AAAA;AAAA;AAAA,MAID,QAAQ,aAGN;AAAA,QACA,MAAM,CAAC,gCAAgC,6BAA6B,WAAW;AAAA,QAC/E,SAAS;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,QACA,QAAQ,CAAC,kCAAkC,+BAA+B,aAAa;AAAA,QACvF,OAAO,CAAC,iCAAiC,8BAA8B,YAAY;AAAA,QACnF,SAAS;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,QACA,OAAO,CAAC,iCAAiC,8BAA8B,YAAY;AAAA,QACnF,OAAO,CAAC,iCAAiC,8BAA8B,YAAY;AAAA,QACnF,MAAM,CAAC,gCAAgC,6BAA6B,WAAW;AAAA,QAC/E,SAAS;AAAA,UACP;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