@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,244 +1,244 @@
|
|
|
1
|
-
import React, { createContext, useContext, useState, useRef, useEffect, forwardRef, useId } from 'react';
|
|
2
|
-
import { Portal } from '../overlays/Portal';
|
|
3
|
-
import { cn } from '../utils/cn';
|
|
4
|
-
|
|
5
|
-
interface DropdownContextType {
|
|
6
|
-
isOpen: boolean;
|
|
7
|
-
setIsOpen: (val: boolean) => void;
|
|
8
|
-
triggerRect: DOMRect | null;
|
|
9
|
-
setTriggerRect: (rect: DOMRect | null) => void;
|
|
10
|
-
triggerRef: React.RefObject<HTMLDivElement | null>;
|
|
11
|
-
menuId: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const DropdownContext = createContext<DropdownContextType | null>(null);
|
|
15
|
-
|
|
16
|
-
export function useDropdown() {
|
|
17
|
-
const context = useContext(DropdownContext);
|
|
18
|
-
if (!context) throw new Error('DropdownMenu components must be used within <DropdownMenu />');
|
|
19
|
-
return context;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface DropdownMenuProps {
|
|
23
|
-
children: React.ReactNode;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function DropdownMenu({ children }: DropdownMenuProps) {
|
|
27
|
-
const [isOpen, setIsOpen] = useState(false);
|
|
28
|
-
const [triggerRect, setTriggerRect] = useState<DOMRect | null>(null);
|
|
29
|
-
const triggerRef = useRef<HTMLDivElement | null>(null);
|
|
30
|
-
const menuId = useId();
|
|
31
|
-
|
|
32
|
-
return (
|
|
33
|
-
<DropdownContext.Provider value={{ isOpen, setIsOpen, triggerRect, setTriggerRect, triggerRef, menuId }}>
|
|
34
|
-
<div className="relative inline-block text-left">{children}</div>
|
|
35
|
-
</DropdownContext.Provider>
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export const DropdownMenuTrigger = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
40
|
-
({ children, onClick, onKeyDown, ...props }, ref) => {
|
|
41
|
-
const { isOpen, setIsOpen, setTriggerRect, triggerRef, menuId } = useDropdown();
|
|
42
|
-
|
|
43
|
-
const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
|
44
|
-
if (triggerRef.current) {
|
|
45
|
-
setTriggerRect(triggerRef.current.getBoundingClientRect());
|
|
46
|
-
}
|
|
47
|
-
setIsOpen(!isOpen);
|
|
48
|
-
onClick?.(e);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
52
|
-
onKeyDown?.(e);
|
|
53
|
-
if (e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') {
|
|
54
|
-
e.preventDefault();
|
|
55
|
-
if (triggerRef.current) {
|
|
56
|
-
setTriggerRect(triggerRef.current.getBoundingClientRect());
|
|
57
|
-
}
|
|
58
|
-
setIsOpen(true);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
return (
|
|
63
|
-
<div
|
|
64
|
-
ref={(node) => {
|
|
65
|
-
(triggerRef as any).current = node;
|
|
66
|
-
if (typeof ref === 'function') ref(node);
|
|
67
|
-
else if (ref) (ref as any).current = node;
|
|
68
|
-
}}
|
|
69
|
-
onClick={handleClick}
|
|
70
|
-
onKeyDown={handleKeyDown}
|
|
71
|
-
role="button"
|
|
72
|
-
tabIndex={0}
|
|
73
|
-
aria-haspopup="menu"
|
|
74
|
-
aria-expanded={isOpen}
|
|
75
|
-
aria-controls={isOpen ? `menu-${menuId}` : undefined}
|
|
76
|
-
className="inline-flex cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ui-primary)] rounded-[var(--ui-radius-sm)]"
|
|
77
|
-
{...props}
|
|
78
|
-
>
|
|
79
|
-
{children}
|
|
80
|
-
</div>
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger';
|
|
85
|
-
|
|
86
|
-
export interface DropdownMenuContentProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
87
|
-
align?: 'start' | 'end';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export const DropdownMenuContent = forwardRef<HTMLDivElement, DropdownMenuContentProps>(
|
|
91
|
-
({ className, align = 'start', children, ...props }, ref) => {
|
|
92
|
-
const { isOpen, setIsOpen, triggerRect, triggerRef, menuId } = useDropdown();
|
|
93
|
-
const menuRef = useRef<HTMLDivElement>(null);
|
|
94
|
-
|
|
95
|
-
useEffect(() => {
|
|
96
|
-
if (!isOpen) return;
|
|
97
|
-
|
|
98
|
-
// Auto-focus first menuitem
|
|
99
|
-
const timer = setTimeout(() => {
|
|
100
|
-
const firstItem = menuRef.current?.querySelector<HTMLButtonElement>('[role="menuitem"]:not([disabled])');
|
|
101
|
-
firstItem?.focus();
|
|
102
|
-
}, 10);
|
|
103
|
-
|
|
104
|
-
const handleOutside = (e: MouseEvent) => {
|
|
105
|
-
if (
|
|
106
|
-
menuRef.current &&
|
|
107
|
-
!menuRef.current.contains(e.target as Node) &&
|
|
108
|
-
!triggerRef.current?.contains(e.target as Node)
|
|
109
|
-
) {
|
|
110
|
-
setIsOpen(false);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const handleKeyDown = (e: KeyboardEvent) => {
|
|
115
|
-
if (!menuRef.current) return;
|
|
116
|
-
const items = Array.from(
|
|
117
|
-
menuRef.current.querySelectorAll<HTMLButtonElement>('[role="menuitem"]:not([disabled])')
|
|
118
|
-
);
|
|
119
|
-
if (items.length === 0) return;
|
|
120
|
-
|
|
121
|
-
const currentIndex = items.indexOf(document.activeElement as HTMLButtonElement);
|
|
122
|
-
|
|
123
|
-
if (e.key === 'ArrowDown') {
|
|
124
|
-
e.preventDefault();
|
|
125
|
-
const nextIndex = currentIndex === -1 ? 0 : (currentIndex + 1) % items.length;
|
|
126
|
-
items[nextIndex]?.focus();
|
|
127
|
-
} else if (e.key === 'ArrowUp') {
|
|
128
|
-
e.preventDefault();
|
|
129
|
-
const prevIndex = currentIndex === -1 ? items.length - 1 : (currentIndex - 1 + items.length) % items.length;
|
|
130
|
-
items[prevIndex]?.focus();
|
|
131
|
-
} else if (e.key === 'Home') {
|
|
132
|
-
e.preventDefault();
|
|
133
|
-
items[0]?.focus();
|
|
134
|
-
} else if (e.key === 'End') {
|
|
135
|
-
e.preventDefault();
|
|
136
|
-
items[items.length - 1]?.focus();
|
|
137
|
-
} else if (e.key === 'Escape') {
|
|
138
|
-
e.preventDefault();
|
|
139
|
-
setIsOpen(false);
|
|
140
|
-
triggerRef.current?.focus();
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
document.addEventListener('mousedown', handleOutside);
|
|
145
|
-
window.addEventListener('keydown', handleKeyDown);
|
|
146
|
-
|
|
147
|
-
return () => {
|
|
148
|
-
clearTimeout(timer);
|
|
149
|
-
document.removeEventListener('mousedown', handleOutside);
|
|
150
|
-
window.removeEventListener('keydown', handleKeyDown);
|
|
151
|
-
};
|
|
152
|
-
}, [isOpen, setIsOpen, triggerRef]);
|
|
153
|
-
|
|
154
|
-
if (!isOpen || !triggerRect) return null;
|
|
155
|
-
|
|
156
|
-
const top = triggerRect.bottom + window.scrollY + 6;
|
|
157
|
-
const left =
|
|
158
|
-
align === 'end'
|
|
159
|
-
? triggerRect.right + window.scrollX
|
|
160
|
-
: triggerRect.left + window.scrollX;
|
|
161
|
-
|
|
162
|
-
return (
|
|
163
|
-
<Portal>
|
|
164
|
-
<div
|
|
165
|
-
ref={(node) => {
|
|
166
|
-
(menuRef as any).current = node;
|
|
167
|
-
if (typeof ref === 'function') ref(node);
|
|
168
|
-
else if (ref) (ref as any).current = node;
|
|
169
|
-
}}
|
|
170
|
-
id={`menu-${menuId}`}
|
|
171
|
-
role="menu"
|
|
172
|
-
tabIndex={-1}
|
|
173
|
-
style={{
|
|
174
|
-
position: 'absolute',
|
|
175
|
-
top: `${top}px`,
|
|
176
|
-
left: `${left}px`,
|
|
177
|
-
}}
|
|
178
|
-
className={cn(
|
|
179
|
-
'z-50 min-w-[12rem] overflow-hidden rounded-[var(--ui-radius-lg)] border border-[var(--ui-border-default)] bg-[var(--ui-bg-surface)] p-1 text-[var(--ui-text-primary)] shadow-xl animate-in fade-in zoom-in-95 focus:outline-none',
|
|
180
|
-
align === 'end' && '-translate-x-full',
|
|
181
|
-
className
|
|
182
|
-
)}
|
|
183
|
-
{...props}
|
|
184
|
-
>
|
|
185
|
-
{children}
|
|
186
|
-
</div>
|
|
187
|
-
</Portal>
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
);
|
|
191
|
-
DropdownMenuContent.displayName = 'DropdownMenuContent';
|
|
192
|
-
|
|
193
|
-
export interface DropdownMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
194
|
-
icon?: React.ReactNode;
|
|
195
|
-
shortcut?: string;
|
|
196
|
-
destructive?: boolean;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export const DropdownMenuItem = forwardRef<HTMLButtonElement, DropdownMenuItemProps>(
|
|
200
|
-
({ className, icon, shortcut, destructive = false, children, onClick, ...props }, ref) => {
|
|
201
|
-
const { setIsOpen } = useDropdown();
|
|
202
|
-
|
|
203
|
-
return (
|
|
204
|
-
<button
|
|
205
|
-
ref={ref}
|
|
206
|
-
role="menuitem"
|
|
207
|
-
type="button"
|
|
208
|
-
onClick={(e) => {
|
|
209
|
-
onClick?.(e);
|
|
210
|
-
setIsOpen(false);
|
|
211
|
-
}}
|
|
212
|
-
className={cn(
|
|
213
|
-
'relative flex w-full cursor-pointer select-none items-center gap-2 rounded-[var(--ui-radius-sm)] px-2.5 py-1.5 text-xs font-medium outline-none transition-colors hover:bg-[var(--ui-bg-tertiary)] focus:bg-[var(--ui-bg-tertiary)] disabled:pointer-events-none disabled:opacity-50 text-left',
|
|
214
|
-
destructive && 'text-[var(--ui-error)] hover:bg-[var(--ui-error-subtle)] focus:bg-[var(--ui-error-subtle)]',
|
|
215
|
-
className
|
|
216
|
-
)}
|
|
217
|
-
{...props}
|
|
218
|
-
>
|
|
219
|
-
{icon && <span className="flex-shrink-0 text-current">{icon}</span>}
|
|
220
|
-
<span className="flex-1 truncate">{children}</span>
|
|
221
|
-
{shortcut && <span className="ml-auto text-[10px] text-[var(--ui-text-muted)] tracking-widest">{shortcut}</span>}
|
|
222
|
-
</button>
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
);
|
|
226
|
-
DropdownMenuItem.displayName = 'DropdownMenuItem';
|
|
227
|
-
|
|
228
|
-
export const DropdownMenuSeparator = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
229
|
-
({ className, ...props }, ref) => {
|
|
230
|
-
return <div ref={ref} role="separator" className={cn('-mx-1 my-1 h-px bg-[var(--ui-border-subtle)]', className)} {...props} />;
|
|
231
|
-
}
|
|
232
|
-
);
|
|
233
|
-
DropdownMenuSeparator.displayName = 'DropdownMenuSeparator';
|
|
234
|
-
|
|
235
|
-
export const DropdownMenuLabel = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
236
|
-
({ className, children, ...props }, ref) => {
|
|
237
|
-
return (
|
|
238
|
-
<div ref={ref} className={cn('px-2.5 py-1.5 text-[11px] font-semibold text-[var(--ui-text-muted)] uppercase tracking-wider', className)} {...props}>
|
|
239
|
-
{children}
|
|
240
|
-
</div>
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
);
|
|
244
|
-
DropdownMenuLabel.displayName = 'DropdownMenuLabel';
|
|
1
|
+
import React, { createContext, useContext, useState, useRef, useEffect, forwardRef, useId } from 'react';
|
|
2
|
+
import { Portal } from '../overlays/Portal';
|
|
3
|
+
import { cn } from '../utils/cn';
|
|
4
|
+
|
|
5
|
+
interface DropdownContextType {
|
|
6
|
+
isOpen: boolean;
|
|
7
|
+
setIsOpen: (val: boolean) => void;
|
|
8
|
+
triggerRect: DOMRect | null;
|
|
9
|
+
setTriggerRect: (rect: DOMRect | null) => void;
|
|
10
|
+
triggerRef: React.RefObject<HTMLDivElement | null>;
|
|
11
|
+
menuId: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const DropdownContext = createContext<DropdownContextType | null>(null);
|
|
15
|
+
|
|
16
|
+
export function useDropdown() {
|
|
17
|
+
const context = useContext(DropdownContext);
|
|
18
|
+
if (!context) throw new Error('DropdownMenu components must be used within <DropdownMenu />');
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface DropdownMenuProps {
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function DropdownMenu({ children }: DropdownMenuProps) {
|
|
27
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
28
|
+
const [triggerRect, setTriggerRect] = useState<DOMRect | null>(null);
|
|
29
|
+
const triggerRef = useRef<HTMLDivElement | null>(null);
|
|
30
|
+
const menuId = useId();
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<DropdownContext.Provider value={{ isOpen, setIsOpen, triggerRect, setTriggerRect, triggerRef, menuId }}>
|
|
34
|
+
<div className="relative inline-block text-left">{children}</div>
|
|
35
|
+
</DropdownContext.Provider>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const DropdownMenuTrigger = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
40
|
+
({ children, onClick, onKeyDown, ...props }, ref) => {
|
|
41
|
+
const { isOpen, setIsOpen, setTriggerRect, triggerRef, menuId } = useDropdown();
|
|
42
|
+
|
|
43
|
+
const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
|
44
|
+
if (triggerRef.current) {
|
|
45
|
+
setTriggerRect(triggerRef.current.getBoundingClientRect());
|
|
46
|
+
}
|
|
47
|
+
setIsOpen(!isOpen);
|
|
48
|
+
onClick?.(e);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
52
|
+
onKeyDown?.(e);
|
|
53
|
+
if (e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
if (triggerRef.current) {
|
|
56
|
+
setTriggerRect(triggerRef.current.getBoundingClientRect());
|
|
57
|
+
}
|
|
58
|
+
setIsOpen(true);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<div
|
|
64
|
+
ref={(node) => {
|
|
65
|
+
(triggerRef as any).current = node;
|
|
66
|
+
if (typeof ref === 'function') ref(node);
|
|
67
|
+
else if (ref) (ref as any).current = node;
|
|
68
|
+
}}
|
|
69
|
+
onClick={handleClick}
|
|
70
|
+
onKeyDown={handleKeyDown}
|
|
71
|
+
role="button"
|
|
72
|
+
tabIndex={0}
|
|
73
|
+
aria-haspopup="menu"
|
|
74
|
+
aria-expanded={isOpen}
|
|
75
|
+
aria-controls={isOpen ? `menu-${menuId}` : undefined}
|
|
76
|
+
className="inline-flex cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ui-primary)] rounded-[var(--ui-radius-sm)]"
|
|
77
|
+
{...props}
|
|
78
|
+
>
|
|
79
|
+
{children}
|
|
80
|
+
</div>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger';
|
|
85
|
+
|
|
86
|
+
export interface DropdownMenuContentProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
87
|
+
align?: 'start' | 'end';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const DropdownMenuContent = forwardRef<HTMLDivElement, DropdownMenuContentProps>(
|
|
91
|
+
({ className, align = 'start', children, ...props }, ref) => {
|
|
92
|
+
const { isOpen, setIsOpen, triggerRect, triggerRef, menuId } = useDropdown();
|
|
93
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!isOpen) return;
|
|
97
|
+
|
|
98
|
+
// Auto-focus first menuitem
|
|
99
|
+
const timer = setTimeout(() => {
|
|
100
|
+
const firstItem = menuRef.current?.querySelector<HTMLButtonElement>('[role="menuitem"]:not([disabled])');
|
|
101
|
+
firstItem?.focus();
|
|
102
|
+
}, 10);
|
|
103
|
+
|
|
104
|
+
const handleOutside = (e: MouseEvent) => {
|
|
105
|
+
if (
|
|
106
|
+
menuRef.current &&
|
|
107
|
+
!menuRef.current.contains(e.target as Node) &&
|
|
108
|
+
!triggerRef.current?.contains(e.target as Node)
|
|
109
|
+
) {
|
|
110
|
+
setIsOpen(false);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const handleKeyDown = (e: KeyboardEvent) => {
|
|
115
|
+
if (!menuRef.current) return;
|
|
116
|
+
const items = Array.from(
|
|
117
|
+
menuRef.current.querySelectorAll<HTMLButtonElement>('[role="menuitem"]:not([disabled])')
|
|
118
|
+
);
|
|
119
|
+
if (items.length === 0) return;
|
|
120
|
+
|
|
121
|
+
const currentIndex = items.indexOf(document.activeElement as HTMLButtonElement);
|
|
122
|
+
|
|
123
|
+
if (e.key === 'ArrowDown') {
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
const nextIndex = currentIndex === -1 ? 0 : (currentIndex + 1) % items.length;
|
|
126
|
+
items[nextIndex]?.focus();
|
|
127
|
+
} else if (e.key === 'ArrowUp') {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
const prevIndex = currentIndex === -1 ? items.length - 1 : (currentIndex - 1 + items.length) % items.length;
|
|
130
|
+
items[prevIndex]?.focus();
|
|
131
|
+
} else if (e.key === 'Home') {
|
|
132
|
+
e.preventDefault();
|
|
133
|
+
items[0]?.focus();
|
|
134
|
+
} else if (e.key === 'End') {
|
|
135
|
+
e.preventDefault();
|
|
136
|
+
items[items.length - 1]?.focus();
|
|
137
|
+
} else if (e.key === 'Escape') {
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
setIsOpen(false);
|
|
140
|
+
triggerRef.current?.focus();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
document.addEventListener('mousedown', handleOutside);
|
|
145
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
146
|
+
|
|
147
|
+
return () => {
|
|
148
|
+
clearTimeout(timer);
|
|
149
|
+
document.removeEventListener('mousedown', handleOutside);
|
|
150
|
+
window.removeEventListener('keydown', handleKeyDown);
|
|
151
|
+
};
|
|
152
|
+
}, [isOpen, setIsOpen, triggerRef]);
|
|
153
|
+
|
|
154
|
+
if (!isOpen || !triggerRect) return null;
|
|
155
|
+
|
|
156
|
+
const top = triggerRect.bottom + window.scrollY + 6;
|
|
157
|
+
const left =
|
|
158
|
+
align === 'end'
|
|
159
|
+
? triggerRect.right + window.scrollX
|
|
160
|
+
: triggerRect.left + window.scrollX;
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<Portal>
|
|
164
|
+
<div
|
|
165
|
+
ref={(node) => {
|
|
166
|
+
(menuRef as any).current = node;
|
|
167
|
+
if (typeof ref === 'function') ref(node);
|
|
168
|
+
else if (ref) (ref as any).current = node;
|
|
169
|
+
}}
|
|
170
|
+
id={`menu-${menuId}`}
|
|
171
|
+
role="menu"
|
|
172
|
+
tabIndex={-1}
|
|
173
|
+
style={{
|
|
174
|
+
position: 'absolute',
|
|
175
|
+
top: `${top}px`,
|
|
176
|
+
left: `${left}px`,
|
|
177
|
+
}}
|
|
178
|
+
className={cn(
|
|
179
|
+
'z-50 min-w-[12rem] overflow-hidden rounded-[var(--ui-radius-lg)] border border-[var(--ui-border-default)] bg-[var(--ui-bg-surface)] p-1 text-[var(--ui-text-primary)] shadow-xl animate-in fade-in zoom-in-95 focus:outline-none',
|
|
180
|
+
align === 'end' && '-translate-x-full',
|
|
181
|
+
className
|
|
182
|
+
)}
|
|
183
|
+
{...props}
|
|
184
|
+
>
|
|
185
|
+
{children}
|
|
186
|
+
</div>
|
|
187
|
+
</Portal>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
DropdownMenuContent.displayName = 'DropdownMenuContent';
|
|
192
|
+
|
|
193
|
+
export interface DropdownMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
194
|
+
icon?: React.ReactNode;
|
|
195
|
+
shortcut?: string;
|
|
196
|
+
destructive?: boolean;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export const DropdownMenuItem = forwardRef<HTMLButtonElement, DropdownMenuItemProps>(
|
|
200
|
+
({ className, icon, shortcut, destructive = false, children, onClick, ...props }, ref) => {
|
|
201
|
+
const { setIsOpen } = useDropdown();
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<button
|
|
205
|
+
ref={ref}
|
|
206
|
+
role="menuitem"
|
|
207
|
+
type="button"
|
|
208
|
+
onClick={(e) => {
|
|
209
|
+
onClick?.(e);
|
|
210
|
+
setIsOpen(false);
|
|
211
|
+
}}
|
|
212
|
+
className={cn(
|
|
213
|
+
'relative flex w-full cursor-pointer select-none items-center gap-2 rounded-[var(--ui-radius-sm)] px-2.5 py-1.5 text-xs font-medium outline-none transition-colors hover:bg-[var(--ui-bg-tertiary)] focus:bg-[var(--ui-bg-tertiary)] disabled:pointer-events-none disabled:opacity-50 text-left',
|
|
214
|
+
destructive && 'text-[var(--ui-error)] hover:bg-[var(--ui-error-subtle)] focus:bg-[var(--ui-error-subtle)]',
|
|
215
|
+
className
|
|
216
|
+
)}
|
|
217
|
+
{...props}
|
|
218
|
+
>
|
|
219
|
+
{icon && <span className="flex-shrink-0 text-current">{icon}</span>}
|
|
220
|
+
<span className="flex-1 truncate">{children}</span>
|
|
221
|
+
{shortcut && <span className="ml-auto text-[10px] text-[var(--ui-text-muted)] tracking-widest">{shortcut}</span>}
|
|
222
|
+
</button>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
DropdownMenuItem.displayName = 'DropdownMenuItem';
|
|
227
|
+
|
|
228
|
+
export const DropdownMenuSeparator = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
229
|
+
({ className, ...props }, ref) => {
|
|
230
|
+
return <div ref={ref} role="separator" className={cn('-mx-1 my-1 h-px bg-[var(--ui-border-subtle)]', className)} {...props} />;
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
DropdownMenuSeparator.displayName = 'DropdownMenuSeparator';
|
|
234
|
+
|
|
235
|
+
export const DropdownMenuLabel = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
|
|
236
|
+
({ className, children, ...props }, ref) => {
|
|
237
|
+
return (
|
|
238
|
+
<div ref={ref} className={cn('px-2.5 py-1.5 text-[11px] font-semibold text-[var(--ui-text-muted)] uppercase tracking-wider', className)} {...props}>
|
|
239
|
+
{children}
|
|
240
|
+
</div>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
DropdownMenuLabel.displayName = 'DropdownMenuLabel';
|