Modals wrapper for Svelte3
npm install --save-dev yasp-modals
View live there : https://svelte.dev/repl/1dde32baa79d4d80a9d30937c5cf48f8?version=3.20.1
<!-- App.svelte -->
<script>
import { Modals, openModal, closeModal } from 'yasp-modals';
import MySimpleModal from 'MySimpleModal.svelte';
function onClick() {
openModal(MySimpleModal, {
title: 'My Modal',
});
}
</script>
<div>
<a class="back" href="/">< Back to examples</a>
</div>
<main>Main content Blablabl</main>
<button on:click="{onClick}">Open Modal !</button>
<Modals />
<!-- MySimpleModal.svelte -->
<script>
import { fly } from "svelte/transition";
export let title = "My Modal Title";
export let closeModal;
let props = {};
let classname = "";
$: {
props = $$props;
if (props.class) {
classname = props.class;
}
delete props.class;
}
</script>
<style>
.modal {
min-width: 400px;
min-height: 400px;
background-color: white;
text-align: center;
display: flex;
flex-direction: column;
}
.modal__header,
.modal__footer {
background-color: darkcyan;
color: white;
padding: 20px;
}
h1 {
margin: 0;
}
.modal__body {
padding-top: 40px;
flex: 1 1 0;
}
</style>
<section
class={`modal ${classname}`}
{...props}
transition:fly={{ y: 100, duration: 400 }}>
<header class="modal__header">
<h1>{title}</h1>
</header>
<div class="modal__body">My modal content !</div>
<header class="modal__footer">
<button on:click={() => closeModal()}>Cancel</button>
</header>
</section>
This package provides 1 Component and 2 methods, an event emitter and one store to use
Components :
Methods:
emitter
$modals
<Modals {options?:object}>
Parameter | Optional | Description |
---|---|---|
options |
optional | Options for the modal layout and behavior |
options
close
showBtn
: boolean (default: true) - if a button to close the modal wrapper is added to the layout. Will have modals__close
for classnamebtnText
: string (default: 'close') - the text to show in the close buttononClickOutside
: boolean (default: true) - if the modals wrapper closes when clicking outside a modal contentonEscKey
: boolean (default true) - if the modals wrapper closes when the esc key is pressedtransition
type
: svelte/transition (default: fade) - the transition type for wrapper appearanceprops
: object (default: {}) - the transition propertiesprops
: objectstyle
: string (default: see src/components/Modals.svelte
) - the style to apply to the wrapper{...props}
to the wrapper elementopenModal(component:SvelteComponent, props?:object)
Opens a Modal
component
- the component to add to the Modal wrapperprops
: object (default: {}) - props to pass to the componentWhen a component is rendered, it will also have openModal and closeModal as properties
closeModal()
Closes the current modal
emitter is an EventEmitter.
Events:
open
({component, props}) - emitted when a Modal is openedbeforeClose
- emitted before closing a Modalclose
- emitted when a modal closesModals is a custom store.
It contains a reference to openModal
, closeModal
and the emitter
.
When the no modal is opened, $modals
as for value null
When a modal is opened, $modals
as for value {component, props}
The directory examples
contains examples of different usage of this package.
The best way to test the example(s) is to clone this repository and launch the examples quick server shipped within
git clone [email protected]:dievardump/yasp-modals.git
cd yasp-modals/examples
npm install
npm run dev
This should create a local server accessible to http://localhost:3333 (if you kept the default port)
Simon Fremaux / dievardump (dievardump@gmail.com)