fix: auto complation z-index

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-07-28 16:11:52 +08:00
parent c606ce1b41
commit 2f307db205
4 changed files with 20 additions and 165 deletions
@@ -207,7 +207,7 @@ export const Autocomplete = forwardRef<HTMLInputElement, AutocompleteProps>(
const ListElement = (
<div
className={clsx(
"pointer-events-auto z-[101] mt-1 overflow-hidden",
"pointer-events-auto z-[999] mt-1 overflow-hidden",
portal ? "absolute flex flex-col" : "absolute w-full",
)}
ref={useCallback((el) => {
@@ -1,148 +0,0 @@
import * as Popover from "@radix-ui/react-popover"
import {
Command,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@renderer/components/ui/command"
import { cn } from "@renderer/lib/utils"
import { useCallback, useState } from "react"
import * as React from "react"
export interface InputProps
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "value"> {
// TODO isLoading
options: string[]
emptyMessage: string
value?: string | null
maxHeight?: number
}
/**
* @deprecated please use `ui/auto-completion` instead
*/
export const AutoComplete = React.forwardRef<HTMLInputElement, InputProps>(
({ options, emptyMessage, className, value, maxHeight, ...props }, ref) => {
const [open, setOpen] = useState(false)
const onValueChange = useCallback(
(value: string) => {
props.onChange?.({
target: {
value,
},
} as React.ChangeEvent<HTMLInputElement>)
},
[props.onChange],
)
const handleSelectOption = useCallback(
(option: string) => {
setOpen(false)
onValueChange(option)
},
[props.onChange],
)
const inputRef = React.useRef<HTMLInputElement>(null)
const [dropdownHeight, setDropdownHeight] = React.useState(
maxHeight || 200,
)
const [inputRect, setInputRect] = React.useState<DOMRect | null>(null)
const [dropdownPos, setDropdownPos] = React.useState("down")
React.useLayoutEffect(() => {
if (!inputRef.current) {
return
}
const rect = inputRef.current.getBoundingClientRect()
const { top, height } = rect
if (top + height + dropdownHeight > window.innerHeight) {
setDropdownPos("up")
} else {
setDropdownPos("down")
}
setInputRect(rect)
}, [inputRect?.height, dropdownHeight])
React.useImperativeHandle(ref, () => inputRef.current!)
return (
<Command className="relative overflow-visible rounded-lg border bg-theme-background [&_.lucide-search]:hidden [&_[cmdk-input-wrapper]]:border-0">
<Popover.Root open={open && options.length > 0}>
<Popover.Trigger>
<CommandInput
{...props}
className="h-10"
onBlur={(e) => {
setOpen(false)
props.onBlur?.(e)
}}
onFocus={(e) => {
setOpen(true)
props.onFocus?.(e)
}}
value={value || void 0}
onValueChange={(value) => {
setOpen(true)
onValueChange(value)
}}
ref={inputRef}
/>
</Popover.Trigger>
<CommandList>
<Popover.Portal>
<Popover.Content>
<div
style={{
width: inputRect?.width,
bottom: dropdownPos === "up" ? 0 : undefined,
transform:
dropdownPos === "up" ?
`translateY(-${
dropdownHeight + (inputRect?.height || 0)
}px)` :
undefined,
}}
>
<CommandGroup
className={cn(
"absolute top-full z-10 mt-1 hidden w-full rounded-lg border bg-theme-background [&[hidden]]:!hidden",
open && "block",
)}
ref={(el: HTMLDivElement) => {
if (el) {
setDropdownHeight(el.clientHeight)
}
}}
style={{
maxHeight,
}}
>
{options.map((option) => (
<CommandItem
key={option}
value={option}
onSelect={() => handleSelectOption(option)}
className="text-zinc-800"
onMouseDown={(event) => {
event.preventDefault()
event.stopPropagation()
}}
>
{option}
</CommandItem>
))}
</CommandGroup>
</div>
</Popover.Content>
</Popover.Portal>
</CommandList>
</Popover.Root>
</Command>
)
},
)
AutoComplete.displayName = "AutoComplete"
@@ -24,6 +24,7 @@ import { useHotkeysContext } from "react-hotkeys-hook"
import { useEventCallback } from "usehooks-ts"
import { Divider } from "../../divider"
import { RootPortalProvider } from "../../portal/provider"
import { modalStackAtom } from "./atom"
import { MODAL_STACK_Z_INDEX, modalMontionConfig } from "./constants"
import type { CurrentModalContentProps, ModalActionsInternal } from "./context"
@@ -148,12 +149,14 @@ export const ModalInternal: Component<{
() => ({
dismiss: close,
setClickOutSideToDismiss: (v) => {
setStack((state) => produce(state, (draft) => {
const model = draft.find((modal) => modal.id === item.id)
if (!model) return
if (model.clickOutsideToDismiss === v) return
model.clickOutsideToDismiss = v
}))
setStack((state) =>
produce(state, (draft) => {
const model = draft.find((modal) => modal.id === item.id)
if (!model) return
if (model.clickOutsideToDismiss === v) return
model.clickOutsideToDismiss = v
}),
)
},
}),
[close, item.id, setStack],
@@ -166,11 +169,16 @@ export const ModalInternal: Component<{
}),
[ModalProps],
)
const edgeElementRef = useRef<HTMLDivElement>(null)
const finalChildren = useMemo(
() => (
<CurrentModalContext.Provider value={ModalContextProps}>
<AppErrorBoundary errorType={ErrorComponentType.Modal}>
{children ?? createElement(content, ModalProps)}
<RootPortalProvider value={edgeElementRef.current as HTMLElement}>
{children ?? createElement(content, ModalProps)}
</RootPortalProvider>
</AppErrorBoundary>
</CurrentModalContext.Provider>
),
@@ -184,8 +192,6 @@ export const ModalInternal: Component<{
}
}, [currentIsClosing])
const edgeElementRef = useRef<HTMLDivElement>(null)
const { enableScope, disableScope } = useHotkeysContext()
useEffect(() => {
@@ -207,9 +213,10 @@ export const ModalInternal: Component<{
</Dialog.DialogTitle>
<Dialog.Content asChild>
<div
ref={edgeElementRef}
className={cn(
"fixed inset-0 z-20 overflow-auto",
currentIsClosing && "!pointer-events-none",
currentIsClosing ? "!pointer-events-none" : "!pointer-events-auto",
modalContainerClassName,
)}
onClick={
@@ -3,13 +3,9 @@ import { createContext, useContext } from "react"
export const useRootPortal = () => {
const ctx = useContext(RootPortalContext)
return ctx.to || document.body
return ctx || document.body
}
const RootPortalContext = createContext<{
to?: HTMLElement | undefined
}>({
to: undefined,
})
const RootPortalContext = createContext<HTMLElement | undefined>(undefined)
export const RootPortalProvider = RootPortalContext.Provider