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

80 lines
2.4 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';
import InformationIcon from 'carbon-icons-svelte/lib/Information.svelte';
import WarningIcon from 'carbon-icons-svelte/lib/Warning.svelte';
import CheckmarkOutline24 from 'carbon-icons-svelte/lib/CheckmarkOutline.svelte';
2021-08-28 22:44:06 +03:00
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
2023-10-30 04:52:00 +00:00
let severityColor: string;
let timeoutId: NodeJS.Timeout | undefined;
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);
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 size={24} class="carbon-icon dark:text-white" />
2021-09-17 07:48:26 +03:00
{:else if $snackbarStore.severity === SNACKBAR_SEVERITY.WARNING}
<WarningIcon size={24} class="carbon-icon dark:text-white" />
2021-09-17 07:48:26 +03:00
{:else}
<InformationIcon size={24} class="carbon-icon dark:text-white" />
2021-09-17 07:48:26 +03:00
{/if}
<p class="dark:text-white ml-2 text-lg">{$snackbarStore.message}</p>
2021-09-17 07:48:26 +03:00
</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>