Updating toast component to have dismissible and timing options.

This commit is contained in:
Bradley Shellnut 2022-07-26 17:23:28 -07:00
parent 0fcf8cd858
commit bb1f51dcfd
3 changed files with 49 additions and 18 deletions

View file

@ -8,28 +8,39 @@
<Portal>
<div class="toast-wrapper">
{#each $toast as message (message.id)}
{#each $toast as toastData (toastData.id)}
<div
on:click={() => toast.remove(message.id)}
aria-label={toastData.dismissible ? "Click to dismiss" : `${toastData.message}`}
on:click={() => toastData.dismissible && toast.remove(toastData.id)}
in:fly={{ opacity: 0, x: 100 }}
out:fade
animate:flip
class={`toast ${message.type.toLowerCase()}`}
class={`toast ${toastData.type.toLowerCase()}`}
>
<ToastMessage {message} />
<ToastMessage {toastData} />
{#if toastData.dismissible && toastData.showButton}
<button type="button" aria-label="Click to dismiss" on:click={() => toastData.dismissible && toast.remove(toastData.id)}
class="close">
<svg viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M11.7816 4.03157C12.0062 3.80702 12.0062 3.44295 11.7816 3.2184C11.5571 2.99385 11.193 2.99385 10.9685 3.2184L7.50005 6.68682L4.03164 3.2184C3.80708 2.99385 3.44301 2.99385 3.21846 3.2184C2.99391 3.44295 2.99391 3.80702 3.21846 4.03157L6.68688 7.49999L3.21846 10.9684C2.99391 11.193 2.99391 11.557 3.21846 11.7816C3.44301 12.0061 3.80708 12.0061 4.03164 11.7816L7.50005 8.31316L10.9685 11.7816C11.193 12.0061 11.5571 12.0061 11.7816 11.7816C12.0062 11.557 12.0062 11.193 11.7816 10.9684L8.31322 7.49999L11.7816 4.03157Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path></svg>
</button>
{/if}
</div>
{/each}
</div>
</Portal>
<style>
<style lang="scss">
.toast-wrapper {
position: fixed;
bottom: 0;
right: 20px;
bottom: 5px;
right: 25px;
}
.toast {
display: flex;
gap: 1rem;
place-items: center;
overflow: hidden;
position: relative;
margin-bottom: 1rem;
@ -38,6 +49,18 @@
padding: 20px;
border-radius: 15px;
box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.3);
.close {
padding: 5px;
border-radius: 5px;
opacity: 1;
transition: ease 0.3s;
&:hover, &:focus {
transition: ease 0.3s;
background-color: #625df5;
}
}
}
.toast.error {

View file

@ -1,21 +1,24 @@
<script lang="ts">
import type { ToastData } from '$lib/types';
import { onMount } from 'svelte';
import { tweened } from 'svelte/motion';
import type { ToastData } from '$lib/types';
import { toast } from './toast';
export let toastData: ToastData;
let progress = tweened(100, { duration: toastData.duration });
onMount(async () => {
await progress.set(0);
toast.remove(toastData.id);
if (toastData.autoDismiss) {
await progress.set(0);
toast.remove(toastData.id);
}
});
</script>
<div class="progress" style={`width: ${$progress}%;`} />
{#if toastData.autoDismiss}
<div class="progress" style={`width: ${$progress}%;`} />
{/if}
<p>{toastData.message}</p>
<style>

View file

@ -1,15 +1,20 @@
import type { ToastData } from '$lib/types';
import { writable } from 'svelte/store';
import type { ToastData } from '$lib/types';
import { ToastType } from '$lib/types';
// Custom store
const newToast = () => {
const { subscribe, update } = writable([]);
const { subscribe, update } = writable<ToastData[]>([]);
function send(message: string, { duration = 2000, type = 'INFO' } = {}) {
function send(message: string, { duration = 2000, type = ToastType.INFO, autoDismiss = true, dismissible = false, showButton = false } = {}) {
const id = Math.floor(Math.random() * 1000);
const newMessage = {
const newMessage: ToastData = {
id,
duration,
autoDismiss,
dismissible,
showButton,
type,
message
};