From 2f307db20504cbefed40b52f78e3adae425f31b6 Mon Sep 17 00:00:00 2001 From: Innei Date: Sun, 28 Jul 2024 16:11:52 +0800 Subject: [PATCH] fix: auto complation z-index Signed-off-by: Innei --- .../ui/auto-completion/AutoCompletion.tsx | 2 +- .../src/components/ui/autocomplete.tsx | 148 ------------------ .../src/components/ui/modal/stacked/modal.tsx | 27 ++-- .../src/components/ui/portal/provider.tsx | 8 +- 4 files changed, 20 insertions(+), 165 deletions(-) delete mode 100644 src/renderer/src/components/ui/autocomplete.tsx diff --git a/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx b/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx index c7dd8e333..934890f12 100644 --- a/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx +++ b/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx @@ -207,7 +207,7 @@ export const Autocomplete = forwardRef( const ListElement = (
{ diff --git a/src/renderer/src/components/ui/autocomplete.tsx b/src/renderer/src/components/ui/autocomplete.tsx deleted file mode 100644 index 7d2e67cd5..000000000 --- a/src/renderer/src/components/ui/autocomplete.tsx +++ /dev/null @@ -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, "value"> { - // TODO isLoading - options: string[] - emptyMessage: string - value?: string | null - maxHeight?: number -} -/** - * @deprecated please use `ui/auto-completion` instead - */ -export const AutoComplete = React.forwardRef( - ({ options, emptyMessage, className, value, maxHeight, ...props }, ref) => { - const [open, setOpen] = useState(false) - - const onValueChange = useCallback( - (value: string) => { - props.onChange?.({ - target: { - value, - }, - } as React.ChangeEvent) - }, - [props.onChange], - ) - - const handleSelectOption = useCallback( - (option: string) => { - setOpen(false) - onValueChange(option) - }, - [props.onChange], - ) - - const inputRef = React.useRef(null) - const [dropdownHeight, setDropdownHeight] = React.useState( - maxHeight || 200, - ) - const [inputRect, setInputRect] = React.useState(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 ( - - 0}> - - { - setOpen(false) - props.onBlur?.(e) - }} - onFocus={(e) => { - setOpen(true) - props.onFocus?.(e) - }} - value={value || void 0} - onValueChange={(value) => { - setOpen(true) - onValueChange(value) - }} - ref={inputRef} - /> - - - - - -
- -
-
-
-
-
-
- ) - }, -) -AutoComplete.displayName = "AutoComplete" diff --git a/src/renderer/src/components/ui/modal/stacked/modal.tsx b/src/renderer/src/components/ui/modal/stacked/modal.tsx index 8076ed2f4..40fcee18f 100644 --- a/src/renderer/src/components/ui/modal/stacked/modal.tsx +++ b/src/renderer/src/components/ui/modal/stacked/modal.tsx @@ -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(null) + const finalChildren = useMemo( () => ( - {children ?? createElement(content, ModalProps)} + + {children ?? createElement(content, ModalProps)} + ), @@ -184,8 +192,6 @@ export const ModalInternal: Component<{ } }, [currentIsClosing]) - const edgeElementRef = useRef(null) - const { enableScope, disableScope } = useHotkeysContext() useEffect(() => { @@ -207,9 +213,10 @@ export const ModalInternal: Component<{
{ const ctx = useContext(RootPortalContext) - return ctx.to || document.body + return ctx || document.body } -const RootPortalContext = createContext<{ - to?: HTMLElement | undefined -}>({ - to: undefined, -}) +const RootPortalContext = createContext(undefined) export const RootPortalProvider = RootPortalContext.Provider