Svelte Modals does not provide any modal components, it is only responsible for
managing what is opened and closed. All other functionality and styling is up to you.
By default, modals are opened using a “last in first out” stack (this can be customized).
The top modal is the only one that is visible, while the others below remain mounted but hidden determined by an isOpen prop.
Opening Modals
To open a modal, import modals and call modals.open(YourModalComponent, props).
Stacking Modals
You can call modals.open any time to open a new modal. This will add the new modal to the top of the stack,
keeping the previous modals mounted but updating their isOpen prop to false.
Closing Modals
From anywhere in your app you can call modals.close() to close the top modal, modals.close(amount) to
close the last given number of modals, or modals.closeAll() to close all modals.
The close() Prop
Modals receive a close prop which will close the modal and resolve
their corresponding modals.open with the given value.
If you are using Typescript, you can define the type of the value by using the ModalProps interface.
Transitions
Transitions can be added to your modal components just like any other Svelte component. However they
must be global transitions because they are mounted and unmounted by the ModalStack component.
Exit before enter
By default, transitions for both opening and closing modals will play at the same time.
Depending on your transition this might be ok, but often it’s cleaner to play one at a time.
To opt-in to this behaviour, you can use the exitBeforeEnter action on the element that has the transitions.
Lazy Loading
Modal components can be lazy loaded with dynamic imports
While the component is being imported, the <ModalStack /> component will render backdrop and loading snippets.
Snippets
Snippets can be passed in as props the same way you would with a regular Svelte 5 component.
You can also use Svelte’s createRawSnippet if you wish
Props
Modal components receive the following props:
If you’re using Typescript, you should use this interface to define the props of your modal components.