-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathAlert.svelte
More file actions
96 lines (86 loc) · 2.19 KB
/
Alert.svelte
File metadata and controls
96 lines (86 loc) · 2.19 KB
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
86
87
88
89
90
91
92
93
94
95
96
<script lang="ts">
import { fade } from "svelte/transition";
import { alert } from "./theme";
import clsx from "clsx";
import { type AlertProps, type ParamsType } from "$lib";
import CloseButton from "$lib/utils/CloseButton.svelte";
import { getTheme } from "$lib/theme/themeUtils";
import { createDismissableContext } from "$lib/utils/dismissable";
let {
children,
icon,
alertStatus = $bindable(true),
closeIcon: CloseIcon,
closeAriaLabel = "Remove alert",
color = "primary",
rounded = true,
border,
class: className,
dismissable,
transition = fade,
params,
...restProps
}: AlertProps = $props();
// Theme context
const theme = $derived(getTheme("alert"));
let divCls = $derived(
alert({
color,
rounded,
border,
icon: !!icon,
dismissable,
class: clsx(theme, className)
})
);
let ref: HTMLDivElement | undefined = $state(undefined);
function close() {
if (ref?.dispatchEvent(new Event("close", { bubbles: true, cancelable: true }))) {
alertStatus = false;
}
}
createDismissableContext(close);
</script>
{#if alertStatus}
<div role="alert" bind:this={ref} {...restProps} transition:transition={params as ParamsType} class={divCls}>
{#if icon}
{@render icon()}
{/if}
{#if icon || dismissable}
<div>
{@render children()}
</div>
{:else}
{@render children()}
{/if}
{#if dismissable}
{#if CloseIcon}
<CloseButton class="-my-1.5 ms-auto -me-1.5" {color} ariaLabel={closeAriaLabel}>
<CloseIcon />
</CloseButton>
{:else}
<CloseButton class="-my-1.5 ms-auto -me-1.5" {color} ariaLabel={closeAriaLabel} />
{/if}
{/if}
</div>
{/if}
<!--
@component
[Go to docs](https://flowbite-svelte.com/)
## Type
[AlertProps](https://github.com/themesberg/flowbite-svelte/blob/main/src/lib/types.ts#L221)
## Props
@prop children
@prop icon
@prop alertStatus = $bindable(true)
@prop closeIcon: CloseIcon
@prop closeAriaLabel = "Remove alert"
@prop color = "primary"
@prop rounded = true
@prop border
@prop class: className
@prop dismissable
@prop transition = fade
@prop params
@prop ...restProps
-->