feat: add errorcontainer to move error messages to a better place

This commit is contained in:
Xylight committed 2024-09-01 14:55:10 -07:00
1 parent 60004f03a7
commit e097df959b
3 files changed
+108 -18

No files matched your search

@@ -0,0 +1,77 @@
<script lang="ts" context="module">
import { writable } from 'svelte/store'
interface PageScopedError {
scope: string
message: string
}
export let errors = writable<PageScopedError[]>([
{
scope: '/login',
message: 'AAAAAAAAAAAAAAAAA',
},
])
export function pushError(args: { scope: string; message: string }) {
errors.update((e) => [...e, args])
}
export function clearErrorScope(scope: string | null | undefined) {
errors.update((errors) => errors.filter((error) => error.scope != scope))
}
</script>
<script lang="ts">
import { Button } from 'mono-svelte'
import { ExclamationCircle, Icon, XCircle, XMark } from 'svelte-hero-icons'
import { fly, slide } from 'svelte/transition'
import { expoOut } from 'svelte/easing'
import { onDestroy } from 'svelte'
export let scope: string | undefined | null = undefined
$: scopedErrors = $errors.filter(
(e) => e.scope == scope || e.scope == 'global'
)
onDestroy(() => {
clearErrorScope(scope)
})
</script>
{#if scopedErrors.length > 0}
<div
class={$$props.class}
in:slide={{ duration: 400, easing: expoOut }}
out:slide={{ duration: 400, delay: 400, easing: expoOut }}
>
<div
class="flex flex-row p-1.5 bg-red-500/20 rounded-3xl border border-red-500/50 gap-2 items-center"
in:fly={{ y: -8, duration: 400, delay: 200, opacity: 0, easing: expoOut }}
out:fly={{ y: -8, duration: 400, opacity: 0, easing: expoOut }}
>
<div class="rounded-full p-0.5 bg-red-400">
<Icon
src={XMark}
class="text-red-500 dark:text-red-400 p-px rounded-full bg-white dark:bg-zinc-950"
size="20"
micro
/>
</div>
<div class="flex flex-col text-sm">
{#each scopedErrors as error}
{error.message}
{/each}
</div>
<Button
on:click={() => clearErrorScope(scope)}
color="tertiary"
size="square-sm"
class="ml-auto"
>
<Icon src={XMark} size="16" micro />
</Button>
</div>
</div>
{/if}