From a74e9aa34115fb62cc7e60ee2e8b9bc0f5c2ac93 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Sun, 13 Aug 2023 10:27:42 -0700 Subject: [PATCH] Store portal elements as state instead of refs (#1212) i :heart: dan abramov --- .../Explorer/QuickPreview/Context.tsx | 8 ++--- .../app/$libraryId/Explorer/View/index.tsx | 3 +- interface/app/$libraryId/TopBar/Layout.tsx | 12 +++---- interface/app/$libraryId/TopBar/Portal.tsx | 10 ++---- interface/app/$libraryId/TopBar/index.tsx | 32 +++++++++---------- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/interface/app/$libraryId/Explorer/QuickPreview/Context.tsx b/interface/app/$libraryId/Explorer/QuickPreview/Context.tsx index 2326bb9de..2c05bef4d 100644 --- a/interface/app/$libraryId/Explorer/QuickPreview/Context.tsx +++ b/interface/app/$libraryId/Explorer/QuickPreview/Context.tsx @@ -1,18 +1,18 @@ -import { PropsWithChildren, RefObject, createContext, useContext, useRef } from 'react'; +import { PropsWithChildren, createContext, useContext, useState } from 'react'; interface QuickPreviewContext { - ref: RefObject; + ref: HTMLDivElement | null; } const QuickPreviewContext = createContext(null); export const QuickPreviewContextProvider = ({ children }: PropsWithChildren) => { - const ref = useRef(null); + const [ref, setRef] = useState(null); return ( {children} -
+
); }; diff --git a/interface/app/$libraryId/Explorer/View/index.tsx b/interface/app/$libraryId/Explorer/View/index.tsx index f3f8638e4..fe31a45ec 100644 --- a/interface/app/$libraryId/Explorer/View/index.tsx +++ b/interface/app/$libraryId/Explorer/View/index.tsx @@ -172,8 +172,7 @@ export default memo( )}
- {quickPreviewCtx.ref.current && - createPortal(, quickPreviewCtx.ref.current)} + {quickPreviewCtx.ref && createPortal(, quickPreviewCtx.ref)} ); } diff --git a/interface/app/$libraryId/TopBar/Layout.tsx b/interface/app/$libraryId/TopBar/Layout.tsx index 6105ef2ec..d74e36a29 100644 --- a/interface/app/$libraryId/TopBar/Layout.tsx +++ b/interface/app/$libraryId/TopBar/Layout.tsx @@ -1,21 +1,21 @@ -import { RefObject, createContext, useContext, useRef } from 'react'; +import { createContext, useContext, useState } from 'react'; import { Outlet } from 'react-router'; import TopBar from '.'; interface TopBarContext { - left: RefObject; - right: RefObject; + left: HTMLDivElement | null; + right: HTMLDivElement | null; } const TopBarContext = createContext(null); export const Component = () => { - const left = useRef(null); - const right = useRef(null); + const [left, setLeft] = useState(null); + const [right, setRight] = useState(null); return ( - + ); diff --git a/interface/app/$libraryId/TopBar/Portal.tsx b/interface/app/$libraryId/TopBar/Portal.tsx index 550b74c9c..0fbd733e0 100644 --- a/interface/app/$libraryId/TopBar/Portal.tsx +++ b/interface/app/$libraryId/TopBar/Portal.tsx @@ -2,17 +2,13 @@ import { ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { useTopBarContext } from './Layout'; -interface Props { - left?: ReactNode; - right?: ReactNode; -} -export const TopBarPortal = ({ left, right }: Props) => { +export const TopBarPortal = (props: { left?: ReactNode; right?: ReactNode }) => { const ctx = useTopBarContext(); return ( <> - {left && ctx.left.current && createPortal(left, ctx.left.current)} - {right && ctx.right.current && createPortal(right, ctx.right.current)} + {props.left && ctx.left && createPortal(props.left, ctx.left)} + {props.right && ctx.right && createPortal(props.right, ctx.right)} ); }; diff --git a/interface/app/$libraryId/TopBar/index.tsx b/interface/app/$libraryId/TopBar/index.tsx index 551e3fbb3..95bae6bf8 100644 --- a/interface/app/$libraryId/TopBar/index.tsx +++ b/interface/app/$libraryId/TopBar/index.tsx @@ -1,33 +1,31 @@ -import { RefObject } from 'react'; +import { Ref } from 'react'; import { NavigationButtons } from './NavigationButtons'; import SearchBar from './SearchBar'; export const TOP_BAR_HEIGHT = 46; interface Props { - leftRef?: RefObject; - rightRef?: RefObject; + leftRef?: Ref; + rightRef?: Ref; } -const TopBar = (props: Props) => { - return ( -
-
- -
-
- -
+ > +
+ +
- ); -}; + +
+
+); export default TopBar;