mirror of
https://github.com/BradNut/svelte-library
synced 2025-09-08 17:40:21 +00:00
Created click outside action event.
This commit is contained in:
parent
32b7a187e0
commit
da1eaf278a
3 changed files with 36 additions and 5 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
<script>
|
<script>
|
||||||
import { fly, fade } from 'svelte/transition';
|
import { fly, fade } from 'svelte/transition';
|
||||||
import Portal from './Portal.svelte';
|
import Portal from './Portal.svelte';
|
||||||
|
import { clickOutside } from './clickOutside';
|
||||||
|
|
||||||
export let isModalOpen = false;
|
export let isModalOpen = false;
|
||||||
|
export let background = true;
|
||||||
|
|
||||||
function closeModal() {
|
function closeModal() {
|
||||||
isModalOpen = false;
|
isModalOpen = false;
|
||||||
|
|
@ -11,11 +13,18 @@
|
||||||
|
|
||||||
{#if isModalOpen}
|
{#if isModalOpen}
|
||||||
<Portal>
|
<Portal>
|
||||||
<div class="modal-wrapper" transition:fly={{ opacity: 0, y: 100 }}>
|
<div
|
||||||
<button on:click={closeModal} aria-label="Close Modal Box">Close</button>
|
use:clickOutside
|
||||||
|
on:click-outside={closeModal}
|
||||||
|
class="modal-wrapper"
|
||||||
|
transition:fly={{ opacity: 0, y: 100 }}
|
||||||
|
>
|
||||||
|
<button class="close-btn" on:click={closeModal} aria-label="Close Modal Box">Close</button>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
<div on:click={closeModal} transition:fade class="background" />
|
{#if background}
|
||||||
|
<div on:click={closeModal} transition:fade class="background" />
|
||||||
|
{/if}
|
||||||
</Portal>
|
</Portal>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
|
@ -37,4 +46,10 @@
|
||||||
position: fixed;
|
position: fixed;
|
||||||
z-index: 100;
|
z-index: 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.close-btn {
|
||||||
|
position: absolute;
|
||||||
|
top: -10px;
|
||||||
|
right: -10px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
16
src/lib/clickOutside.js
Normal file
16
src/lib/clickOutside.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
export function clickOutside(node) {
|
||||||
|
function handleClick(event) {
|
||||||
|
if(node && !node.contains(event.target) && !event.defaultPrevented) {
|
||||||
|
node.dispatchEvent(
|
||||||
|
new CustomEvent('click-outside')
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('click', handleClick, true);
|
||||||
|
|
||||||
|
return {
|
||||||
|
destroy() {
|
||||||
|
document.removeEventListener('click', handleClick, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
<h1>Welcome to Level Up UI {search}</h1>
|
<h1>Welcome to Level Up UI {search}</h1>
|
||||||
|
|
||||||
<Modal {isModalOpen}>
|
<Modal bind:isModalOpen background={true}>
|
||||||
<div style="background: white; box-shadow: 1px 1px 4px rgba(0,0,0,0.3);">
|
<div style="background: white; box-shadow: 1px 1px 4px rgba(0,0,0,0.3);">
|
||||||
<Field bind:value={search} label="Search" insrustions="Type to search" placeholder="Joe" />
|
<Field bind:value={search} label="Search" insrustions="Type to search" placeholder="Joe" />
|
||||||
<Field value={0} label="Number" type="number" />
|
<Field value={0} label="Number" type="number" />
|
||||||
|
|
@ -34,7 +34,7 @@
|
||||||
>Error Message</button
|
>Error Message</button
|
||||||
>
|
>
|
||||||
|
|
||||||
<!-- <button on:click={() => (isModalOpen = true)}>Open Modal Form</button> -->
|
<button on:click={() => (isModalOpen = true)}>Open Modal Form</button>
|
||||||
|
|
||||||
<SearchFilter {items} bind:search />
|
<SearchFilter {items} bind:search />
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue