aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/components/notification.svelte
blob: f7b172ec1524f8718557eb4f1bafd7682d78651b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<script lang="ts">
    import { Transition } from "@rgossiaux/svelte-headlessui";
    import { onDestroy } from "svelte";
    import { XMarkIcon, ExclamationCircleIcon, InformationCircleIcon, XCircleIcon, CheckCircleIcon } from "./icons";

    export let title: string;
    export let subtitle = "";
    export let show = false;
    export let type: "info" | "error" | "success" | "warning" = "info";
    export let hideAfterSeconds = -1;

    let timeout;
    let iconClass = "";
    let icon = undefined;

    onDestroy(() => {
        clearTimeout(timeout);
    });

    $: if (hideAfterSeconds > 0) {
        timeout = setTimeout(() => close(), hideAfterSeconds * 60);
    }
    $: switch (type) {
        case "error":
            iconClass = "text-red-400";
            icon = XCircleIcon;
            break;
        case "info":
            iconClass = "text-blue-400";
            icon = InformationCircleIcon;
            break;
        case "success":
            iconClass = "text-green-400";
            icon = CheckCircleIcon;
            break;
        case "warning":
            iconClass = "text-yellow-400";
            icon = ExclamationCircleIcon;
            break;
    }

    function close() {
        show = false;
    }
</script>

<div aria-live="assertive" class="pointer-events-none fixed inset-0 flex items-end px-4 py-6 sm:items-start sm:p-6">
    <div class="flex w-full flex-col items-center space-y-4 sm:items-end">
        <Transition
            class="w-full flex justify-end"
            {show}
            enter="transform ease-out duration-300 transition"
            enterFrom="translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2"
            enterTo="translate-y-0 opacity-100 sm:translate-x-0"
            leave="transition ease-in duration-100"
            leaveFrom="opacity-100"
            leaveTo="opacity-0"
        >
            <div class="pointer-events-auto w-full max-w-sm overflow-hidden rounded-lg bg-white shadow-lg ring-1 ring-black ring-opacity-5">
                <div class="p-4">
                    <div class="flex items-start">
                        <div class="flex-shrink-0">
                            <svelte:component this={icon} class={iconClass} />
                        </div>
                        <div class="ml-3 w-0 flex-1 pt-0.5">
                            <p class="text-sm font-medium text-gray-900">{title}</p>
                            {#if subtitle}
                                <p class="mt-1 text-sm text-gray-500">{subtitle}</p>
                            {/if}
                        </div>
                        <div class="ml-4 flex flex-shrink-0">
                            <button
                                on:click={close}
                                type="button"
                                class="inline-flex rounded-md bg-white text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
                            >
                                <XMarkIcon />
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </Transition>
    </div>
</div>