From e08d7fe3a610b74b2ea51dfcbbea38205fb8b5dd Mon Sep 17 00:00:00 2001 From: Charles Mangwa Date: Sat, 18 Jun 2022 07:59:19 +0200 Subject: [PATCH] fix: do not queue any `closingAction` if there are no opened modal Fixes #81. --- src/lib/ModalState.ts | 10 ++++++++-- src/types.ts | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/lib/ModalState.ts b/src/lib/ModalState.ts index da78132..7ba04a1 100644 --- a/src/lib/ModalState.ts +++ b/src/lib/ModalState.ts @@ -11,6 +11,7 @@ import type { ModalStateEqualityChecker, ModalPendingClosingAction, ModalState as ModalStateType, + ModalStatePendingClosingAction, } from '../types' import { invariant, getStackItemOptions, defaultOptions } from '../utils' @@ -247,11 +248,11 @@ const createModalState = (): ModalStateType => { return false } - const queueClosingAction =

({ + const queueClosingAction = ({ action, callback, modalName, - }: ModalStateType

['queueClosingAction']['arguments']): ModalStateType

['queueClosingAction']['arguments'] => { + }: ModalStatePendingClosingAction): ModalPendingClosingAction | null => { const { stack: { names }, } = state @@ -265,12 +266,17 @@ const createModalState = (): ModalStateType => { ) } + const noOpenedItems = !state?.stack?.openedItems?.size + + if (noOpenedItems) return null + const hash = `${modalName ? `${modalName}_${action}` : action}_${Math.random().toString(36).substring(2, 11)}` const { pendingClosingActions } = setState(currentState => ({ ...currentState, stack: { ...currentState.stack, + // @ts-ignore pendingClosingActions: currentState.stack.pendingClosingActions.add({ hash, action, diff --git a/src/types.ts b/src/types.ts index fa451f4..708088e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -46,6 +46,23 @@ export type ModalEventPayload = { handler: ModalEventCallback } +export type ModalStatePendingClosingAction = + | { + modalName?: string + action: 'closeModal' + callback?: () => void + } + | { + modalName: string + action: 'closeModals' + callback?: () => void + } + | { + modalName?: never + action: 'closeAllModals' + callback?: () => void + } + export type ModalPendingClosingAction = | { hash: string @@ -63,6 +80,7 @@ export type ModalPendingClosingAction = } | { hash: string + modalName?: never currentModalHash?: string action: 'closeAllModals' callback?: () => void @@ -144,7 +162,7 @@ export type ModalState

= Omit, 'currentModal' | 'stac listener: ModalStateListener, equalityFn?: ModalStateEqualityChecker, ) => ModalStateSubscription - queueClosingAction: (action: Partial) => ModalPendingClosingAction + queueClosingAction: (action: ModalStatePendingClosingAction) => ModalPendingClosingAction | null removeClosingAction: (action: ModalPendingClosingAction) => boolean }