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

74 lines
1.8 KiB
Svelte
Raw Normal View History

2023-10-30 04:52:00 +00:00
<script lang="ts">
2021-08-28 22:44:06 +03:00
import { fly } from 'svelte/transition';
2023-11-01 09:45:40 +00:00
import { InlineNotification } from 'carbon-components-svelte';
import { capitalizeFirstLetter } from '$lib/utils/functions/string';
2021-08-28 22:44:06 +03:00
2023-11-01 09:45:40 +00:00
import { SNACKBAR_SEVERITY } from './constants';
2021-12-30 22:30:30 +02:00
import { snackbarStore, snackbarStoreInitialState } from './store';
2021-08-28 22:44:06 +03:00
2023-10-30 04:52:00 +00:00
let timeoutId: NodeJS.Timeout | undefined;
2023-11-01 09:45:40 +00:00
let kind: 'error' | 'info' | 'info-square' | 'success' | 'warning' | 'warning-alt' = 'info';
2021-08-28 22:44:06 +03:00
function handleClose() {
2023-10-30 04:52:00 +00:00
if (typeof $snackbarStore.handleClose === 'function') {
2021-08-28 22:44:06 +03:00
$snackbarStore.handleClose();
}
2023-05-02 23:55:26 +00:00
snackbarStore.update((_s) => ({
..._s,
...snackbarStoreInitialState
2023-05-02 23:55:26 +00:00
}));
clearTimeout(timeoutId);
2023-10-30 04:52:00 +00:00
timeoutId = undefined;
2021-08-28 22:44:06 +03:00
}
// When open is true trigger autoHideDuration
2023-10-30 04:52:00 +00:00
function handleOpen(open: boolean) {
2021-08-28 22:44:06 +03:00
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);
2023-11-01 09:45:40 +00:00
$: kind =
$snackbarStore.severity === SNACKBAR_SEVERITY.SUCCESS
? 'success'
: $snackbarStore.severity === SNACKBAR_SEVERITY.ERROR
? 'error'
: 'info';
2021-08-28 22:44:06 +03:00
</script>
{#if $snackbarStore.open}
2021-09-17 07:48:26 +03:00
{#key $snackbarStore.message}
2023-11-01 09:45:40 +00:00
<div transition:fly={{ x: 200, duration: 500 }} class="root absolute">
<InlineNotification
{kind}
title={capitalizeFirstLetter(kind || '')}
subtitle={$snackbarStore.message}
on:close={(e) => {
e.preventDefault();
handleClose();
}}
/>
2021-09-17 07:48:26 +03:00
</div>
{/key}
2021-08-28 22:44:06 +03:00
{/if}
<style>
.root {
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;
z-index: 51;
2021-08-28 22:44:06 +03:00
}
</style>