Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/quiet-overlays-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'overlay-kit': patch
---

Fix `openAsync` promises remaining pending when reopening a closed overlay with the same ID, while preserving the overlay's component state.
43 changes: 34 additions & 9 deletions packages/src/context/provider/content-overlay-controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,53 @@ export type OverlayAsyncControllerProps<T> = Omit<OverlayControllerProps, 'close
export type OverlayControllerComponent = FC<OverlayControllerProps>;
export type OverlayAsyncControllerComponent<T> = FC<OverlayAsyncControllerProps<T>>;

export type OverlayAsyncResult = {
resolve: (value: unknown) => void;
reject: (reason?: unknown) => void;
};

type ContentOverlayControllerProps = {
isOpen: boolean;
overlayId: string;
overlayDispatch: ActionDispatch<[action: OverlayReducerAction]>;
controller: OverlayControllerComponent;
isAsyncController?: boolean;
asyncResult?: OverlayAsyncResult;
};

export const ContentOverlayController = memo(
({ isOpen, overlayId, overlayDispatch, controller: Controller }: ContentOverlayControllerProps) => {
({
isOpen,
overlayId,
overlayDispatch,
controller: Controller,
isAsyncController,
asyncResult,
}: ContentOverlayControllerProps) => {
useEffect(() => {
requestAnimationFrame(() => {
overlayDispatch({ type: 'OPEN', overlayId });
});
}, [overlayDispatch, overlayId]);

return (
<Controller
isOpen={isOpen}
overlayId={overlayId}
close={() => overlayDispatch({ type: 'CLOSE', overlayId })}
unmount={() => overlayDispatch({ type: 'REMOVE', overlayId })}
/>
);
const props = {
isOpen,
overlayId,
close: (value?: unknown) => {
asyncResult?.resolve(value);
overlayDispatch({ type: 'CLOSE', overlayId, asyncResult });
},
unmount: () => overlayDispatch({ type: 'REMOVE', overlayId }),
...(isAsyncController
? {
reject: (reason?: unknown) => {
asyncResult?.reject(reason);
overlayDispatch({ type: 'CLOSE', overlayId, asyncResult });
},
}
: {}),
};

return <Controller {...props} />;
}
);
15 changes: 13 additions & 2 deletions packages/src/context/provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function createOverlayProvider() {
});
const prevOverlayState = useRef(overlayState);

const open: OverlayEvent['open'] = useCallback(({ controller, overlayId, componentKey }) => {
const open: OverlayEvent['open'] = useCallback(({ controller, overlayId, componentKey, asyncResult }) => {
overlayDispatch({
type: 'ADD',
overlay: {
Expand All @@ -27,6 +27,8 @@ export function createOverlayProvider() {
isOpen: false,
isMounted: false,
controller: controller,
isAsyncController: asyncResult != null,
asyncResult,
},
});
}, []);
Expand Down Expand Up @@ -75,7 +77,14 @@ export function createOverlayProvider() {
<OverlayContextProvider value={overlayState}>
{children}
{overlayState.overlayOrderList.map((item) => {
const { id: currentOverlayId, componentKey, isOpen, controller: Controller } = overlayState.overlayData[item];
const {
id: currentOverlayId,
componentKey,
isOpen,
controller: Controller,
isAsyncController,
asyncResult,
} = overlayState.overlayData[item];

return (
<ContentOverlayController
Expand All @@ -84,6 +93,8 @@ export function createOverlayProvider() {
controller={Controller}
overlayId={currentOverlayId}
overlayDispatch={overlayDispatch}
isAsyncController={isAsyncController}
asyncResult={asyncResult}
/>
);
})}
Expand Down
13 changes: 10 additions & 3 deletions packages/src/context/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type OverlayControllerComponent } from './provider/content-overlay-controller';
import { type OverlayAsyncResult, type OverlayControllerComponent } from './provider/content-overlay-controller';

type OverlayId = string;
type OverlayItem = {
Expand All @@ -14,6 +14,8 @@ type OverlayItem = {
isOpen: boolean;
isMounted: boolean;
controller: OverlayControllerComponent;
isAsyncController?: boolean;
asyncResult?: OverlayAsyncResult;
};
export type OverlayData = {
current: OverlayId | null;
Expand All @@ -24,7 +26,7 @@ export type OverlayData = {
export type OverlayReducerAction =
| { type: 'ADD'; overlay: OverlayItem }
| { type: 'OPEN'; overlayId: string }
| { type: 'CLOSE'; overlayId: string }
| { type: 'CLOSE'; overlayId: string; asyncResult?: OverlayAsyncResult }
| { type: 'REMOVE'; overlayId: string }
| { type: 'CLOSE_ALL' }
| { type: 'REMOVE_ALL' };
Expand Down Expand Up @@ -77,7 +79,7 @@ export function overlayReducer(state: OverlayData, action: OverlayReducerAction)
current: action.overlay.id,
overlayData: {
...state.overlayData,
[action.overlay.id]: { ...overlay, isOpen: true },
[action.overlay.id]: { ...overlay, isOpen: true, asyncResult: action.overlay.asyncResult },
},
};
}
Expand Down Expand Up @@ -123,6 +125,11 @@ export function overlayReducer(state: OverlayData, action: OverlayReducerAction)
case 'CLOSE': {
const overlay = state.overlayData[action.overlayId];

// A callback retained from an earlier openAsync call must not close the reopened overlay.
if ('asyncResult' in action && overlay?.asyncResult !== action.asyncResult) {
return state;
}

// ignore if the overlay don't exist or already closed
if (overlay == null || !overlay.isOpen) {
return state;
Expand Down
Loading