From a4df11c4823bbbafd9b792fa0efffe14f680ecc0 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 14 Jul 2024 14:52:11 +0200 Subject: [PATCH] Add curtain context for a curtain instance. --- src/Components/Curtains/Curtains.tsx | 70 ++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/src/Components/Curtains/Curtains.tsx b/src/Components/Curtains/Curtains.tsx index cf6ba8f..d5aba3a 100644 --- a/src/Components/Curtains/Curtains.tsx +++ b/src/Components/Curtains/Curtains.tsx @@ -50,6 +50,8 @@ export function useCurtains(): CurtainsContextState return useContext(CurtainsContext); } + + /** * Page curtains system. */ @@ -117,20 +119,78 @@ function CurtainsPortal({curtains}: { }) { return ReactDOM.createPortal(Object.entries(curtains).map(([uuid, curtainContent]) => ( - + {curtainContent} )), document.body); } + + +/** + * Current curtain data and functions. + */ +export interface CurtainContextState +{ + /** + * Curtain UUID. + */ + uuid: CurtainUuidType; + + /** + * Close the curtain. + */ + close: () => void; +} + +/** + * Current curtain context. + */ +const CurtainContext = React.createContext({ + // Empty values. + uuid: "", + close: () => {}, +}); + +/** + * Hook to access current curtain data and functions. + */ +export function useCurtain(): CurtainContextState +{ + return useContext(CurtainContext); +} + /** * Component of an opened curtain instance. */ -function CurtainInstance({children}: React.PropsWithChildren<{}>) +function CurtainInstance({uuid, children}: React.PropsWithChildren<{ + /** + * Curtain UUID. + */ + uuid: string; +}>) { + // Get close curtain function. + const {close} = useCurtains(); + + // Initialize close curtain function. + const closeCurtain = useRef<() => void>(); + closeCurtain.current = useCallback(() => { + // Close the current curtain. + close(uuid); + }, [uuid, close]); + + // Initialize context state from action functions. + const contextState = useMemo(() => ({ + uuid: uuid, + close: () => closeCurtain.current(), + }), [uuid, closeCurtain]); + return ( -
- {children} -
+ +
+ {children} +
+
); }