Svelte overlay
Fully customizable Svelte overlay which fits to available space
Great for creating dropdowns, tooltips and popovers
Svelte overlay is wrapper copmponent which makes all difficult stuff for you, but you decide when to open/close it and how it looks.
Features
- may be nested
- if content has not enough space on one side it will try to render on other side. For instance if position is set to
top-left
and there's no room on top position will be set tobottom-left
. This feature listens to window resize event - may be open/closed on every trigger and content event or from outside
- you decide how trigger and content looks
- may be closed on click outside, window keydown, or body scroll
- may update position on scroll
- dispatches toggle event when open state changed
REPL
Example<script> ;</script> <Overlay> <!-- TRIGGER --> <button slot="parent" let:toggle on:click=toggle> Click Me! </button> <!-- CONTENT --> <div slot="content"> Lorem ipsum dolor sit </div> </Overlay>
Installation
npm install --save svelte-overlay
or
yarn add svelte-overlay
Props
Important To get it work component requires two slots:
parent
content
Overlay props
Prop name | Type | Default | Description |
---|---|---|---|
isOpen | Boolean | false | isOpen state |
position | String | bottom-right |
top-left top-center top-right bottom-left bottom-center bottom-right left-top left-center left-bottom right-top right-center right-bottom Uses default position when wrong position was passed. You may import array of all positions with: import { positions } from 'svelte-overlay'; |
zIndex | Number | 1 | value of z-index for overlay and content |
class | string | "" | global class name |
style | string | "" | style string which will be added at the end of component style attribute |
closeOnClickOutside | Boolean | false | if true - click outside will close overlay |
closeOnScroll | Boolean | false | if true - scrolling outside content will close overlay |
updateOnScroll | Boolean | false | if true - scrolling will update content position |
onWindowKeyDown | Function | undefined | triggers when overlay is opened and user hit any button. Gets Event as first argument and object of { open, close, toggle, isOpen } |
on:toggle | Function | undefined | Event dispatched on overlay toggle. Gets Event as first argument and object of { open, close, toggle, isOpen } |
slot props
Each slot gets theese props, available through let:propName
Prop name | Type | Description |
---|---|---|
isOpen | Boolean | isOpen state |
toggle | Function | allows to toggle open state. Gets current isOpen value as argument |
open | Function | allows to open overlay |
close | Function | allows to close overlay |
Usage
REPL
Close on escape keydown and click outside<script> ; let isOpen = false; { if eventkey === 'Escape' isOpen = false; }</script> <Overlay onWindowKeyDown=handleWindowKeyDown closeOnClickOutside bind:isOpen=isOpen> <button slot="parent" let:toggle on:click=toggle> Click Me! </button> <div slot="content" let:close> <p>Lorem ipsum dolor sit</p> <button on:click=close>Close</button> </div> </Overlay>
REPL
Close from content<script> ;</script> <Overlay> <button slot="parent" let:toggle on:click=toggle> Click Me! </button> <div slot="content" let:close> <p>Lorem ipsum dolor sit</p> <button on:click=close>Close</button> </div> </Overlay>
REPL
Open/close on mouseenter/mouseleave<script> ;</script> <Overlay> <button slot="parent" let:open let:close on:mouseenter=open on:mouseleave=close> Hover Me! </button> <div slot="content"> Lorem ipsum dolor sit </div> </Overlay>
REPL
Open from outside<script> ; let isOpen = false; { isOpen = !isOpen; }</script> <button on:click=toggleFromOutside>Toggle from outside</button> <Overlay bind:isOpen=isOpen > <div slot="parent"> I am a parent </div> <div slot="content"> Lorem ipsum dolor sit </div> </Overlay>
REPL
With backdrop, disabled scroll and animations<script> ; ; let isOpen = false; { if eventdetail !== isOpen isOpen = eventdetail; documentbodyclassList; }</script> #if isOpen <div class="backdrop" transition:fade= duration: 200 />/if <Overlay on:toggle=handleToggle closeOnClickOutside zIndex=100 bind:isOpen=isOpen> <button slot="parent" let:toggle on:click=toggle>Click Me!</button> <div slot="content" transition:fly= y: 5 duration: 200 class="content"> Lorem ipsum dolor sit </div></Overlay> <style> : { overflow: hidden; } backdrop z-index: 99; position: fixed; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; background: ; content border: 1px solid #ddd; background: #f7f7f7; padding: 1em; width: max-content; </style>
REPL
Nested with click outside and close on esc key<script> ; let isOpen = false; { isOpen = !isOpen; }</script> <button on:click=toggleFromOutside>Toggle from outside</button> <Overlay bind:isOpen=isOpen > <div slot="parent"> I am a parent </div> <div slot="content"> Lorem ipsum dolor sit </div> </Overlay>