An open API service indexing awesome lists of open source software.

https://github.com/mattjennings/svelte-modals

A simple, flexible, zero-dependency modal stack manager for Svelte.
https://github.com/mattjennings/svelte-modals

Last synced: 30 days ago
JSON representation

A simple, flexible, zero-dependency modal stack manager for Svelte.

Awesome Lists containing this project

README

        

# svelte-modals

A simple, flexible, zero-dependency modal manager for Svelte.

[View documentation](https://svelte-modals.mattjennin.gs)

## Getting Started

### Installation

```bash
npm install svelte-modals
```

### Add \ to your app

All opened modals will be rendered inside ``. If you're using SvelteKit, `+layout.svelte` would be appropriate
place to put it. Otherwise, wherever you want the modals to be rendered.

```svelte

import { Modals } from 'svelte-modals'


{#snippet backdrop({ close })}

close()} />
{/snippet}

.backdrop {
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
background: rgba(0, 0, 0, 0.5);
}

```

### Create your Modal component

Let's create a basic modal component:

```svelte

const {
// provided by <Modals />
isOpen,
close,

// your props
title,
message
} = $props()

{#if isOpen}


{/if}

.modal {
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;

/* allow click-through to backdrop */
pointer-events: none;
}

.contents {
min-width: 240px;
padding: 16px;
background: white;
display: flex;
flex-direction: column;
justify-content: space-between;
pointer-events: auto;
}

h2 {
text-align: center;
font-size: 24px;
}

p {
text-align: center;
margin-top: 16px;
border-radius: 6px;
}

.actions {
margin-top: 32px;
display: flex;
justify-content: flex-end;
}

```

### Try it out

Import `modals` anywhere in your app to open or close your modals

```svelte

import { modals } from 'svelte-modals'
import MyModal from '$lib/components/MyModal.svelte'

function handleClick() {
modals.open(MyModal, { title: 'Alert', message: 'This is an alert' })
}

Open Modal
```