Skip to content

Commit

Permalink
fix: do not queue any closingAction if there are no opened modal
Browse files Browse the repository at this point in the history
Fixes #81.
  • Loading branch information
CharlesMangwa committed Jun 18, 2022
1 parent a7d5138 commit e08d7fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/lib/ModalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ModalStateEqualityChecker,
ModalPendingClosingAction,
ModalState as ModalStateType,
ModalStatePendingClosingAction,
} from '../types'

import { invariant, getStackItemOptions, defaultOptions } from '../utils'
Expand Down Expand Up @@ -247,11 +248,11 @@ const createModalState = (): ModalStateType<any> => {
return false
}

const queueClosingAction = <P>({
const queueClosingAction = ({
action,
callback,
modalName,
}: ModalStateType<P>['queueClosingAction']['arguments']): ModalStateType<P>['queueClosingAction']['arguments'] => {
}: ModalStatePendingClosingAction): ModalPendingClosingAction | null => {
const {
stack: { names },
} = state
Expand All @@ -265,12 +266,17 @@ const createModalState = (): ModalStateType<any> => {
)
}

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,
Expand Down
20 changes: 19 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -63,6 +80,7 @@ export type ModalPendingClosingAction =
}
| {
hash: string
modalName?: never
currentModalHash?: string
action: 'closeAllModals'
callback?: () => void
Expand Down Expand Up @@ -144,7 +162,7 @@ export type ModalState<P> = Omit<ModalContextProvider<P>, 'currentModal' | 'stac
listener: ModalStateListener<T>,
equalityFn?: ModalStateEqualityChecker<T>,
) => ModalStateSubscription<T>
queueClosingAction: (action: Partial<ModalPendingClosingAction>) => ModalPendingClosingAction
queueClosingAction: (action: ModalStatePendingClosingAction) => ModalPendingClosingAction | null
removeClosingAction: (action: ModalPendingClosingAction) => boolean
}

Expand Down

0 comments on commit e08d7fe

Please sign in to comment.