@thangnqdev/ui 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +144 -142
- package/bin/cli.js +125 -125
- package/dist/bento/index.cjs.map +1 -1
- package/dist/bento/index.js +2 -2
- package/dist/buttons/index.cjs.map +1 -1
- package/dist/buttons/index.js +3 -3
- package/dist/{chunk-77KTYOMQ.js → chunk-2OXZK6IP.js} +3 -3
- package/dist/chunk-2OXZK6IP.js.map +1 -0
- package/dist/{chunk-4CRFNALF.js → chunk-4GNFT2ZJ.js} +7 -7
- package/dist/chunk-4GNFT2ZJ.js.map +1 -0
- package/dist/{chunk-2YJBW4E2.js → chunk-4S6L5V7X.js} +2 -2
- package/dist/chunk-4S6L5V7X.js.map +1 -0
- package/dist/{chunk-I2FQBMVW.js → chunk-66VWUJEI.js} +3 -3
- package/dist/chunk-66VWUJEI.js.map +1 -0
- package/dist/{chunk-YOCGBORV.js → chunk-7NMB2Y64.js} +4 -4
- package/dist/chunk-7NMB2Y64.js.map +1 -0
- package/dist/{chunk-V52JOSDF.js → chunk-A7P3O5Z5.js} +3 -3
- package/dist/chunk-A7P3O5Z5.js.map +1 -0
- package/dist/{chunk-ME4XILG5.js → chunk-ATYGDTMK.js} +5 -5
- package/dist/chunk-ATYGDTMK.js.map +1 -0
- package/dist/{chunk-S6XJMYME.js → chunk-C4SUXMI3.js} +3 -3
- package/dist/chunk-C4SUXMI3.js.map +1 -0
- package/dist/{chunk-EXFR2BFJ.js → chunk-DUPPPGXY.js} +7 -7
- package/dist/chunk-DUPPPGXY.js.map +1 -0
- package/dist/{chunk-I5O2WCGF.js → chunk-EU2LPUYP.js} +3 -3
- package/dist/chunk-EU2LPUYP.js.map +1 -0
- package/dist/{chunk-D7FB3TCA.js → chunk-GXI2H5XA.js} +3 -3
- package/dist/chunk-GXI2H5XA.js.map +1 -0
- package/dist/{chunk-ADO3SVUA.js → chunk-IABPUCAQ.js} +2 -2
- package/dist/chunk-IABPUCAQ.js.map +1 -0
- package/dist/{chunk-PC5SZFZC.js → chunk-JOKEKOP3.js} +8 -8
- package/dist/chunk-JOKEKOP3.js.map +1 -0
- package/dist/{chunk-GXWB6BSY.js → chunk-KUQWUTZA.js} +4 -4
- package/dist/chunk-KUQWUTZA.js.map +1 -0
- package/dist/{chunk-ZEDXARXW.js → chunk-U3J4UPV3.js} +2 -2
- package/dist/chunk-U3J4UPV3.js.map +1 -0
- package/dist/{chunk-DSEL7ELZ.js → chunk-VE2PNSDQ.js} +4 -4
- package/dist/chunk-VE2PNSDQ.js.map +1 -0
- package/dist/{chunk-YJ64RI7J.js → chunk-WAFHC4DQ.js} +3 -3
- package/dist/chunk-WAFHC4DQ.js.map +1 -0
- package/dist/{chunk-W5WNWTQC.js → chunk-YJ67WYVW.js} +5 -5
- package/dist/chunk-YJ67WYVW.js.map +1 -0
- package/dist/{chunk-4MS6ZHFQ.js → chunk-Z427NPMU.js} +7 -7
- package/dist/chunk-Z427NPMU.js.map +1 -0
- package/dist/core/index.cjs +4 -4
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.js +2 -2
- package/dist/data-display/index.cjs.map +1 -1
- package/dist/data-display/index.js +5 -5
- package/dist/feedback/index.cjs.map +1 -1
- package/dist/feedback/index.js +2 -2
- package/dist/forms/index.cjs.map +1 -1
- package/dist/forms/index.js +8 -8
- package/dist/glass/index.cjs.map +1 -1
- package/dist/glass/index.js +2 -2
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +19 -19
- package/dist/layout/index.cjs.map +1 -1
- package/dist/layout/index.js +2 -2
- package/dist/navigation/index.cjs.map +1 -1
- package/dist/navigation/index.js +6 -6
- package/dist/overlays/index.cjs.map +1 -1
- package/dist/overlays/index.js +7 -7
- package/dist/reset.css +45 -45
- package/dist/tokens.css +11 -11
- package/dist/typography/index.cjs.map +1 -1
- package/dist/typography/index.js +4 -4
- package/llms.txt +3 -3
- package/package.json +165 -165
- package/scripts/postinstall.js +125 -125
- package/src/bento/BentoCard.tsx +140 -140
- package/src/bento/BentoGrid.tsx +50 -50
- package/src/bento/index.ts +2 -2
- package/src/bento/tokens.css +46 -46
- package/src/buttons/Button.tsx +104 -104
- package/src/buttons/ButtonGroup.tsx +43 -43
- package/src/buttons/IconButton.tsx +79 -79
- package/src/buttons/SplitButton.tsx +186 -186
- package/src/buttons/ToggleButton.tsx +157 -157
- package/src/buttons/index.ts +5 -5
- package/src/buttons/tokens.css +6 -6
- package/src/core/ThemeProvider.tsx +133 -133
- package/src/core/index.ts +2 -2
- package/src/core/tokens.css +134 -134
- package/src/data-display/Accordion.tsx +147 -147
- package/src/data-display/Avatar.tsx +134 -134
- package/src/data-display/Card.tsx +112 -112
- package/src/data-display/Chip.tsx +74 -74
- package/src/data-display/DataTablePro.tsx +534 -534
- package/src/data-display/StatCard.tsx +60 -60
- package/src/data-display/Table.tsx +74 -74
- package/src/data-display/Timeline.tsx +62 -62
- package/src/data-display/TreeView.tsx +279 -279
- package/src/data-display/tokens.css +4 -4
- package/src/feedback/Alert.tsx +80 -80
- package/src/feedback/EmptyState.tsx +45 -45
- package/src/feedback/Skeleton.tsx +66 -66
- package/src/feedback/Spinner.tsx +65 -65
- package/src/feedback/index.ts +5 -5
- package/src/feedback/tokens.css +4 -4
- package/src/forms/Checkbox.tsx +149 -149
- package/src/forms/CheckboxGroup.tsx +76 -76
- package/src/forms/Combobox.tsx +512 -512
- package/src/forms/DatePicker.tsx +784 -784
- package/src/forms/FileUpload.tsx +266 -266
- package/src/forms/FormField.tsx +115 -115
- package/src/forms/Input.tsx +151 -151
- package/src/forms/NumberInput.tsx +262 -262
- package/src/forms/PasswordInput.tsx +35 -35
- package/src/forms/PinInput.tsx +139 -139
- package/src/forms/Radio.tsx +142 -142
- package/src/forms/RangeSlider.tsx +237 -237
- package/src/forms/SearchInput.tsx +34 -34
- package/src/forms/Select.tsx +103 -103
- package/src/forms/Slider.tsx +85 -85
- package/src/forms/Textarea.tsx +114 -114
- package/src/forms/tokens.css +6 -6
- package/src/glass/GlassButton.tsx +59 -59
- package/src/glass/GlassCard.tsx +71 -71
- package/src/glass/index.ts +3 -3
- package/src/glass/tokens.css +81 -81
- package/src/index.ts +26 -26
- package/src/layout/AspectRatio.tsx +39 -39
- package/src/layout/Container.tsx +53 -53
- package/src/layout/Divider.tsx +61 -61
- package/src/layout/Flex.tsx +79 -79
- package/src/layout/Grid.tsx +124 -124
- package/src/layout/PageLayout.tsx +59 -59
- package/src/layout/Section.tsx +47 -47
- package/src/layout/Spacer.tsx +30 -30
- package/src/layout/Stack.tsx +88 -88
- package/src/layout/tokens.css +8 -8
- package/src/navigation/Breadcrumb.tsx +73 -73
- package/src/navigation/CommandPalette.tsx +142 -142
- package/src/navigation/DropdownMenu.tsx +244 -244
- package/src/navigation/Pagination.tsx +114 -114
- package/src/navigation/Stepper.tsx +93 -93
- package/src/navigation/Tabs.tsx +265 -265
- package/src/navigation/index.ts +6 -6
- package/src/navigation/tokens.css +4 -4
- package/src/overlays/AlertDialog.tsx +63 -63
- package/src/overlays/BottomSheet.tsx +143 -143
- package/src/overlays/ContextMenu.tsx +161 -161
- package/src/overlays/Drawer.tsx +160 -160
- package/src/overlays/FocusTrap.tsx +122 -122
- package/src/overlays/Modal.tsx +296 -296
- package/src/overlays/Popover.tsx +156 -156
- package/src/overlays/Portal.tsx +23 -23
- package/src/overlays/Toast.tsx +175 -175
- package/src/overlays/Tooltip.tsx +125 -125
- package/src/overlays/tokens.css +8 -8
- package/src/registry/components.ts +1368 -1368
- package/src/styles/components.css +39 -39
- package/src/styles/index.css +5 -5
- package/src/styles/reset.css +45 -45
- package/src/styles/tokens.css +11 -11
- package/src/typography/Blockquote.tsx +32 -32
- package/src/typography/Code.tsx +89 -89
- package/src/typography/Heading.tsx +56 -56
- package/src/typography/Kbd.tsx +25 -25
- package/src/typography/Label.tsx +47 -47
- package/src/typography/Link.tsx +56 -56
- package/src/typography/Text.tsx +62 -62
- package/src/typography/index.ts +7 -7
- package/src/typography/tokens.css +5 -5
- package/src/utils/cn.ts +6 -6
- package/src/utils/scroll-lock.ts +19 -19
- package/src/utils/useControllableState.ts +54 -54
- package/ui-contract.json +1 -1
- package/dist/chunk-2YJBW4E2.js.map +0 -1
- package/dist/chunk-4CRFNALF.js.map +0 -1
- package/dist/chunk-4MS6ZHFQ.js.map +0 -1
- package/dist/chunk-77KTYOMQ.js.map +0 -1
- package/dist/chunk-ADO3SVUA.js.map +0 -1
- package/dist/chunk-D7FB3TCA.js.map +0 -1
- package/dist/chunk-DSEL7ELZ.js.map +0 -1
- package/dist/chunk-EXFR2BFJ.js.map +0 -1
- package/dist/chunk-GXWB6BSY.js.map +0 -1
- package/dist/chunk-I2FQBMVW.js.map +0 -1
- package/dist/chunk-I5O2WCGF.js.map +0 -1
- package/dist/chunk-ME4XILG5.js.map +0 -1
- package/dist/chunk-PC5SZFZC.js.map +0 -1
- package/dist/chunk-S6XJMYME.js.map +0 -1
- package/dist/chunk-V52JOSDF.js.map +0 -1
- package/dist/chunk-W5WNWTQC.js.map +0 -1
- package/dist/chunk-YJ64RI7J.js.map +0 -1
- package/dist/chunk-YOCGBORV.js.map +0 -1
- package/dist/chunk-ZEDXARXW.js.map +0 -1
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
import React, { forwardRef, createContext, useContext } from 'react';
|
|
2
|
-
import { cva, type VariantProps } from 'class-variance-authority';
|
|
3
|
-
import { cn } from '../utils/cn';
|
|
4
|
-
|
|
5
|
-
export const toggleVariants = cva(
|
|
6
|
-
'inline-flex items-center justify-center text-sm font-medium transition-colors hover:bg-[var(--ui-bg-tertiary)] hover:text-[var(--ui-text-primary)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ui-primary)] disabled:pointer-events-none disabled:opacity-50 select-none cursor-pointer',
|
|
7
|
-
{
|
|
8
|
-
variants: {
|
|
9
|
-
variant: {
|
|
10
|
-
default: 'bg-transparent text-[var(--ui-text-secondary)]',
|
|
11
|
-
outline:
|
|
12
|
-
'border border-[var(--ui-border-default)] bg-transparent hover:bg-[var(--ui-bg-tertiary)]',
|
|
13
|
-
},
|
|
14
|
-
size: {
|
|
15
|
-
sm: 'h-8 px-2.5 text-xs rounded-[var(--ui-radius-sm)]',
|
|
16
|
-
md: 'h-9 px-3 text-sm rounded-[var(--ui-radius-md)]',
|
|
17
|
-
lg: 'h-10 px-4 text-base rounded-[var(--ui-radius-md)]',
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
defaultVariants: {
|
|
21
|
-
variant: 'default',
|
|
22
|
-
size: 'md',
|
|
23
|
-
},
|
|
24
|
-
}
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
interface ToggleGroupContextType {
|
|
28
|
-
value?: string | string[];
|
|
29
|
-
onChange?: (val: string) => void;
|
|
30
|
-
variant?: 'default' | 'outline';
|
|
31
|
-
size?: 'sm' | 'md' | 'lg';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const ToggleGroupContext = createContext<ToggleGroupContextType>({});
|
|
35
|
-
|
|
36
|
-
export interface ToggleButtonProps
|
|
37
|
-
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
38
|
-
VariantProps<typeof toggleVariants> {
|
|
39
|
-
pressed?: boolean;
|
|
40
|
-
value?: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const ToggleButton = forwardRef<HTMLButtonElement, ToggleButtonProps>(
|
|
44
|
-
({ className, pressed: customPressed, variant, size, value, onClick, children, ...props }, ref) => {
|
|
45
|
-
const group = useContext(ToggleGroupContext);
|
|
46
|
-
|
|
47
|
-
const isPressed =
|
|
48
|
-
customPressed !== undefined
|
|
49
|
-
? customPressed
|
|
50
|
-
: group.value !== undefined && value !== undefined
|
|
51
|
-
? Array.isArray(group.value)
|
|
52
|
-
? group.value.includes(value)
|
|
53
|
-
: group.value === value
|
|
54
|
-
: false;
|
|
55
|
-
|
|
56
|
-
const currentVariant = variant || group.variant || 'default';
|
|
57
|
-
const currentSize = size || group.size || 'md';
|
|
58
|
-
|
|
59
|
-
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
60
|
-
if (value && group.onChange) {
|
|
61
|
-
group.onChange(value);
|
|
62
|
-
}
|
|
63
|
-
onClick?.(e);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
return (
|
|
67
|
-
<button
|
|
68
|
-
ref={ref}
|
|
69
|
-
type="button"
|
|
70
|
-
aria-pressed={isPressed}
|
|
71
|
-
data-state={isPressed ? 'on' : 'off'}
|
|
72
|
-
onClick={handleClick}
|
|
73
|
-
className={cn(
|
|
74
|
-
toggleVariants({ variant: currentVariant, size: currentSize }),
|
|
75
|
-
isPressed &&
|
|
76
|
-
'bg-[var(--ui-bg-tertiary)] text-[var(--ui-text-primary)] font-semibold shadow-inner border-[var(--ui-border-strong)]',
|
|
77
|
-
className
|
|
78
|
-
)}
|
|
79
|
-
{...props}
|
|
80
|
-
>
|
|
81
|
-
{children}
|
|
82
|
-
</button>
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
ToggleButton.displayName = 'ToggleButton';
|
|
87
|
-
|
|
88
|
-
export interface ToggleGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
89
|
-
type?: 'single' | 'multiple';
|
|
90
|
-
value?: string | string[];
|
|
91
|
-
defaultValue?: string | string[];
|
|
92
|
-
onValueChange?: (value: any) => void;
|
|
93
|
-
variant?: 'default' | 'outline';
|
|
94
|
-
size?: 'sm' | 'md' | 'lg';
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export const ToggleGroup = forwardRef<HTMLDivElement, ToggleGroupProps>(
|
|
98
|
-
(
|
|
99
|
-
{
|
|
100
|
-
className,
|
|
101
|
-
type = 'single',
|
|
102
|
-
value: controlledValue,
|
|
103
|
-
defaultValue,
|
|
104
|
-
onValueChange,
|
|
105
|
-
variant = 'outline',
|
|
106
|
-
size = 'md',
|
|
107
|
-
children,
|
|
108
|
-
...props
|
|
109
|
-
},
|
|
110
|
-
ref
|
|
111
|
-
) => {
|
|
112
|
-
const [stateValue, setStateValue] = React.useState<string | string[]>(
|
|
113
|
-
defaultValue || (type === 'multiple' ? [] : '')
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
const currentValue = controlledValue !== undefined ? controlledValue : stateValue;
|
|
117
|
-
|
|
118
|
-
const handleItemChange = (itemValue: string) => {
|
|
119
|
-
if (type === 'single') {
|
|
120
|
-
const next = currentValue === itemValue ? '' : itemValue;
|
|
121
|
-
setStateValue(next);
|
|
122
|
-
onValueChange?.(next);
|
|
123
|
-
} else {
|
|
124
|
-
const arr = Array.isArray(currentValue) ? currentValue : [];
|
|
125
|
-
const next = arr.includes(itemValue)
|
|
126
|
-
? arr.filter((v) => v !== itemValue)
|
|
127
|
-
: [...arr, itemValue];
|
|
128
|
-
setStateValue(next);
|
|
129
|
-
onValueChange?.(next);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
return (
|
|
134
|
-
<ToggleGroupContext.Provider
|
|
135
|
-
value={{
|
|
136
|
-
value: currentValue,
|
|
137
|
-
onChange: handleItemChange,
|
|
138
|
-
variant,
|
|
139
|
-
size,
|
|
140
|
-
}}
|
|
141
|
-
>
|
|
142
|
-
<div
|
|
143
|
-
ref={ref}
|
|
144
|
-
role="group"
|
|
145
|
-
className={cn(
|
|
146
|
-
'inline-flex items-center gap-1 rounded-[var(--ui-radius-md)] p-1 bg-[var(--ui-bg-secondary)] border border-[var(--ui-border-subtle)]',
|
|
147
|
-
className
|
|
148
|
-
)}
|
|
149
|
-
{...props}
|
|
150
|
-
>
|
|
151
|
-
{children}
|
|
152
|
-
</div>
|
|
153
|
-
</ToggleGroupContext.Provider>
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
);
|
|
157
|
-
ToggleGroup.displayName = 'ToggleGroup';
|
|
1
|
+
import React, { forwardRef, createContext, useContext } from 'react';
|
|
2
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
3
|
+
import { cn } from '../utils/cn';
|
|
4
|
+
|
|
5
|
+
export const toggleVariants = cva(
|
|
6
|
+
'inline-flex items-center justify-center text-sm font-medium transition-colors hover:bg-[var(--ui-bg-tertiary)] hover:text-[var(--ui-text-primary)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ui-primary)] disabled:pointer-events-none disabled:opacity-50 select-none cursor-pointer',
|
|
7
|
+
{
|
|
8
|
+
variants: {
|
|
9
|
+
variant: {
|
|
10
|
+
default: 'bg-transparent text-[var(--ui-text-secondary)]',
|
|
11
|
+
outline:
|
|
12
|
+
'border border-[var(--ui-border-default)] bg-transparent hover:bg-[var(--ui-bg-tertiary)]',
|
|
13
|
+
},
|
|
14
|
+
size: {
|
|
15
|
+
sm: 'h-8 px-2.5 text-xs rounded-[var(--ui-radius-sm)]',
|
|
16
|
+
md: 'h-9 px-3 text-sm rounded-[var(--ui-radius-md)]',
|
|
17
|
+
lg: 'h-10 px-4 text-base rounded-[var(--ui-radius-md)]',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
defaultVariants: {
|
|
21
|
+
variant: 'default',
|
|
22
|
+
size: 'md',
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
interface ToggleGroupContextType {
|
|
28
|
+
value?: string | string[];
|
|
29
|
+
onChange?: (val: string) => void;
|
|
30
|
+
variant?: 'default' | 'outline';
|
|
31
|
+
size?: 'sm' | 'md' | 'lg';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ToggleGroupContext = createContext<ToggleGroupContextType>({});
|
|
35
|
+
|
|
36
|
+
export interface ToggleButtonProps
|
|
37
|
+
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
38
|
+
VariantProps<typeof toggleVariants> {
|
|
39
|
+
pressed?: boolean;
|
|
40
|
+
value?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const ToggleButton = forwardRef<HTMLButtonElement, ToggleButtonProps>(
|
|
44
|
+
({ className, pressed: customPressed, variant, size, value, onClick, children, ...props }, ref) => {
|
|
45
|
+
const group = useContext(ToggleGroupContext);
|
|
46
|
+
|
|
47
|
+
const isPressed =
|
|
48
|
+
customPressed !== undefined
|
|
49
|
+
? customPressed
|
|
50
|
+
: group.value !== undefined && value !== undefined
|
|
51
|
+
? Array.isArray(group.value)
|
|
52
|
+
? group.value.includes(value)
|
|
53
|
+
: group.value === value
|
|
54
|
+
: false;
|
|
55
|
+
|
|
56
|
+
const currentVariant = variant || group.variant || 'default';
|
|
57
|
+
const currentSize = size || group.size || 'md';
|
|
58
|
+
|
|
59
|
+
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
60
|
+
if (value && group.onChange) {
|
|
61
|
+
group.onChange(value);
|
|
62
|
+
}
|
|
63
|
+
onClick?.(e);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<button
|
|
68
|
+
ref={ref}
|
|
69
|
+
type="button"
|
|
70
|
+
aria-pressed={isPressed}
|
|
71
|
+
data-state={isPressed ? 'on' : 'off'}
|
|
72
|
+
onClick={handleClick}
|
|
73
|
+
className={cn(
|
|
74
|
+
toggleVariants({ variant: currentVariant, size: currentSize }),
|
|
75
|
+
isPressed &&
|
|
76
|
+
'bg-[var(--ui-bg-tertiary)] text-[var(--ui-text-primary)] font-semibold shadow-inner border-[var(--ui-border-strong)]',
|
|
77
|
+
className
|
|
78
|
+
)}
|
|
79
|
+
{...props}
|
|
80
|
+
>
|
|
81
|
+
{children}
|
|
82
|
+
</button>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
ToggleButton.displayName = 'ToggleButton';
|
|
87
|
+
|
|
88
|
+
export interface ToggleGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
89
|
+
type?: 'single' | 'multiple';
|
|
90
|
+
value?: string | string[];
|
|
91
|
+
defaultValue?: string | string[];
|
|
92
|
+
onValueChange?: (value: any) => void;
|
|
93
|
+
variant?: 'default' | 'outline';
|
|
94
|
+
size?: 'sm' | 'md' | 'lg';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const ToggleGroup = forwardRef<HTMLDivElement, ToggleGroupProps>(
|
|
98
|
+
(
|
|
99
|
+
{
|
|
100
|
+
className,
|
|
101
|
+
type = 'single',
|
|
102
|
+
value: controlledValue,
|
|
103
|
+
defaultValue,
|
|
104
|
+
onValueChange,
|
|
105
|
+
variant = 'outline',
|
|
106
|
+
size = 'md',
|
|
107
|
+
children,
|
|
108
|
+
...props
|
|
109
|
+
},
|
|
110
|
+
ref
|
|
111
|
+
) => {
|
|
112
|
+
const [stateValue, setStateValue] = React.useState<string | string[]>(
|
|
113
|
+
defaultValue || (type === 'multiple' ? [] : '')
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const currentValue = controlledValue !== undefined ? controlledValue : stateValue;
|
|
117
|
+
|
|
118
|
+
const handleItemChange = (itemValue: string) => {
|
|
119
|
+
if (type === 'single') {
|
|
120
|
+
const next = currentValue === itemValue ? '' : itemValue;
|
|
121
|
+
setStateValue(next);
|
|
122
|
+
onValueChange?.(next);
|
|
123
|
+
} else {
|
|
124
|
+
const arr = Array.isArray(currentValue) ? currentValue : [];
|
|
125
|
+
const next = arr.includes(itemValue)
|
|
126
|
+
? arr.filter((v) => v !== itemValue)
|
|
127
|
+
: [...arr, itemValue];
|
|
128
|
+
setStateValue(next);
|
|
129
|
+
onValueChange?.(next);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<ToggleGroupContext.Provider
|
|
135
|
+
value={{
|
|
136
|
+
value: currentValue,
|
|
137
|
+
onChange: handleItemChange,
|
|
138
|
+
variant,
|
|
139
|
+
size,
|
|
140
|
+
}}
|
|
141
|
+
>
|
|
142
|
+
<div
|
|
143
|
+
ref={ref}
|
|
144
|
+
role="group"
|
|
145
|
+
className={cn(
|
|
146
|
+
'inline-flex items-center gap-1 rounded-[var(--ui-radius-md)] p-1 bg-[var(--ui-bg-secondary)] border border-[var(--ui-border-subtle)]',
|
|
147
|
+
className
|
|
148
|
+
)}
|
|
149
|
+
{...props}
|
|
150
|
+
>
|
|
151
|
+
{children}
|
|
152
|
+
</div>
|
|
153
|
+
</ToggleGroupContext.Provider>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
ToggleGroup.displayName = 'ToggleGroup';
|
package/src/buttons/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './Button';
|
|
2
|
-
export * from './IconButton';
|
|
3
|
-
export * from './ButtonGroup';
|
|
4
|
-
export * from './ToggleButton';
|
|
5
|
-
export * from './SplitButton';
|
|
1
|
+
export * from './Button';
|
|
2
|
+
export * from './IconButton';
|
|
3
|
+
export * from './ButtonGroup';
|
|
4
|
+
export * from './ToggleButton';
|
|
5
|
+
export * from './SplitButton';
|
package/src/buttons/tokens.css
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* Button Tokens */
|
|
2
|
-
:root {
|
|
3
|
-
--ui-btn-height-sm: 2rem;
|
|
4
|
-
--ui-btn-height-md: 2.5rem;
|
|
5
|
-
--ui-btn-height-lg: 3rem;
|
|
6
|
-
}
|
|
1
|
+
/* Button Tokens */
|
|
2
|
+
:root {
|
|
3
|
+
--ui-btn-height-sm: 2rem;
|
|
4
|
+
--ui-btn-height-md: 2.5rem;
|
|
5
|
+
--ui-btn-height-lg: 3rem;
|
|
6
|
+
}
|
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect, useState, useMemo, useCallback } from 'react';
|
|
2
|
-
|
|
3
|
-
export type Theme = 'dark' | 'light' | 'system';
|
|
4
|
-
|
|
5
|
-
export interface ThemeProviderProps {
|
|
6
|
-
children: React.ReactNode;
|
|
7
|
-
defaultTheme?: Theme;
|
|
8
|
-
storageKey?: string;
|
|
9
|
-
attribute?: string;
|
|
10
|
-
enableSystem?: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface ThemeContextType {
|
|
14
|
-
theme: Theme;
|
|
15
|
-
resolvedTheme: 'dark' | 'light';
|
|
16
|
-
setTheme: (theme: Theme) => void;
|
|
17
|
-
toggleTheme: () => void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
|
21
|
-
|
|
22
|
-
export function ThemeProvider({
|
|
23
|
-
children,
|
|
24
|
-
defaultTheme = '
|
|
25
|
-
storageKey = 'ui-theme',
|
|
26
|
-
attribute = 'data-theme',
|
|
27
|
-
enableSystem = true,
|
|
28
|
-
}: ThemeProviderProps) {
|
|
29
|
-
// Always initialize identically on SSR and initial client pass to avoid hydration mismatches
|
|
30
|
-
const [theme, setThemeState] = useState<Theme>(defaultTheme);
|
|
31
|
-
const [resolvedTheme, setResolvedTheme] = useState<'dark' | 'light'>(
|
|
32
|
-
defaultTheme === '
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
const computeResolved = useCallback(
|
|
36
|
-
(t: Theme): 'dark' | 'light' => {
|
|
37
|
-
if (t === 'system' && enableSystem && typeof window !== 'undefined' && window.matchMedia) {
|
|
38
|
-
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
39
|
-
}
|
|
40
|
-
return t === 'dark' ? 'dark' : 'light';
|
|
41
|
-
},
|
|
42
|
-
[enableSystem]
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// Client-side initialization: read storage once mounted
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (typeof window === 'undefined') return;
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
const stored = window.localStorage?.getItem(storageKey)
|
|
51
|
-
if (stored
|
|
1
|
+
import React, { createContext, useContext, useEffect, useState, useMemo, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
export type Theme = 'dark' | 'light' | 'system';
|
|
4
|
+
|
|
5
|
+
export interface ThemeProviderProps {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
defaultTheme?: Theme;
|
|
8
|
+
storageKey?: string;
|
|
9
|
+
attribute?: string;
|
|
10
|
+
enableSystem?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ThemeContextType {
|
|
14
|
+
theme: Theme;
|
|
15
|
+
resolvedTheme: 'dark' | 'light';
|
|
16
|
+
setTheme: (theme: Theme) => void;
|
|
17
|
+
toggleTheme: () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
|
21
|
+
|
|
22
|
+
export function ThemeProvider({
|
|
23
|
+
children,
|
|
24
|
+
defaultTheme = 'light',
|
|
25
|
+
storageKey = 'ui-theme',
|
|
26
|
+
attribute = 'data-theme',
|
|
27
|
+
enableSystem = true,
|
|
28
|
+
}: ThemeProviderProps) {
|
|
29
|
+
// Always initialize identically on SSR and initial client pass to avoid hydration mismatches
|
|
30
|
+
const [theme, setThemeState] = useState<Theme>(defaultTheme);
|
|
31
|
+
const [resolvedTheme, setResolvedTheme] = useState<'dark' | 'light'>(
|
|
32
|
+
defaultTheme === 'dark' ? 'dark' : 'light'
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const computeResolved = useCallback(
|
|
36
|
+
(t: Theme): 'dark' | 'light' => {
|
|
37
|
+
if (t === 'system' && enableSystem && typeof window !== 'undefined' && window.matchMedia) {
|
|
38
|
+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
39
|
+
}
|
|
40
|
+
return t === 'dark' ? 'dark' : 'light';
|
|
41
|
+
},
|
|
42
|
+
[enableSystem]
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Client-side initialization: read storage once mounted
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (typeof window === 'undefined') return;
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const stored = window.localStorage?.getItem(storageKey);
|
|
51
|
+
if (stored === 'dark' || stored === 'light' || stored === 'system') {
|
|
52
52
|
setThemeState(stored);
|
|
53
53
|
}
|
|
54
|
-
} catch {
|
|
55
|
-
// Ignore storage access errors in restricted iframe/incognito environments
|
|
56
|
-
}
|
|
57
|
-
}, [storageKey
|
|
58
|
-
|
|
59
|
-
// Sync theme changes to DOM and storage
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
if (typeof document === 'undefined') return;
|
|
62
|
-
|
|
63
|
-
const root = document.documentElement;
|
|
64
|
-
const currentResolved = computeResolved(theme);
|
|
65
|
-
setResolvedTheme(currentResolved);
|
|
66
|
-
|
|
67
|
-
root.setAttribute(attribute, currentResolved);
|
|
68
|
-
if (currentResolved === 'dark') {
|
|
69
|
-
root.classList.add('dark');
|
|
70
|
-
root.classList.remove('light');
|
|
71
|
-
} else {
|
|
72
|
-
root.classList.add('light');
|
|
73
|
-
root.classList.remove('dark');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {
|
|
77
|
-
try {
|
|
78
|
-
window.localStorage.setItem(storageKey, theme);
|
|
79
|
-
} catch {
|
|
80
|
-
// Ignore storage access errors
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}, [theme, attribute, computeResolved, storageKey]);
|
|
84
|
-
|
|
85
|
-
// System media query listener
|
|
86
|
-
useEffect(() => {
|
|
87
|
-
if (theme !== 'system' || !enableSystem || typeof window === 'undefined' || !window.matchMedia) return;
|
|
88
|
-
|
|
89
|
-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
90
|
-
const handleChange = () => {
|
|
91
|
-
const newResolved = mediaQuery.matches ? 'dark' : 'light';
|
|
92
|
-
setResolvedTheme(newResolved);
|
|
93
|
-
if (typeof document !== 'undefined') {
|
|
94
|
-
document.documentElement.setAttribute(attribute, newResolved);
|
|
95
|
-
if (newResolved === 'dark') {
|
|
96
|
-
document.documentElement.classList.add('dark');
|
|
97
|
-
document.documentElement.classList.remove('light');
|
|
98
|
-
} else {
|
|
99
|
-
document.documentElement.classList.add('light');
|
|
100
|
-
document.documentElement.classList.remove('dark');
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
mediaQuery.addEventListener('change', handleChange);
|
|
106
|
-
return () => mediaQuery.removeEventListener('change', handleChange);
|
|
107
|
-
}, [theme, enableSystem, attribute]);
|
|
108
|
-
|
|
109
|
-
const toggleTheme = () => {
|
|
110
|
-
setThemeState((prev) => {
|
|
111
|
-
if (prev === 'dark') return 'light';
|
|
112
|
-
return 'dark';
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const value = useMemo(
|
|
117
|
-
() => ({
|
|
118
|
-
theme,
|
|
119
|
-
resolvedTheme,
|
|
120
|
-
setTheme: setThemeState,
|
|
121
|
-
toggleTheme,
|
|
122
|
-
}),
|
|
123
|
-
[theme, resolvedTheme]
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function useTheme(): ThemeContextType {
|
|
130
|
-
const context = useContext(ThemeContext);
|
|
131
|
-
if (!context) {
|
|
132
|
-
throw new Error('useTheme must be used within a ThemeProvider');
|
|
133
|
-
}
|
|
134
|
-
return context;
|
|
135
|
-
}
|
|
54
|
+
} catch {
|
|
55
|
+
// Ignore storage access errors in restricted iframe/incognito environments
|
|
56
|
+
}
|
|
57
|
+
}, [storageKey]);
|
|
58
|
+
|
|
59
|
+
// Sync theme changes to DOM and storage
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (typeof document === 'undefined') return;
|
|
62
|
+
|
|
63
|
+
const root = document.documentElement;
|
|
64
|
+
const currentResolved = computeResolved(theme);
|
|
65
|
+
setResolvedTheme(currentResolved);
|
|
66
|
+
|
|
67
|
+
root.setAttribute(attribute, currentResolved);
|
|
68
|
+
if (currentResolved === 'dark') {
|
|
69
|
+
root.classList.add('dark');
|
|
70
|
+
root.classList.remove('light');
|
|
71
|
+
} else {
|
|
72
|
+
root.classList.add('light');
|
|
73
|
+
root.classList.remove('dark');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {
|
|
77
|
+
try {
|
|
78
|
+
window.localStorage.setItem(storageKey, theme);
|
|
79
|
+
} catch {
|
|
80
|
+
// Ignore storage access errors
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}, [theme, attribute, computeResolved, storageKey]);
|
|
84
|
+
|
|
85
|
+
// System media query listener
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (theme !== 'system' || !enableSystem || typeof window === 'undefined' || !window.matchMedia) return;
|
|
88
|
+
|
|
89
|
+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
90
|
+
const handleChange = () => {
|
|
91
|
+
const newResolved = mediaQuery.matches ? 'dark' : 'light';
|
|
92
|
+
setResolvedTheme(newResolved);
|
|
93
|
+
if (typeof document !== 'undefined') {
|
|
94
|
+
document.documentElement.setAttribute(attribute, newResolved);
|
|
95
|
+
if (newResolved === 'dark') {
|
|
96
|
+
document.documentElement.classList.add('dark');
|
|
97
|
+
document.documentElement.classList.remove('light');
|
|
98
|
+
} else {
|
|
99
|
+
document.documentElement.classList.add('light');
|
|
100
|
+
document.documentElement.classList.remove('dark');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
mediaQuery.addEventListener('change', handleChange);
|
|
106
|
+
return () => mediaQuery.removeEventListener('change', handleChange);
|
|
107
|
+
}, [theme, enableSystem, attribute]);
|
|
108
|
+
|
|
109
|
+
const toggleTheme = () => {
|
|
110
|
+
setThemeState((prev) => {
|
|
111
|
+
if (prev === 'dark') return 'light';
|
|
112
|
+
return 'dark';
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const value = useMemo(
|
|
117
|
+
() => ({
|
|
118
|
+
theme,
|
|
119
|
+
resolvedTheme,
|
|
120
|
+
setTheme: setThemeState,
|
|
121
|
+
toggleTheme,
|
|
122
|
+
}),
|
|
123
|
+
[theme, resolvedTheme]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function useTheme(): ThemeContextType {
|
|
130
|
+
const context = useContext(ThemeContext);
|
|
131
|
+
if (!context) {
|
|
132
|
+
throw new Error('useTheme must be used within a ThemeProvider');
|
|
133
|
+
}
|
|
134
|
+
return context;
|
|
135
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './ThemeProvider';
|
|
2
|
-
export * from '../utils/cn';
|
|
1
|
+
export * from './ThemeProvider';
|
|
2
|
+
export * from '../utils/cn';
|