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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
import { Toast as Kobalte, toaster } from "@kobalte/core/toast"
import type { ToastRootProps, ToastCloseButtonProps, ToastTitleProps, ToastDescriptionProps } from "@kobalte/core/toast"
import type { ComponentProps, JSX } from "solid-js"
import { Show } from "solid-js"
import { Portal } from "solid-js/web"
import { useI18n } from "../context/i18n"
import { Icon, type IconProps } from "./icon"
import { IconButton } from "./icon-button"
export interface ToastRegionProps extends ComponentProps<typeof Kobalte.Region> {}
function ToastRegion(props: ToastRegionProps) {
return (
<Portal>
<Kobalte.Region data-component="toast-region" {...props}>
<Kobalte.List data-slot="toast-list" />
</Kobalte.Region>
</Portal>
)
}
export interface ToastRootComponentProps extends ToastRootProps {
class?: string
classList?: ComponentProps<"li">["classList"]
children?: JSX.Element
}
function ToastRoot(props: ToastRootComponentProps) {
return (
<Kobalte
data-component="toast"
classList={{
...props.classList,
[props.class ?? ""]: !!props.class,
}}
{...props}
/>
)
}
function ToastIcon(props: { name: IconProps["name"] }) {
return (
<div data-slot="toast-icon">
<Icon name={props.name} />
</div>
)
}
function ToastContent(props: ComponentProps<"div">) {
return <div data-slot="toast-content" {...props} />
}
function ToastTitle(props: ToastTitleProps & ComponentProps<"div">) {
return <Kobalte.Title data-slot="toast-title" {...props} />
}
function ToastDescription(props: ToastDescriptionProps & ComponentProps<"div">) {
return <Kobalte.Description data-slot="toast-description" {...props} />
}
function ToastActions(props: ComponentProps<"div">) {
return <div data-slot="toast-actions" {...props} />
}
function ToastCloseButton(props: ToastCloseButtonProps & ComponentProps<"button">) {
const i18n = useI18n()
return (
<Kobalte.CloseButton
data-slot="toast-close-button"
as={IconButton}
icon="close"
variant="ghost"
aria-label={i18n.t("ui.common.dismiss")}
{...props}
/>
)
}
function ToastProgressTrack(props: ComponentProps<typeof Kobalte.ProgressTrack>) {
return <Kobalte.ProgressTrack data-slot="toast-progress-track" {...props} />
}
function ToastProgressFill(props: ComponentProps<typeof Kobalte.ProgressFill>) {
return <Kobalte.ProgressFill data-slot="toast-progress-fill" {...props} />
}
export const Toast = Object.assign(ToastRoot, {
Region: ToastRegion,
Icon: ToastIcon,
Content: ToastContent,
Title: ToastTitle,
Description: ToastDescription,
Actions: ToastActions,
CloseButton: ToastCloseButton,
ProgressTrack: ToastProgressTrack,
ProgressFill: ToastProgressFill,
})
export { toaster }
export type ToastVariant = "default" | "success" | "error" | "loading"
export interface ToastAction {
label: string
onClick: "dismiss" | (() => void)
}
export interface ToastOptions {
title?: string
description?: string
icon?: IconProps["name"]
variant?: ToastVariant
duration?: number
persistent?: boolean
actions?: ToastAction[]
}
export function showToast(options: ToastOptions | string) {
const opts = typeof options === "string" ? { description: options } : options
return toaster.show((props) => (
<Toast
toastId={props.toastId}
duration={opts.duration}
persistent={opts.persistent}
data-variant={opts.variant ?? "default"}
>
<Show when={opts.icon}>
<Toast.Icon name={opts.icon!} />
</Show>
<Toast.Content>
<Show when={opts.title}>
<Toast.Title>{opts.title}</Toast.Title>
</Show>
<Show when={opts.description}>
<Toast.Description>{opts.description}</Toast.Description>
</Show>
<Show when={opts.actions?.length}>
<Toast.Actions>
{opts.actions!.map((action) => (
<button
data-slot="toast-action"
onClick={() => {
if (typeof action.onClick === "function") {
action.onClick()
}
toaster.dismiss(props.toastId)
}}
>
{action.label}
</button>
))}
</Toast.Actions>
</Show>
</Toast.Content>
<Toast.CloseButton />
</Toast>
))
}
export interface ToastPromiseOptions<T, U = unknown> {
loading?: JSX.Element
success?: (data: T) => JSX.Element
error?: (error: U) => JSX.Element
}
export function showPromiseToast<T, U = unknown>(
promise: Promise<T> | (() => Promise<T>),
options: ToastPromiseOptions<T, U>,
) {
return toaster.promise(promise, (props) => (
<Toast
toastId={props.toastId}
data-variant={props.state === "pending" ? "loading" : props.state === "fulfilled" ? "success" : "error"}
>
<Toast.Content>
<Toast.Description>
{props.state === "pending" && options.loading}
{props.state === "fulfilled" && options.success?.(props.data!)}
{props.state === "rejected" && options.error?.(props.error)}
</Toast.Description>
</Toast.Content>
<Toast.CloseButton />
</Toast>
))
}
|