Skip to content

Commit

Permalink
allow closing modals during transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjennings committed Nov 3, 2024
1 parent ea1a12f commit 45cab91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/lib/components/LegacyAnimatedInfiniteModal.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { createEventDispatcher } from 'svelte'
import { closeModal, modals } from 'svelte-modals/legacy'
import { closeAllModals, closeModal, modals } from 'svelte-modals/legacy'
import { fade, fly } from 'svelte/transition'
const dispatch = createEventDispatcher()
Expand Down Expand Up @@ -46,6 +46,7 @@
<div class="modal-actions">
<button on:click={openAnother}>Open</button>
<button on:click={closeModal}>Close</button>
<button on:click={closeAllModals}>Close All</button>
</div>
</div>
</div>
Expand Down
4 changes: 0 additions & 4 deletions src/lib/modal.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export class Modal<R = any> {
}

close(...args: R extends void ? [] : [result: R]) {
if (this.modals.transitioning) {
return false
}

if (this.onBeforeClose?.() === false) {
return false
}
Expand Down
20 changes: 20 additions & 0 deletions src/tests/modals.spec.ts → src/tests/modal-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const FakeComponent = class {} as any as ModalComponent<ModalProps<{ foo: 'bar'

afterEach(() => {
modals.stack.length = 0
modals.transitioning = false
})

describe('open', () => {
Expand Down Expand Up @@ -55,6 +56,25 @@ describe('close', () => {
})
})

describe('closeAll', () => {
test('removes all modals from the stack', () => {
modals.open(FakeComponent)
modals.open(FakeComponent)
modals.open(FakeComponent)
modals.closeAll()
expect(modals.stack).toHaveLength(0)
})

test('removes all modals from the stack even if transitioning', () => {
modals.open(FakeComponent)
modals.stack[0].exitBeforeEnter = true
modals.open(FakeComponent)
modals.open(FakeComponent)
modals.closeAll()
expect(modals.stack).toHaveLength(0)
})
})

describe('closeById', () => {
test('removes a modal from the stack', () => {
modals.open(FakeComponent)
Expand Down

0 comments on commit 45cab91

Please sign in to comment.