Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add runtime error for missing OverlayKitProvider #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import { createSafeContext } from '../utils/create-safe-context';

export const [OverlayContextProvider, useOverlayContext] = createSafeContext<OverlayData>('overlay-kit/OverlayContext');

function useSafeOverlayContext() {
const overlayContext = useOverlayContext();

if (overlayContext == null) {
throw new Error('This component must be used inside a <OverlayProvider> component.');
}

return overlayContext;
}

export function useCurrentOverlay() {
return useOverlayContext().current;
return useSafeOverlayContext().current;
}

export function useOverlayData() {
return useOverlayContext().overlayData;
return useSafeOverlayContext().overlayData;
}
43 changes: 24 additions & 19 deletions packages/src/context/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { overlay } from '../event';
export function OverlayProvider({ children }: PropsWithChildren) {
const overlayState = useSyncOverlayStore();

if (overlayState == null) {
dispatchOverlay({ type: 'INIT' });
}

useEffect(() => {
return () => {
dispatchOverlay({ type: 'REMOVE_ALL' });
Expand All @@ -16,26 +20,27 @@ export function OverlayProvider({ children }: PropsWithChildren) {
return (
<OverlayContextProvider value={overlayState}>
{children}
{overlayState.overlayOrderList.map((item) => {
const { id: currentOverlayId, isOpen, controller: currentController } = overlayState.overlayData[item];
{overlayState != null &&
overlayState.overlayOrderList.map((item) => {
const { id: currentOverlayId, isOpen, controller: currentController } = overlayState.overlayData[item];

return (
<ContentOverlayController
key={currentOverlayId}
isOpen={isOpen}
current={overlayState.current}
overlayId={currentOverlayId}
onMounted={() => {
requestAnimationFrame(() => {
dispatchOverlay({ type: 'OPEN', overlayId: currentOverlayId });
});
}}
onCloseModal={() => overlay.close(currentOverlayId)}
onExitModal={() => overlay.unmount(currentOverlayId)}
controller={currentController}
/>
);
})}
return (
<ContentOverlayController
key={currentOverlayId}
isOpen={isOpen}
current={overlayState.current}
overlayId={currentOverlayId}
onMounted={() => {
requestAnimationFrame(() => {
dispatchOverlay({ type: 'OPEN', overlayId: currentOverlayId });
});
}}
onCloseModal={() => overlay.close(currentOverlayId)}
onExitModal={() => overlay.unmount(currentOverlayId)}
controller={currentController}
/>
);
})}
</OverlayContextProvider>
);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/src/context/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type OverlayData, type OverlayItem } from './store';

export type OverlayReducerAction =
| { type: 'INIT' }
| { type: 'ADD'; overlay: OverlayItem }
| { type: 'OPEN'; overlayId: string }
| { type: 'CLOSE'; overlayId: string }
Expand All @@ -9,6 +10,18 @@ export type OverlayReducerAction =
| { type: 'REMOVE_ALL' };

export function overlayReducer(state: OverlayData, action: OverlayReducerAction): OverlayData {
if (state == null) {
if (action.type !== 'INIT') {
throw new Error('This component must be used inside a <OverlayProvider> component.');
}

return {
current: null,
overlayOrderList: [],
overlayData: {},
};
}

switch (action.type) {
case 'ADD': {
const isExisted = state.overlayOrderList.includes(action.overlay.id);
Expand Down
21 changes: 11 additions & 10 deletions packages/src/context/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ export type OverlayItem = {
isOpen: boolean;
controller: OverlayControllerComponent;
};
export type OverlayData = {
current: OverlayId | null;
overlayOrderList: OverlayId[];
overlayData: Record<OverlayId, OverlayItem>;
};
export type OverlayData =
| {
current: OverlayId | null;
overlayOrderList: OverlayId[];
overlayData: Record<OverlayId, OverlayItem>;
}
| undefined;

let overlays: OverlayData = {
current: null,
overlayOrderList: [],
overlayData: {},
};
/**
* @description Using `OverlayProvider` will initialize the variable
*/
let overlays: OverlayData = undefined;
let listeners: Array<() => void> = [];

function emitChangeListener() {
Expand Down