From d9855cb244f13c764f5ccdf903a4333d04456b69 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sun, 8 Oct 2023 20:12:31 +0200 Subject: [PATCH] more overlay system testing Co-authored-by: Jip Frijlink --- src/components/overlays/OverlayDisplay.tsx | 33 +++++++--------- src/components/overlays/OverlayPage.tsx | 2 +- .../player/atoms/LeftSideControls.tsx | 3 -- src/hooks/useOverlayRouter.ts | 10 +++-- src/hooks/useQueryParams.ts | 9 +++-- src/pages/developer/TestView.tsx | 39 ++++++++++++++++++- 6 files changed, 67 insertions(+), 29 deletions(-) delete mode 100644 src/components/player/atoms/LeftSideControls.tsx diff --git a/src/components/overlays/OverlayDisplay.tsx b/src/components/overlays/OverlayDisplay.tsx index a470a9ce..9cbc03dc 100644 --- a/src/components/overlays/OverlayDisplay.tsx +++ b/src/components/overlays/OverlayDisplay.tsx @@ -3,11 +3,11 @@ import { ReactNode, useCallback, useEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { Transition } from "@/components/Transition"; +import { useOverlayRouter } from "@/hooks/useOverlayRouter"; export interface OverlayProps { + id: string; children?: ReactNode; - onClose?: () => void; - show?: boolean; darken?: boolean; } @@ -16,6 +16,7 @@ export function OverlayDisplay(props: { children: ReactNode }) { } export function Overlay(props: OverlayProps) { + const router = useOverlayRouter(props.id); const [portalElement, setPortalElement] = useState(null); const ref = useRef(null); const target = useRef(null); @@ -37,9 +38,9 @@ export function Overlay(props: OverlayProps) { if (e.currentTarget !== e.target) return; if (!startedTarget) return; if (!startedTarget.isEqualNode(e.currentTarget as Element)) return; - if (props.onClose) props.onClose(); + router.close(); }, - [props] + [router] ); useEffect(() => { @@ -47,25 +48,21 @@ export function Overlay(props: OverlayProps) { setPortalElement(element ?? document.body); }, []); - const backdrop = ( - -
- - ); - return (
{portalElement ? createPortal( - +
- {backdrop} + +
+ {props.children} diff --git a/src/components/overlays/OverlayPage.tsx b/src/components/overlays/OverlayPage.tsx index bc8e0bfd..17a71dbf 100644 --- a/src/components/overlays/OverlayPage.tsx +++ b/src/components/overlays/OverlayPage.tsx @@ -13,7 +13,7 @@ interface Props { active?: boolean; // true if a child view is loaded } -export function FloatingView(props: Props) { +export function OverlayPage(props: Props) { const { isMobile } = useIsMobile(); const width = !isMobile ? `${props.width}px` : "100%"; return ( diff --git a/src/components/player/atoms/LeftSideControls.tsx b/src/components/player/atoms/LeftSideControls.tsx deleted file mode 100644 index 8d49f1d3..00000000 --- a/src/components/player/atoms/LeftSideControls.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export function LeftSideControls(props: {children: React.ReactNode}) { - -} diff --git a/src/hooks/useOverlayRouter.ts b/src/hooks/useOverlayRouter.ts index f6e90913..fe5ecc29 100644 --- a/src/hooks/useOverlayRouter.ts +++ b/src/hooks/useOverlayRouter.ts @@ -4,7 +4,6 @@ export function useOverlayRouter(id: string) { const [route, setRoute] = useQueryParam("r"); const routeParts = (route ?? "").split("/").filter((v) => v.length > 0); const routerActive = routeParts.length > 0 && routeParts[0] === id; - const currentPage = routeParts[routeParts.length - 1] ?? "/"; function navigate(path: string) { const newRoute = [id, ...path.split("/").filter((v) => v.length > 0)]; @@ -20,7 +19,7 @@ export function useOverlayRouter(id: string) { } function isCurrentPage(page: string) { - return routerActive && page === currentPage; + return routerActive && route === `/${id}${page}`; } function isLoaded(page: string) { @@ -40,7 +39,11 @@ export function useOverlayRouter(id: string) { } function close() { - navigate("/"); + setRoute(null); + } + + function open() { + setRoute(`/${id}`); } return { @@ -51,5 +54,6 @@ export function useOverlayRouter(id: string) { isCurrentPage, pageProps, isActive, + open, }; } diff --git a/src/hooks/useQueryParams.ts b/src/hooks/useQueryParams.ts index 6de52f1c..54c78b57 100644 --- a/src/hooks/useQueryParams.ts +++ b/src/hooks/useQueryParams.ts @@ -1,5 +1,5 @@ import { useCallback, useMemo } from "react"; -import { useLocation } from "react-router-dom"; +import { useHistory, useLocation } from "react-router-dom"; export function useQueryParams() { const loc = useLocation(); @@ -19,6 +19,7 @@ export function useQueryParams() { export function useQueryParam(param: string) { const params = useQueryParams(); const location = useLocation(); + const router = useHistory(); const currentValue = params[param]; const set = useCallback( @@ -26,9 +27,11 @@ export function useQueryParam(param: string) { const parsed = new URLSearchParams(location.search); if (value) parsed.set(param, value); else parsed.delete(param); - location.search = parsed.toString(); + router.push({ + search: parsed.toString(), + }); }, - [param, location] + [param, location, router] ); return [currentValue, set] as const; diff --git a/src/pages/developer/TestView.tsx b/src/pages/developer/TestView.tsx index 2b9e3817..c796b502 100644 --- a/src/pages/developer/TestView.tsx +++ b/src/pages/developer/TestView.tsx @@ -1,4 +1,41 @@ +import { OverlayAnchor } from "@/components/overlays/OverlayAnchor"; +import { Overlay, OverlayDisplay } from "@/components/overlays/OverlayDisplay"; +import { OverlayPage } from "@/components/overlays/OverlayPage"; +import { useOverlayRouter } from "@/hooks/useOverlayRouter"; + // simple empty view, perfect for putting in tests export default function TestView() { - return
; + const router = useOverlayRouter("test"); + const pages = ["", "/one", "/two"]; + + return ( + +
+ + + +
+ + + Home + Page one + Page two + +
+ + ); }