@checkstack/ui 1.6.0 → 1.7.0
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/CHANGELOG.md +23 -0
- package/package.json +3 -3
- package/src/components/Dialog.tsx +25 -15
- package/src/components/Menu.tsx +76 -0
- package/src/components/Popover.tsx +45 -0
- package/src/components/UserMenu.tsx +99 -59
- package/src/hooks/useIsMobile.ts +21 -0
- package/src/index.ts +3 -1
- package/src/components/DropdownMenu.tsx +0 -187
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# @checkstack/ui
|
|
2
2
|
|
|
3
|
+
## 1.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 32d52c6: Fix several modal/sheet/overlay closing issues:
|
|
8
|
+
|
|
9
|
+
- Replace the custom `DropdownMenu` container with a Radix-based `Popover` (desktop) and `Sheet` (mobile). The previous mobile implementation suppressed outside-click closing, leaving the notification bell's panel only closable by clicking the bell again. `UserMenu` and `NotificationBell` were updated to the new pattern. Leaf primitives `DropdownMenuItem`, `DropdownMenuLabel`, and `DropdownMenuSeparator` are preserved (now backed by a `MenuCloseContext`) so existing call sites continue to work.
|
|
10
|
+
- Fix `Dialog` outside-click closing. The previous structure made `DialogPrimitive.Content` cover the full viewport, so Radix never registered clicks on the dimmed area as "outside" — only ESC could close the modal. The centering wrapper is now a non-Content `<div>` and the actual modal box is the Content, so outside-click closes correctly. A visible X button is now rendered by default; pass `hideCloseButton` to suppress it (e.g. for the search overlay where it would clash with a custom header).
|
|
11
|
+
- Export a standalone `useIsMobile` hook and a new `Popover` primitive.
|
|
12
|
+
- Prevent Radix's auto-focus-return on `NotificationBell` and `UserMenu` overlays. Closing via an item with a `<Link>` (e.g. "View all notifications") would synchronously refocus the trigger via `onCloseAutoFocus`, stealing focus from the link mid-click on pages where another element held focus and requiring a second click to navigate.
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [32d52c6]
|
|
17
|
+
- @checkstack/frontend-api@0.4.1
|
|
18
|
+
|
|
19
|
+
## 1.6.1
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- Updated dependencies [208ad71]
|
|
24
|
+
- @checkstack/frontend-api@0.4.0
|
|
25
|
+
|
|
3
26
|
## 1.6.0
|
|
4
27
|
|
|
5
28
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@checkstack/ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@checkstack/common": "0.
|
|
8
|
-
"@checkstack/frontend-api": "0.
|
|
7
|
+
"@checkstack/common": "0.7.0",
|
|
8
|
+
"@checkstack/frontend-api": "0.4.0",
|
|
9
9
|
"@monaco-editor/react": "^4.7.0",
|
|
10
10
|
"@radix-ui/react-accordion": "^1.2.12",
|
|
11
11
|
"@radix-ui/react-dialog": "^1.1.15",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
|
3
3
|
import { cva, type VariantProps } from "class-variance-authority";
|
|
4
|
+
import { X } from "lucide-react";
|
|
4
5
|
import { cn } from "../utils";
|
|
5
6
|
import { usePerformance } from "./PerformanceProvider";
|
|
6
7
|
|
|
@@ -54,33 +55,42 @@ interface DialogContentProps
|
|
|
54
55
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>,
|
|
55
56
|
VariantProps<typeof dialogContentVariants> {}
|
|
56
57
|
|
|
58
|
+
interface DialogContentExtraProps {
|
|
59
|
+
hideCloseButton?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
57
62
|
const DialogContent = React.forwardRef<
|
|
58
63
|
React.ElementRef<typeof DialogPrimitive.Content>,
|
|
59
|
-
DialogContentProps
|
|
60
|
-
>(({ className, children, size, ...props }, ref) => {
|
|
64
|
+
DialogContentProps & DialogContentExtraProps
|
|
65
|
+
>(({ className, children, size, hideCloseButton, ...props }, ref) => {
|
|
61
66
|
const { isLowPower } = usePerformance();
|
|
62
67
|
return (
|
|
63
68
|
<DialogPortal>
|
|
64
69
|
<DialogOverlay />
|
|
65
|
-
<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"fixed inset-0 z-50 flex items-center justify-center p-4",
|
|
69
|
-
!isLowPower && "duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
|
|
70
|
-
)}
|
|
71
|
-
{...props}
|
|
72
|
-
>
|
|
73
|
-
<div
|
|
70
|
+
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 pointer-events-none">
|
|
71
|
+
<DialogPrimitive.Content
|
|
72
|
+
ref={ref}
|
|
74
73
|
className={cn(
|
|
74
|
+
"pointer-events-auto relative",
|
|
75
75
|
dialogContentVariants({ size }),
|
|
76
|
-
!isLowPower &&
|
|
76
|
+
!isLowPower &&
|
|
77
|
+
"duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
|
|
77
78
|
className,
|
|
78
79
|
)}
|
|
80
|
+
{...props}
|
|
79
81
|
>
|
|
80
|
-
{/* Wrapper with negative margin and positive padding to allow focus rings to extend */}
|
|
81
82
|
<div className="-mx-2 px-2">{children}</div>
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
{!hideCloseButton && (
|
|
84
|
+
<DialogPrimitive.Close
|
|
85
|
+
className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2"
|
|
86
|
+
aria-label="Close"
|
|
87
|
+
>
|
|
88
|
+
<X className="h-4 w-4" />
|
|
89
|
+
<span className="sr-only">Close</span>
|
|
90
|
+
</DialogPrimitive.Close>
|
|
91
|
+
)}
|
|
92
|
+
</DialogPrimitive.Content>
|
|
93
|
+
</div>
|
|
84
94
|
</DialogPortal>
|
|
85
95
|
);
|
|
86
96
|
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React, { createContext, useContext } from "react";
|
|
2
|
+
import { cn } from "../utils";
|
|
3
|
+
|
|
4
|
+
export const MenuCloseContext = createContext<{ onClose?: () => void }>({});
|
|
5
|
+
|
|
6
|
+
export const DropdownMenuItem: React.FC<{
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
onClick?: () => void;
|
|
9
|
+
className?: string;
|
|
10
|
+
icon?: React.ReactNode;
|
|
11
|
+
description?: React.ReactNode;
|
|
12
|
+
closeOnClick?: boolean;
|
|
13
|
+
}> = ({
|
|
14
|
+
children,
|
|
15
|
+
onClick,
|
|
16
|
+
className,
|
|
17
|
+
icon,
|
|
18
|
+
description,
|
|
19
|
+
closeOnClick = true,
|
|
20
|
+
}) => {
|
|
21
|
+
const { onClose } = useContext(MenuCloseContext);
|
|
22
|
+
|
|
23
|
+
const handleClick = () => {
|
|
24
|
+
if (onClick) onClick();
|
|
25
|
+
if (closeOnClick && onClose) onClose();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<button
|
|
30
|
+
onClick={handleClick}
|
|
31
|
+
className={cn(
|
|
32
|
+
"flex flex-col items-start w-full px-4 py-2 text-sm text-popover-foreground hover:bg-accent hover:text-accent-foreground transition-colors rounded-sm overflow-hidden",
|
|
33
|
+
className,
|
|
34
|
+
)}
|
|
35
|
+
role="menuitem"
|
|
36
|
+
type="button"
|
|
37
|
+
>
|
|
38
|
+
<div className="flex items-center w-full overflow-hidden">
|
|
39
|
+
{icon && (
|
|
40
|
+
<span className="mr-3 text-muted-foreground shrink-0">{icon}</span>
|
|
41
|
+
)}
|
|
42
|
+
<span className="flex-1 text-left truncate">{children}</span>
|
|
43
|
+
</div>
|
|
44
|
+
{description && (
|
|
45
|
+
<span
|
|
46
|
+
className={cn(
|
|
47
|
+
"text-[10px] text-muted-foreground mt-1 leading-tight text-left truncate w-full",
|
|
48
|
+
icon ? "pl-7" : "",
|
|
49
|
+
)}
|
|
50
|
+
>
|
|
51
|
+
{description}
|
|
52
|
+
</span>
|
|
53
|
+
)}
|
|
54
|
+
</button>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const DropdownMenuSeparator: React.FC<{ className?: string }> = ({
|
|
59
|
+
className,
|
|
60
|
+
}) => (
|
|
61
|
+
<div className={cn("my-1 h-px bg-border col-span-full", className)} />
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export const DropdownMenuLabel: React.FC<{
|
|
65
|
+
children: React.ReactNode;
|
|
66
|
+
className?: string;
|
|
67
|
+
}> = ({ children, className }) => (
|
|
68
|
+
<div
|
|
69
|
+
className={cn(
|
|
70
|
+
"px-4 py-2 text-xs font-semibold text-muted-foreground uppercase tracking-wider col-span-full",
|
|
71
|
+
className,
|
|
72
|
+
)}
|
|
73
|
+
>
|
|
74
|
+
{children}
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as PopoverPrimitive from "@radix-ui/react-popover";
|
|
3
|
+
import { cn } from "../utils";
|
|
4
|
+
import { usePerformance } from "./PerformanceProvider";
|
|
5
|
+
|
|
6
|
+
const Popover = PopoverPrimitive.Root;
|
|
7
|
+
const PopoverTrigger = PopoverPrimitive.Trigger;
|
|
8
|
+
const PopoverAnchor = PopoverPrimitive.Anchor;
|
|
9
|
+
const PopoverClose = PopoverPrimitive.Close;
|
|
10
|
+
|
|
11
|
+
type PopoverContentProps = React.ComponentPropsWithoutRef<
|
|
12
|
+
typeof PopoverPrimitive.Content
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
const PopoverContent = React.forwardRef<
|
|
16
|
+
React.ElementRef<typeof PopoverPrimitive.Content>,
|
|
17
|
+
PopoverContentProps
|
|
18
|
+
>(({ className, align = "end", sideOffset = 8, ...props }, ref) => {
|
|
19
|
+
const { isLowPower } = usePerformance();
|
|
20
|
+
return (
|
|
21
|
+
<PopoverPrimitive.Portal>
|
|
22
|
+
<PopoverPrimitive.Content
|
|
23
|
+
ref={ref}
|
|
24
|
+
align={align}
|
|
25
|
+
sideOffset={sideOffset}
|
|
26
|
+
className={cn(
|
|
27
|
+
"z-50 rounded-md border border-border bg-popover text-popover-foreground shadow-md outline-none",
|
|
28
|
+
!isLowPower &&
|
|
29
|
+
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
|
|
30
|
+
className,
|
|
31
|
+
)}
|
|
32
|
+
{...props}
|
|
33
|
+
/>
|
|
34
|
+
</PopoverPrimitive.Portal>
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
PopoverContent.displayName = "PopoverContent";
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
Popover,
|
|
41
|
+
PopoverTrigger,
|
|
42
|
+
PopoverAnchor,
|
|
43
|
+
PopoverClose,
|
|
44
|
+
PopoverContent,
|
|
45
|
+
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import React, { useState } from "react";
|
|
1
|
+
import React, { useMemo, useState } from "react";
|
|
2
2
|
import { User, ChevronDown } from "lucide-react";
|
|
3
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./Popover";
|
|
3
4
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./
|
|
5
|
+
Sheet,
|
|
6
|
+
SheetContent,
|
|
7
|
+
SheetHeader,
|
|
8
|
+
SheetTitle,
|
|
9
|
+
SheetTrigger,
|
|
10
|
+
} from "./Sheet";
|
|
11
|
+
import { MenuCloseContext, DropdownMenuLabel, DropdownMenuSeparator } from "./Menu";
|
|
12
|
+
import { useIsMobile } from "../hooks/useIsMobile";
|
|
10
13
|
import { cn } from "../utils";
|
|
11
14
|
|
|
12
15
|
interface UserMenuProps {
|
|
@@ -25,60 +28,97 @@ export const UserMenu: React.FC<UserMenuProps> = ({
|
|
|
25
28
|
className,
|
|
26
29
|
}) => {
|
|
27
30
|
const [isOpen, setIsOpen] = useState(false);
|
|
31
|
+
const isMobile = useIsMobile();
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
</div>
|
|
50
|
-
<span className="text-sm font-medium text-foreground hidden sm:inline-block max-w-[120px] truncate">
|
|
51
|
-
{user.name || user.email}
|
|
52
|
-
</span>
|
|
53
|
-
<ChevronDown
|
|
54
|
-
size={14}
|
|
55
|
-
className={cn(
|
|
56
|
-
"text-muted-foreground transition-transform",
|
|
57
|
-
isOpen && "rotate-180"
|
|
58
|
-
)}
|
|
33
|
+
const closeContextValue = useMemo(
|
|
34
|
+
() => ({ onClose: () => setIsOpen(false) }),
|
|
35
|
+
[],
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const trigger = (
|
|
39
|
+
<button
|
|
40
|
+
className={cn(
|
|
41
|
+
"flex items-center gap-2 px-3 py-1.5 rounded-full hover:bg-accent transition-all border border-transparent hover:border-border",
|
|
42
|
+
isOpen && "bg-accent border-border",
|
|
43
|
+
className,
|
|
44
|
+
)}
|
|
45
|
+
type="button"
|
|
46
|
+
>
|
|
47
|
+
<div className="w-6 h-6 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
|
48
|
+
{user.image ? (
|
|
49
|
+
<img
|
|
50
|
+
src={user.image}
|
|
51
|
+
alt={user.name || "User"}
|
|
52
|
+
className="w-full h-full rounded-full object-cover"
|
|
59
53
|
/>
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
) : (
|
|
55
|
+
<User size={14} />
|
|
56
|
+
)}
|
|
57
|
+
</div>
|
|
58
|
+
<span className="text-sm font-medium text-foreground hidden sm:inline-block max-w-[120px] truncate">
|
|
59
|
+
{user.name || user.email}
|
|
60
|
+
</span>
|
|
61
|
+
<ChevronDown
|
|
62
|
+
size={14}
|
|
63
|
+
className={cn(
|
|
64
|
+
"text-muted-foreground transition-transform",
|
|
65
|
+
isOpen && "rotate-180",
|
|
66
|
+
)}
|
|
67
|
+
/>
|
|
68
|
+
</button>
|
|
69
|
+
);
|
|
62
70
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
className="
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
<
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
<
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
const userInfo = (
|
|
72
|
+
<DropdownMenuLabel>
|
|
73
|
+
<div className="flex flex-col">
|
|
74
|
+
<span className="text-sm font-bold text-foreground truncate">
|
|
75
|
+
{user.name || "User"}
|
|
76
|
+
</span>
|
|
77
|
+
<span className="text-xs font-normal text-muted-foreground truncate">
|
|
78
|
+
{user.email}
|
|
79
|
+
</span>
|
|
80
|
+
</div>
|
|
81
|
+
</DropdownMenuLabel>
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (isMobile) {
|
|
85
|
+
return (
|
|
86
|
+
<MenuCloseContext.Provider value={closeContextValue}>
|
|
87
|
+
<Sheet open={isOpen} onOpenChange={setIsOpen}>
|
|
88
|
+
<SheetTrigger asChild>{trigger}</SheetTrigger>
|
|
89
|
+
<SheetContent
|
|
90
|
+
size="full"
|
|
91
|
+
className="flex flex-col p-0"
|
|
92
|
+
onCloseAutoFocus={(e) => e.preventDefault()}
|
|
93
|
+
>
|
|
94
|
+
<SheetHeader className="px-4 py-3 border-b border-border">
|
|
95
|
+
<SheetTitle className="text-base">Menu</SheetTitle>
|
|
96
|
+
</SheetHeader>
|
|
97
|
+
<div className="flex-1 overflow-y-auto p-2 grid grid-cols-1 gap-2">
|
|
98
|
+
{userInfo}
|
|
99
|
+
<DropdownMenuSeparator />
|
|
100
|
+
{children}
|
|
101
|
+
</div>
|
|
102
|
+
</SheetContent>
|
|
103
|
+
</Sheet>
|
|
104
|
+
</MenuCloseContext.Provider>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<MenuCloseContext.Provider value={closeContextValue}>
|
|
110
|
+
<Popover open={isOpen} onOpenChange={setIsOpen}>
|
|
111
|
+
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
|
|
112
|
+
<PopoverContent
|
|
113
|
+
align="end"
|
|
114
|
+
className="w-[400px] md:w-[460px] p-2 grid grid-cols-1 sm:grid-cols-2 gap-1"
|
|
115
|
+
onCloseAutoFocus={(e) => e.preventDefault()}
|
|
116
|
+
>
|
|
117
|
+
{userInfo}
|
|
118
|
+
<DropdownMenuSeparator />
|
|
119
|
+
{children}
|
|
120
|
+
</PopoverContent>
|
|
121
|
+
</Popover>
|
|
122
|
+
</MenuCloseContext.Provider>
|
|
83
123
|
);
|
|
84
124
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
const MOBILE_BREAKPOINT_QUERY = "(max-width: 640px)";
|
|
4
|
+
|
|
5
|
+
export function useIsMobile() {
|
|
6
|
+
const [isMobile, setIsMobile] = useState(false);
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const mql = globalThis.matchMedia(MOBILE_BREAKPOINT_QUERY);
|
|
10
|
+
setIsMobile(mql.matches);
|
|
11
|
+
const handler = (e: MediaQueryListEvent) => {
|
|
12
|
+
setIsMobile(e.matches);
|
|
13
|
+
};
|
|
14
|
+
mql.addEventListener("change", handler);
|
|
15
|
+
return () => {
|
|
16
|
+
mql.removeEventListener("change", handler);
|
|
17
|
+
};
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
return isMobile;
|
|
21
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export * from "./components/SectionHeader";
|
|
|
10
10
|
export * from "./components/StatusCard";
|
|
11
11
|
export * from "./components/EmptyState";
|
|
12
12
|
export * from "./components/LoadingSpinner";
|
|
13
|
-
export * from "./components/
|
|
13
|
+
export * from "./components/Menu";
|
|
14
14
|
export * from "./components/UserMenu";
|
|
15
15
|
export * from "./components/EditableText";
|
|
16
16
|
export * from "./components/ConfirmationModal";
|
|
@@ -59,3 +59,5 @@ export * from "./hooks/useAnimatedNumber";
|
|
|
59
59
|
export * from "./components/IDELayout";
|
|
60
60
|
export * from "./components/MetricTile";
|
|
61
61
|
export * from "./components/Sheet";
|
|
62
|
+
export * from "./components/Popover";
|
|
63
|
+
export * from "./hooks/useIsMobile";
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import React, { useRef, useEffect, createContext, useContext, useState } from "react";
|
|
2
|
-
import { createPortal } from "react-dom";
|
|
3
|
-
import { X } from "lucide-react";
|
|
4
|
-
import { cn } from "../utils";
|
|
5
|
-
import { usePerformance } from "./PerformanceProvider";
|
|
6
|
-
|
|
7
|
-
export const DropdownMenuContext = createContext<{ onClose?: () => void }>({});
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Custom hook to detect mobile viewport.
|
|
11
|
-
*/
|
|
12
|
-
export function useIsMobile() {
|
|
13
|
-
const [isMobile, setIsMobile] = useState(false);
|
|
14
|
-
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
// Initial check
|
|
17
|
-
const mql = globalThis.matchMedia("(max-width: 640px)");
|
|
18
|
-
setIsMobile(mql.matches);
|
|
19
|
-
|
|
20
|
-
// Listener for changes
|
|
21
|
-
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
|
|
22
|
-
mql.addEventListener("change", handler);
|
|
23
|
-
return () => mql.removeEventListener("change", handler);
|
|
24
|
-
}, []);
|
|
25
|
-
|
|
26
|
-
return isMobile;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export const DropdownMenuRootContext = createContext<{ rootRef?: React.RefObject<HTMLDivElement> }>({});
|
|
31
|
-
|
|
32
|
-
export const DropdownMenu: React.FC<{ children: React.ReactNode }> = ({
|
|
33
|
-
children,
|
|
34
|
-
}) => {
|
|
35
|
-
const rootRef = useRef<HTMLDivElement>(null);
|
|
36
|
-
return (
|
|
37
|
-
<DropdownMenuRootContext.Provider value={{ rootRef }}>
|
|
38
|
-
<div ref={rootRef} className="relative inline-block text-left">{children}</div>
|
|
39
|
-
</DropdownMenuRootContext.Provider>
|
|
40
|
-
);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const DropdownMenuTrigger: React.FC<{
|
|
44
|
-
children: React.ReactNode;
|
|
45
|
-
asChild?: boolean;
|
|
46
|
-
onClick?: () => void;
|
|
47
|
-
}> = ({ children, onClick }) => {
|
|
48
|
-
return (
|
|
49
|
-
<div onClick={onClick} className="cursor-pointer">
|
|
50
|
-
{children}
|
|
51
|
-
</div>
|
|
52
|
-
);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const DropdownMenuContent: React.FC<{
|
|
56
|
-
children: React.ReactNode;
|
|
57
|
-
isOpen: boolean;
|
|
58
|
-
onClose: () => void;
|
|
59
|
-
className?: string;
|
|
60
|
-
innerClassName?: string;
|
|
61
|
-
}> = ({ children, isOpen, onClose, className, innerClassName }) => {
|
|
62
|
-
const { isLowPower } = usePerformance();
|
|
63
|
-
const contentRef = useRef<HTMLDivElement>(null);
|
|
64
|
-
const { rootRef } = useContext(DropdownMenuRootContext);
|
|
65
|
-
const isMobile = useIsMobile();
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
const handleClickOutside = (event: MouseEvent) => {
|
|
69
|
-
// Don't auto-close if clicking outside on mobile (full screen modal style)
|
|
70
|
-
if (isMobile) return;
|
|
71
|
-
|
|
72
|
-
const target = event.target as Node;
|
|
73
|
-
|
|
74
|
-
// Don't close if clicking inside the menu content
|
|
75
|
-
if (contentRef.current && contentRef.current.contains(target)) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Don't close if clicking inside the wrapper (e.g. the trigger button)
|
|
80
|
-
// This allows the trigger's onClick to handle toggling the menu without conflict.
|
|
81
|
-
if (rootRef?.current && rootRef.current.contains(target)) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
onClose();
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
if (isOpen) {
|
|
89
|
-
document.addEventListener("mousedown", handleClickOutside);
|
|
90
|
-
// Lock body scroll on mobile
|
|
91
|
-
if (isMobile) {
|
|
92
|
-
document.body.style.overflow = "hidden";
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return () => {
|
|
96
|
-
document.removeEventListener("mousedown", handleClickOutside);
|
|
97
|
-
if (isMobile) {
|
|
98
|
-
document.body.style.overflow = "";
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}, [isOpen, onClose, isMobile, rootRef]);
|
|
102
|
-
|
|
103
|
-
if (!isOpen) return <React.Fragment />;
|
|
104
|
-
|
|
105
|
-
const content = (
|
|
106
|
-
<div
|
|
107
|
-
ref={contentRef}
|
|
108
|
-
className={cn(
|
|
109
|
-
isMobile
|
|
110
|
-
? "fixed inset-0 z-[100] bg-background w-full h-full p-4 overflow-y-auto"
|
|
111
|
-
: "absolute right-0 mt-2 w-56 origin-top-right rounded-md bg-popover shadow-lg ring-1 ring-border focus:outline-none z-[100] max-h-[calc(100vh-4rem)] overflow-y-auto",
|
|
112
|
-
!isLowPower && "animate-in fade-in zoom-in-95 duration-100",
|
|
113
|
-
className
|
|
114
|
-
)}
|
|
115
|
-
>
|
|
116
|
-
<DropdownMenuContext.Provider value={{ onClose }}>
|
|
117
|
-
<div className={cn(isMobile ? "flex flex-col pt-4 pb-12" : "py-1", innerClassName)} role="none">
|
|
118
|
-
{isMobile && (
|
|
119
|
-
<div className="flex justify-between items-center mb-6 col-span-full px-2">
|
|
120
|
-
<h2 className="text-xl font-bold text-foreground">Menu</h2>
|
|
121
|
-
<button
|
|
122
|
-
onClick={onClose}
|
|
123
|
-
className="p-2 rounded-full hover:bg-accent text-muted-foreground transition-colors"
|
|
124
|
-
aria-label="Close menu"
|
|
125
|
-
>
|
|
126
|
-
<X className="w-6 h-6" />
|
|
127
|
-
</button>
|
|
128
|
-
</div>
|
|
129
|
-
)}
|
|
130
|
-
{children}
|
|
131
|
-
</div>
|
|
132
|
-
</DropdownMenuContext.Provider>
|
|
133
|
-
</div>
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
return isMobile ? createPortal(content, document.body) : content;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export const DropdownMenuItem: React.FC<{
|
|
140
|
-
children: React.ReactNode;
|
|
141
|
-
onClick?: () => void;
|
|
142
|
-
className?: string;
|
|
143
|
-
icon?: React.ReactNode;
|
|
144
|
-
description?: React.ReactNode;
|
|
145
|
-
closeOnClick?: boolean;
|
|
146
|
-
}> = ({ children, onClick, className, icon, description, closeOnClick = true }) => {
|
|
147
|
-
const { onClose } = useContext(DropdownMenuContext);
|
|
148
|
-
|
|
149
|
-
const handleClick = () => {
|
|
150
|
-
if (onClick) onClick();
|
|
151
|
-
if (closeOnClick && onClose) onClose();
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
return (
|
|
155
|
-
<button
|
|
156
|
-
onClick={handleClick}
|
|
157
|
-
className={cn(
|
|
158
|
-
"flex flex-col items-start w-full px-4 py-2 text-sm text-popover-foreground hover:bg-accent hover:text-accent-foreground transition-colors rounded-sm overflow-hidden",
|
|
159
|
-
className
|
|
160
|
-
)}
|
|
161
|
-
role="menuitem"
|
|
162
|
-
>
|
|
163
|
-
<div className="flex items-center w-full overflow-hidden">
|
|
164
|
-
{icon && <span className="mr-3 text-muted-foreground shrink-0">{icon}</span>}
|
|
165
|
-
<span className="flex-1 text-left truncate">{children}</span>
|
|
166
|
-
</div>
|
|
167
|
-
{description && (
|
|
168
|
-
<span className={cn("text-[10px] text-muted-foreground mt-1 leading-tight text-left truncate w-full", icon ? "pl-7" : "")}>
|
|
169
|
-
{description}
|
|
170
|
-
</span>
|
|
171
|
-
)}
|
|
172
|
-
</button>
|
|
173
|
-
);
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export const DropdownMenuSeparator: React.FC<{ className?: string }> = ({ className }) => (
|
|
177
|
-
<div className={cn("my-1 h-px bg-border col-span-full", className)} />
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
export const DropdownMenuLabel: React.FC<{ children: React.ReactNode; className?: string }> = ({
|
|
181
|
-
children,
|
|
182
|
-
className
|
|
183
|
-
}) => (
|
|
184
|
-
<div className={cn("px-4 py-2 text-xs font-semibold text-muted-foreground uppercase tracking-wider col-span-full", className)}>
|
|
185
|
-
{children}
|
|
186
|
-
</div>
|
|
187
|
-
);
|