mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 07:32:54 +00:00
Improves code consistency by fixing indentation
Standardizes indentation across dropdown and select UI components for improved readability and maintainability. No logic changes introduced.
This commit is contained in:
2 files changed
+178
-178
No files matched your search
+176
-176
@@ -3,223 +3,223 @@ 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>
|
||||
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
|
||||
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
|
||||
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])
|
||||
const [internalOpen, setInternalOpen] = useState(false)
|
||||
const triggerRef = useRef<HTMLButtonElement>(null)
|
||||
|
||||
return (
|
||||
<DropdownContext.Provider value={{ open, setOpen, triggerRef }}>
|
||||
{children}
|
||||
</DropdownContext.Provider>
|
||||
)
|
||||
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
|
||||
children: React.ReactElement
|
||||
asChild?: boolean
|
||||
}
|
||||
|
||||
const DropdownTrigger: React.FC<DropdownTriggerProps> = ({ children, asChild }) => {
|
||||
const { open, setOpen, triggerRef } = useDropdown()
|
||||
const { open, setOpen, triggerRef } = useDropdown()
|
||||
|
||||
const handleClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
setOpen(!open)
|
||||
}
|
||||
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)
|
||||
},
|
||||
})
|
||||
}
|
||||
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>
|
||||
)
|
||||
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
|
||||
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,
|
||||
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)
|
||||
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
|
||||
// Calculate position function
|
||||
const calculatePosition = useCallback(() => {
|
||||
if (!triggerRef.current) return null
|
||||
|
||||
// Use trigger's bottom for vertical positioning (not parent's)
|
||||
if (side === "bottom") {
|
||||
top = triggerRect.bottom + sideOffset
|
||||
} else {
|
||||
top = triggerRect.top - sideOffset
|
||||
}
|
||||
// Use parent element if alignToParent is true
|
||||
const anchorEl = alignToParent ? triggerRef.current.parentElement : triggerRef.current
|
||||
if (!anchorEl) return null
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
const rect = anchorEl.getBoundingClientRect()
|
||||
const triggerRect = triggerRef.current.getBoundingClientRect()
|
||||
const contentEl = contentRef.current
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
let top: number
|
||||
let left: number
|
||||
|
||||
return { top, left }
|
||||
}, [align, sideOffset, side, alignToParent, triggerRef])
|
||||
// Use trigger's bottom for vertical positioning (not parent's)
|
||||
if (side === "bottom") {
|
||||
top = triggerRect.bottom + sideOffset
|
||||
} else {
|
||||
top = triggerRect.top - sideOffset
|
||||
}
|
||||
|
||||
// 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])
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// Close on click outside
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
// Keep within viewport
|
||||
if (contentEl) {
|
||||
const viewportWidth = window.innerWidth
|
||||
const viewportHeight = window.innerHeight
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
const handleEscape = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
setOpen(false)
|
||||
}
|
||||
}
|
||||
return { top, left }
|
||||
}, [align, sideOffset, side, alignToParent, triggerRef])
|
||||
|
||||
// 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)
|
||||
// Calculate position synchronously after DOM updates
|
||||
useLayoutEffect(() => {
|
||||
if (open) {
|
||||
// Initial calculation
|
||||
const pos = calculatePosition()
|
||||
if (pos) setPosition(pos)
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeoutId)
|
||||
document.removeEventListener("mousedown", handleClickOutside)
|
||||
document.removeEventListener("keydown", handleEscape)
|
||||
}
|
||||
}, [open, setOpen, triggerRef])
|
||||
// Recalculate after a frame to ensure content is fully rendered
|
||||
requestAnimationFrame(() => {
|
||||
const pos = calculatePosition()
|
||||
if (pos) setPosition(pos)
|
||||
})
|
||||
} else {
|
||||
setPosition(null)
|
||||
}
|
||||
}, [open, calculatePosition])
|
||||
|
||||
if (!open) return null
|
||||
// Close on click outside
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
|
||||
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>
|
||||
)
|
||||
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 }
|
||||
@@ -76,7 +76,7 @@ const SelectContent = React.forwardRef<
|
||||
className={cn(
|
||||
"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",
|
||||
"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
|
||||
)}
|
||||
position={position}
|
||||
@@ -87,7 +87,7 @@ const SelectContent = React.forwardRef<
|
||||
className={cn(
|
||||
"p-1",
|
||||
position === "popper" &&
|
||||
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
|
||||
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in new issue
Block a user