Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas742 committed Jan 11, 2024
1 parent 3ac6d44 commit 5d2a323
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
28 changes: 13 additions & 15 deletions packages/main/src/components/Modals/ModalsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ReactNode } from 'react';
import React, { useReducer, useRef } from 'react';
import React, { useMemo, useReducer } from 'react';
import { createPortal } from 'react-dom';
import type { ModalState, UpdateModalStateAction } from '../../internal/ModalsContext.js';
import { ModalsContext } from '../../internal/ModalsContext.js';
import { getModalContext } from '../../internal/ModalsContext.js';

export interface ModalsProviderPropTypes {
children: ReactNode;
Expand All @@ -23,22 +23,20 @@ const modalStateReducer = (state: ModalState[], action: UpdateModalStateAction)
export function ModalsProvider({ children }: ModalsProviderPropTypes) {
const [modals, setModal] = useReducer(modalStateReducer, []);

const isSyncedWithWindow = useRef(false);
// necessary for static method
globalThis['@ui5/webcomponents-react'] ??= {};
globalThis['@ui5/webcomponents-react'].setModal = setModal;

if (!isSyncedWithWindow.current && typeof window !== 'undefined') {
window['@ui5/webcomponents-react'] ??= {};
window['@ui5/webcomponents-react'].ModalsContext = ModalsContext;
window['@ui5/webcomponents-react'].setModal = setModal;
isSyncedWithWindow.current = true;
}

const GlobalModalsContext = isSyncedWithWindow.current
? window['@ui5/webcomponents-react'].ModalsContext
: ModalsContext;
const globalSetModal = isSyncedWithWindow.current ? window['@ui5/webcomponents-react'].setModal : setModal;
const GlobalModalsContext = getModalContext();
const memoizedVal = useMemo(
() => ({
setModal: globalThis['@ui5/webcomponents-react'].setModal
}),
[]
);

return (
<GlobalModalsContext.Provider value={{ setModal: globalSetModal }}>
<GlobalModalsContext.Provider value={memoizedVal}>
{modals.map((modal) => {
if (modal?.Component) {
return createPortal(
Expand Down
17 changes: 11 additions & 6 deletions packages/main/src/internal/ModalsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ interface IModalsContext<Props, DomRef> {
setModal?: Dispatch<UpdateModalStateAction<Props, DomRef>>;
}

export const ModalsContext = createContext<IModalsContext<Record<string, any>, HTMLElement>>({
const ModalsContext = createContext<IModalsContext<Record<string, any>, HTMLElement>>({
setModal: null
});

export const useModalsContext = (): ContextType<typeof ModalsContext> => {
let context = ModalsContext;
if (typeof window !== 'undefined' && window['@ui5/webcomponents-react']?.ModalsContext) {
context = window['@ui5/webcomponents-react'].ModalsContext;
export function getModalContext() {
if (!globalThis['@ui5/webcomponents-react']?.ModalsContext) {
globalThis['@ui5/webcomponents-react'] ??= {};
globalThis['@ui5/webcomponents-react'].ModalsContext = ModalsContext;
}
return useContext(context);

return globalThis['@ui5/webcomponents-react'].ModalsContext;
}

export const useModalsContext = (): ContextType<typeof ModalsContext> => {
return useContext(getModalContext());
};

0 comments on commit 5d2a323

Please sign in to comment.