Files
classroomio/src/components/Snackbar/index.svelte
T

80 lines
2.2 KiB
Svelte
Raw Normal View History

2021-08-28 22:44:06 +03:00
<script>
import { fly } from 'svelte/transition';
import Information24 from 'carbon-icons-svelte/lib/Information24';
import Warning24 from 'carbon-icons-svelte/lib/Warning24';
import CheckmarkOutline24 from 'carbon-icons-svelte/lib/CheckmarkOutline24';
import CloseButton from '../Buttons/Close/index.svelte';
import { SNACKBAR_SEVERITY, SNACKBAR_SEVERITY_COLOR } from './constants';
2021-12-30 22:30:30 +02:00
import { snackbarStore, snackbarStoreInitialState } from './store';
2021-08-28 22:44:06 +03:00
2021-09-05 22:16:42 +03:00
let severityColor;
2023-05-02 23:55:26 +00:00
let timeoutId;
2021-08-28 22:44:06 +03:00
function handleClose() {
if (typeof $snackbarStore.handleClose === 'number') {
$snackbarStore.handleClose();
}
2023-05-02 23:55:26 +00:00
snackbarStore.update((_s) => ({
..._s,
...snackbarStoreInitialState,
}));
clearTimeout(timeoutId);
timeoutId = null;
2021-08-28 22:44:06 +03:00
}
// When open is true trigger autoHideDuration
function handleOpen(open) {
if (!open) {
return;
}
const { autoHideDuration } = $snackbarStore;
2023-05-02 23:55:26 +00:00
if (typeof autoHideDuration === 'number' && !timeoutId) {
clearTimeout(timeoutId);
timeoutId = setTimeout(handleClose, autoHideDuration);
2021-08-28 22:44:06 +03:00
}
}
$: handleOpen($snackbarStore.open);
2021-09-05 22:16:42 +03:00
$: severityColor = SNACKBAR_SEVERITY_COLOR[$snackbarStore.severity];
2021-08-28 22:44:06 +03:00
</script>
{#if $snackbarStore.open}
2021-09-17 07:48:26 +03:00
{#key $snackbarStore.message}
<div
transition:fly={{ x: 200, duration: 500 }}
class="root text-white rounded-md flex justify-between items-center absolute {severityColor}"
>
<div class="flex items-center">
{#if $snackbarStore.severity === SNACKBAR_SEVERITY.SUCCESS}
<CheckmarkOutline24 class="carbon-icon" />
{:else if $snackbarStore.severity === SNACKBAR_SEVERITY.WARNING}
<Warning24 class="carbon-icon" />
{:else}
<Information24 class="carbon-icon" />
{/if}
<p class="ml-2 text-lg">{$snackbarStore.message}</p>
</div>
2021-08-28 22:44:06 +03:00
2021-09-17 07:48:26 +03:00
<CloseButton onClick={handleClose} contained={true} color="text-white" />
</div>
{/key}
2021-08-28 22:44:06 +03:00
{/if}
<style>
.root {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
2021-09-17 07:48:26 +03:00
right: 5%;
left: auto;
top: 24px;
transform: translateX(-5%);
2021-08-28 22:44:06 +03:00
min-width: 288px;
padding: 6px 16px;
flex-wrap: wrap;
z-index: 51;
2021-08-28 22:44:06 +03:00
}
</style>