mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 09:51:28 +00:00
Replaces popover components with dropdowns for menus
Switches UI menu interactions from popover to dropdown for improved stacking and positioning reliability. Updates z-index and disables problematic animations to prevent display issues, especially on first open. Enhances consistency and visual layering of menu components across the application.
This commit is contained in:
4 files changed
+259
-28
No files matched your search
@@ -32,7 +32,7 @@ import { Label } from './ui/label';
|
||||
import { Textarea } from './ui/textarea';
|
||||
import { ScrollArea } from './ui/scroll-area';
|
||||
import { cn } from '../lib/utils';
|
||||
import { Popover, PopoverContent, PopoverTrigger } from './ui/popover';
|
||||
import { Dropdown, DropdownTrigger, DropdownContent } from './ui/dropdown';
|
||||
import {
|
||||
ContextMenu,
|
||||
ContextMenuContent,
|
||||
@@ -828,8 +828,8 @@ echo $3 >> "$FILE"`);
|
||||
<Key size={14} />
|
||||
KEY
|
||||
</Button>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Dropdown>
|
||||
<DropdownTrigger asChild>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
@@ -840,8 +840,8 @@ echo $3 >> "$FILE"`);
|
||||
>
|
||||
<ChevronDown size={12} />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-44 p-1">
|
||||
</DropdownTrigger>
|
||||
<DropdownContent className="w-44" align="start" alignToParent>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="w-full justify-start gap-2"
|
||||
@@ -865,8 +865,8 @@ echo $3 >> "$FILE"`);
|
||||
<UserPlus size={14} /> New Identity
|
||||
</Button>
|
||||
)}
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</DropdownContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
|
||||
{/* CERTIFICATE button with split interaction */}
|
||||
@@ -891,8 +891,8 @@ echo $3 >> "$FILE"`);
|
||||
{keys.filter(k => k.certificate).length}
|
||||
</span>
|
||||
</Button>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Dropdown>
|
||||
<DropdownTrigger asChild>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
@@ -903,8 +903,8 @@ echo $3 >> "$FILE"`);
|
||||
>
|
||||
<ChevronDown size={12} />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-44 p-1">
|
||||
</DropdownTrigger>
|
||||
<DropdownContent className="w-44" align="start" alignToParent>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="w-full justify-start gap-2"
|
||||
@@ -912,8 +912,8 @@ echo $3 >> "$FILE"`);
|
||||
>
|
||||
<Upload size={14} /> Import Certificate
|
||||
</Button>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</DropdownContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
@@ -956,14 +956,14 @@ echo $3 >> "$FILE"`);
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Dropdown>
|
||||
<DropdownTrigger asChild>
|
||||
<Button variant="ghost" size="icon" className="h-9 w-9 flex-shrink-0">
|
||||
{viewMode === 'grid' ? <LayoutGrid size={16} /> : <ListIcon size={16} />}
|
||||
<ChevronDown size={10} className="ml-0.5" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-32 p-1 z-50" align="end">
|
||||
</DropdownTrigger>
|
||||
<DropdownContent className="w-32" align="end">
|
||||
<Button
|
||||
variant={viewMode === 'grid' ? 'secondary' : 'ghost'}
|
||||
className="w-full justify-start gap-2 h-9"
|
||||
@@ -978,8 +978,8 @@ echo $3 >> "$FILE"`);
|
||||
>
|
||||
<ListIcon size={14} /> List
|
||||
</Button>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</DropdownContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1627,8 +1627,8 @@ echo $3 >> "$FILE"`);
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Authentication Method</Label>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Dropdown>
|
||||
<DropdownTrigger asChild>
|
||||
<Button variant="secondary" className="w-full justify-between h-10">
|
||||
<span className="flex items-center gap-2">
|
||||
{draftIdentity.authMethod === 'key' && <><Key size={14} /> Key</>}
|
||||
@@ -1638,8 +1638,8 @@ echo $3 >> "$FILE"`);
|
||||
</span>
|
||||
<ChevronDown size={14} />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-56 p-1">
|
||||
</DropdownTrigger>
|
||||
<DropdownContent className="w-56" align="start">
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="w-full justify-start gap-2"
|
||||
@@ -1661,8 +1661,8 @@ echo $3 >> "$FILE"`);
|
||||
>
|
||||
<Shield size={14} /> FIDO2
|
||||
</Button>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</DropdownContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
|
||||
{(draftIdentity.authMethod === 'key' || draftIdentity.authMethod === 'certificate') && (
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
import * as React from "react"
|
||||
import { useEffect, useLayoutEffect, useRef, useState, useCallback } from "react"
|
||||
import { cn } from "../../lib/utils"
|
||||
|
||||
interface DropdownContextValue {
|
||||
open: boolean
|
||||
setOpen: (open: boolean) => void
|
||||
triggerRef: React.RefObject<HTMLButtonElement | null>
|
||||
}
|
||||
|
||||
const DropdownContext = React.createContext<DropdownContextValue | null>(null)
|
||||
|
||||
function useDropdown() {
|
||||
const context = React.useContext(DropdownContext)
|
||||
if (!context) {
|
||||
throw new Error("Dropdown components must be used within a Dropdown")
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
interface DropdownProps {
|
||||
children: React.ReactNode
|
||||
open?: boolean
|
||||
onOpenChange?: (open: boolean) => void
|
||||
}
|
||||
|
||||
const Dropdown: React.FC<DropdownProps> = ({ children, open: controlledOpen, onOpenChange }) => {
|
||||
const [internalOpen, setInternalOpen] = useState(false)
|
||||
const triggerRef = useRef<HTMLButtonElement>(null)
|
||||
|
||||
const open = controlledOpen !== undefined ? controlledOpen : internalOpen
|
||||
const setOpen = useCallback((value: boolean) => {
|
||||
if (controlledOpen === undefined) {
|
||||
setInternalOpen(value)
|
||||
}
|
||||
onOpenChange?.(value)
|
||||
}, [controlledOpen, onOpenChange])
|
||||
|
||||
return (
|
||||
<DropdownContext.Provider value={{ open, setOpen, triggerRef }}>
|
||||
{children}
|
||||
</DropdownContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
interface DropdownTriggerProps {
|
||||
children: React.ReactElement
|
||||
asChild?: boolean
|
||||
}
|
||||
|
||||
const DropdownTrigger: React.FC<DropdownTriggerProps> = ({ children, asChild }) => {
|
||||
const { open, setOpen, triggerRef } = useDropdown()
|
||||
|
||||
const handleClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
setOpen(!open)
|
||||
}
|
||||
|
||||
if (asChild && React.isValidElement(children)) {
|
||||
return React.cloneElement(children as React.ReactElement<any>, {
|
||||
ref: triggerRef,
|
||||
onClick: (e: React.MouseEvent) => {
|
||||
handleClick(e)
|
||||
;(children.props as any)?.onClick?.(e)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<button ref={triggerRef} onClick={handleClick}>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
interface DropdownContentProps {
|
||||
children: React.ReactNode
|
||||
className?: string
|
||||
align?: "start" | "center" | "end"
|
||||
sideOffset?: number
|
||||
side?: "top" | "bottom"
|
||||
/** If true, align to the trigger's parent element instead of the trigger itself */
|
||||
alignToParent?: boolean
|
||||
}
|
||||
|
||||
const DropdownContent: React.FC<DropdownContentProps> = ({
|
||||
children,
|
||||
className,
|
||||
align = "start",
|
||||
sideOffset = 4,
|
||||
side = "bottom",
|
||||
alignToParent = false,
|
||||
}) => {
|
||||
const { open, setOpen, triggerRef } = useDropdown()
|
||||
const contentRef = useRef<HTMLDivElement>(null)
|
||||
const [position, setPosition] = useState<{ top: number; left: number } | null>(null)
|
||||
|
||||
// Calculate position function
|
||||
const calculatePosition = useCallback(() => {
|
||||
if (!triggerRef.current) return null
|
||||
|
||||
// Use parent element if alignToParent is true
|
||||
const anchorEl = alignToParent ? triggerRef.current.parentElement : triggerRef.current
|
||||
if (!anchorEl) return null
|
||||
|
||||
const rect = anchorEl.getBoundingClientRect()
|
||||
const triggerRect = triggerRef.current.getBoundingClientRect()
|
||||
const contentEl = contentRef.current
|
||||
|
||||
let top: number
|
||||
let left: number
|
||||
|
||||
// Use trigger's bottom for vertical positioning (not parent's)
|
||||
if (side === "bottom") {
|
||||
top = triggerRect.bottom + sideOffset
|
||||
} else {
|
||||
top = triggerRect.top - sideOffset
|
||||
}
|
||||
|
||||
// Use anchor element (parent or trigger) for horizontal positioning
|
||||
if (align === "start") {
|
||||
left = rect.left
|
||||
} else if (align === "end") {
|
||||
left = rect.right
|
||||
if (contentEl) {
|
||||
left = rect.right - contentEl.offsetWidth
|
||||
}
|
||||
} else {
|
||||
left = rect.left + rect.width / 2
|
||||
if (contentEl) {
|
||||
left -= contentEl.offsetWidth / 2
|
||||
}
|
||||
}
|
||||
|
||||
// Keep within viewport
|
||||
if (contentEl) {
|
||||
const viewportWidth = window.innerWidth
|
||||
const viewportHeight = window.innerHeight
|
||||
|
||||
if (left + contentEl.offsetWidth > viewportWidth - 8) {
|
||||
left = viewportWidth - contentEl.offsetWidth - 8
|
||||
}
|
||||
if (left < 8) {
|
||||
left = 8
|
||||
}
|
||||
if (top + contentEl.offsetHeight > viewportHeight - 8) {
|
||||
top = rect.top - contentEl.offsetHeight - sideOffset
|
||||
}
|
||||
}
|
||||
|
||||
return { top, left }
|
||||
}, [align, sideOffset, side, alignToParent, triggerRef])
|
||||
|
||||
// Calculate position synchronously after DOM updates
|
||||
useLayoutEffect(() => {
|
||||
if (open) {
|
||||
// Initial calculation
|
||||
const pos = calculatePosition()
|
||||
if (pos) setPosition(pos)
|
||||
|
||||
// Recalculate after a frame to ensure content is fully rendered
|
||||
requestAnimationFrame(() => {
|
||||
const pos = calculatePosition()
|
||||
if (pos) setPosition(pos)
|
||||
})
|
||||
} else {
|
||||
setPosition(null)
|
||||
}
|
||||
}, [open, calculatePosition])
|
||||
|
||||
// Close on click outside
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (
|
||||
contentRef.current &&
|
||||
!contentRef.current.contains(e.target as Node) &&
|
||||
triggerRef.current &&
|
||||
!triggerRef.current.contains(e.target as Node)
|
||||
) {
|
||||
setOpen(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleEscape = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
setOpen(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Use setTimeout to avoid closing immediately on the same click that opened it
|
||||
const timeoutId = setTimeout(() => {
|
||||
document.addEventListener("mousedown", handleClickOutside)
|
||||
document.addEventListener("keydown", handleEscape)
|
||||
}, 0)
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeoutId)
|
||||
document.removeEventListener("mousedown", handleClickOutside)
|
||||
document.removeEventListener("keydown", handleEscape)
|
||||
}
|
||||
}, [open, setOpen, triggerRef])
|
||||
|
||||
if (!open) return null
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={contentRef}
|
||||
className={cn(
|
||||
"fixed z-[999999] rounded-md border bg-popover p-1 text-popover-foreground shadow-md",
|
||||
className
|
||||
)}
|
||||
style={{
|
||||
top: position?.top ?? -9999,
|
||||
left: position?.left ?? -9999,
|
||||
visibility: position ? 'visible' : 'hidden',
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export { Dropdown, DropdownTrigger, DropdownContent }
|
||||
@@ -7,6 +7,8 @@ const Popover = PopoverPrimitive.Root
|
||||
|
||||
const PopoverTrigger = PopoverPrimitive.Trigger
|
||||
|
||||
const PopoverAnchor = PopoverPrimitive.Anchor
|
||||
|
||||
const PopoverContent = React.forwardRef<
|
||||
React.ElementRef<typeof PopoverPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
|
||||
@@ -16,8 +18,11 @@ const PopoverContent = React.forwardRef<
|
||||
ref={ref}
|
||||
align={align}
|
||||
sideOffset={sideOffset}
|
||||
// Force position recalculation after rendering
|
||||
avoidCollisions={true}
|
||||
collisionPadding={8}
|
||||
className={cn(
|
||||
"z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none pointer-events-auto 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 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
|
||||
"z-[200000] w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none pointer-events-auto",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
@@ -26,4 +31,4 @@ const PopoverContent = React.forwardRef<
|
||||
))
|
||||
PopoverContent.displayName = PopoverPrimitive.Content.displayName
|
||||
|
||||
export { Popover, PopoverTrigger, PopoverContent }
|
||||
export { Popover, PopoverTrigger, PopoverContent, PopoverAnchor }
|
||||
@@ -72,8 +72,9 @@ const SelectContent = React.forwardRef<
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Content
|
||||
ref={ref}
|
||||
// Disable animations - they cause stacking/positioning issues on first open
|
||||
className={cn(
|
||||
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md 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 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
|
||||
"z-[200000] max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md",
|
||||
position === "popper" &&
|
||||
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
|
||||
className
|
||||
|
||||
Reference in new issue
Block a user