|
| 1 | +import type { VariantProps } from '..' |
| 2 | +import { |
| 3 | + expectAssignable, |
| 4 | + expectError, |
| 5 | + expectNotAssignable, |
| 6 | + expectType, |
| 7 | +} from 'tsd' |
| 8 | +import { |
| 9 | + cn, |
| 10 | + cnBase, |
| 11 | + createTV, |
| 12 | + tv, |
| 13 | + |
| 14 | +} from '..' |
| 15 | + |
| 16 | +expectType<string | undefined>(cnBase('px-2', false)) |
| 17 | +expectType<string | undefined>(cn('px-2', undefined)()) |
| 18 | + |
| 19 | +const button = tv({ |
| 20 | + base: 'inline-flex', |
| 21 | + variants: { |
| 22 | + intent: { |
| 23 | + primary: 'bg-blue-500', |
| 24 | + secondary: 'bg-gray-500', |
| 25 | + }, |
| 26 | + size: { |
| 27 | + sm: 'text-sm', |
| 28 | + lg: 'text-lg', |
| 29 | + }, |
| 30 | + rounded: { |
| 31 | + true: 'rounded-full', |
| 32 | + false: 'rounded-none', |
| 33 | + }, |
| 34 | + }, |
| 35 | + defaultVariants: { |
| 36 | + size: 'sm', |
| 37 | + }, |
| 38 | +}) |
| 39 | + |
| 40 | +expectType<string>(button()) |
| 41 | +expectType<string>(button({ class: 'px-2' })) |
| 42 | +expectError(button({ class: 'px-2', className: 'py-2' })) |
| 43 | +expectError(button({ intent: 'tertiary' })) |
| 44 | +expectError(button({ rounded: 'true' })) |
| 45 | +expectError(button({ size: 'xl' })) |
| 46 | + |
| 47 | +type ButtonVariantProps = VariantProps<typeof button> |
| 48 | +const buttonVariantProps: ButtonVariantProps = { |
| 49 | + intent: 'primary', |
| 50 | + rounded: true, |
| 51 | +} |
| 52 | + |
| 53 | +expectType<{ |
| 54 | + intent?: 'primary' | 'secondary' | undefined |
| 55 | + size?: 'sm' | 'lg' | undefined |
| 56 | + rounded?: boolean | undefined |
| 57 | +}>(buttonVariantProps) |
| 58 | +expectNotAssignable<ButtonVariantProps>({ class: 'px-2' }) |
| 59 | +expectNotAssignable<ButtonVariantProps>({ className: 'py-2' }) |
| 60 | + |
| 61 | +const baseButton = tv({ |
| 62 | + variants: { |
| 63 | + tone: { |
| 64 | + solid: 'bg-black', |
| 65 | + soft: 'bg-gray-200', |
| 66 | + }, |
| 67 | + }, |
| 68 | +}) |
| 69 | + |
| 70 | +const extendedButton = tv({ |
| 71 | + extend: baseButton, |
| 72 | + variants: { |
| 73 | + size: { |
| 74 | + sm: 'text-sm', |
| 75 | + lg: 'text-lg', |
| 76 | + }, |
| 77 | + }, |
| 78 | +}) |
| 79 | + |
| 80 | +type ExtendedButtonProps = VariantProps<typeof extendedButton> |
| 81 | +expectAssignable<ExtendedButtonProps>({ tone: 'solid', size: 'sm' }) |
| 82 | +expectError(extendedButton({ tone: 'ghost' })) |
| 83 | + |
| 84 | +const card = tv({ |
| 85 | + base: 'card', |
| 86 | + slots: { |
| 87 | + root: 'card-root', |
| 88 | + icon: 'card-icon', |
| 89 | + }, |
| 90 | + variants: { |
| 91 | + size: { |
| 92 | + sm: { root: 'text-sm', icon: 'h-4' }, |
| 93 | + lg: { root: 'text-lg', icon: 'h-6' }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + defaultVariants: { |
| 97 | + size: 'sm', |
| 98 | + }, |
| 99 | +}) |
| 100 | + |
| 101 | +const cardSlots = card({ size: 'sm' }) |
| 102 | +expectType<string>(cardSlots.base()) |
| 103 | +expectType<string>(cardSlots.root()) |
| 104 | +expectType<string>(cardSlots.icon()) |
| 105 | +expectType<string>(cardSlots.root({ size: 'lg' })) |
| 106 | +expectError(cardSlots.root({ size: 'xl' })) |
| 107 | + |
| 108 | +const createResponsive = createTV({ responsiveVariants: ['sm', 'md'] as const }) |
| 109 | +const responsiveButton = createResponsive({ |
| 110 | + variants: { |
| 111 | + size: { |
| 112 | + sm: 'text-sm', |
| 113 | + lg: 'text-lg', |
| 114 | + }, |
| 115 | + }, |
| 116 | +}) |
| 117 | + |
| 118 | +expectType<string>(responsiveButton()) |
| 119 | + |
| 120 | +type ResponsiveProps = VariantProps<typeof responsiveButton> |
| 121 | +expectAssignable<ResponsiveProps>({ size: 'sm' }) |
| 122 | +expectAssignable<ResponsiveProps>({ size: { initial: 'sm', md: 'lg' } }) |
| 123 | +expectError(responsiveButton({ size: { lg: 'sm' } })) |
0 commit comments