toast.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Toast as Kobalte, toaster } from "@kobalte/core/toast"
  2. import type { ToastRootProps, ToastCloseButtonProps, ToastTitleProps, ToastDescriptionProps } from "@kobalte/core/toast"
  3. import type { ComponentProps, JSX } from "solid-js"
  4. import { Show } from "solid-js"
  5. import { Portal } from "solid-js/web"
  6. import { useI18n } from "../context/i18n"
  7. import { Icon, type IconProps } from "./icon"
  8. import { IconButton } from "./icon-button"
  9. export interface ToastRegionProps extends ComponentProps<typeof Kobalte.Region> {}
  10. function ToastRegion(props: ToastRegionProps) {
  11. return (
  12. <Portal>
  13. <Kobalte.Region data-component="toast-region" {...props}>
  14. <Kobalte.List data-slot="toast-list" />
  15. </Kobalte.Region>
  16. </Portal>
  17. )
  18. }
  19. export interface ToastRootComponentProps extends ToastRootProps {
  20. class?: string
  21. classList?: ComponentProps<"li">["classList"]
  22. children?: JSX.Element
  23. }
  24. function ToastRoot(props: ToastRootComponentProps) {
  25. return (
  26. <Kobalte
  27. data-component="toast"
  28. classList={{
  29. ...props.classList,
  30. [props.class ?? ""]: !!props.class,
  31. }}
  32. {...props}
  33. />
  34. )
  35. }
  36. function ToastIcon(props: { name: IconProps["name"] }) {
  37. return (
  38. <div data-slot="toast-icon">
  39. <Icon name={props.name} />
  40. </div>
  41. )
  42. }
  43. function ToastContent(props: ComponentProps<"div">) {
  44. return <div data-slot="toast-content" {...props} />
  45. }
  46. function ToastTitle(props: ToastTitleProps & ComponentProps<"div">) {
  47. return <Kobalte.Title data-slot="toast-title" {...props} />
  48. }
  49. function ToastDescription(props: ToastDescriptionProps & ComponentProps<"div">) {
  50. return <Kobalte.Description data-slot="toast-description" {...props} />
  51. }
  52. function ToastActions(props: ComponentProps<"div">) {
  53. return <div data-slot="toast-actions" {...props} />
  54. }
  55. function ToastCloseButton(props: ToastCloseButtonProps & ComponentProps<"button">) {
  56. const i18n = useI18n()
  57. return (
  58. <Kobalte.CloseButton
  59. data-slot="toast-close-button"
  60. as={IconButton}
  61. icon="close"
  62. variant="ghost"
  63. aria-label={i18n.t("ui.common.dismiss")}
  64. {...props}
  65. />
  66. )
  67. }
  68. function ToastProgressTrack(props: ComponentProps<typeof Kobalte.ProgressTrack>) {
  69. return <Kobalte.ProgressTrack data-slot="toast-progress-track" {...props} />
  70. }
  71. function ToastProgressFill(props: ComponentProps<typeof Kobalte.ProgressFill>) {
  72. return <Kobalte.ProgressFill data-slot="toast-progress-fill" {...props} />
  73. }
  74. export const Toast = Object.assign(ToastRoot, {
  75. Region: ToastRegion,
  76. Icon: ToastIcon,
  77. Content: ToastContent,
  78. Title: ToastTitle,
  79. Description: ToastDescription,
  80. Actions: ToastActions,
  81. CloseButton: ToastCloseButton,
  82. ProgressTrack: ToastProgressTrack,
  83. ProgressFill: ToastProgressFill,
  84. })
  85. export { toaster }
  86. export type ToastVariant = "default" | "success" | "error" | "loading"
  87. export interface ToastAction {
  88. label: string
  89. onClick: "dismiss" | (() => void)
  90. }
  91. export interface ToastOptions {
  92. title?: string
  93. description?: string
  94. icon?: IconProps["name"]
  95. variant?: ToastVariant
  96. duration?: number
  97. persistent?: boolean
  98. actions?: ToastAction[]
  99. }
  100. export function showToast(options: ToastOptions | string) {
  101. const opts = typeof options === "string" ? { description: options } : options
  102. return toaster.show((props) => (
  103. <Toast
  104. toastId={props.toastId}
  105. duration={opts.duration}
  106. persistent={opts.persistent}
  107. data-variant={opts.variant ?? "default"}
  108. >
  109. <Show when={opts.icon}>
  110. <Toast.Icon name={opts.icon!} />
  111. </Show>
  112. <Toast.Content>
  113. <Show when={opts.title}>
  114. <Toast.Title>{opts.title}</Toast.Title>
  115. </Show>
  116. <Show when={opts.description}>
  117. <Toast.Description>{opts.description}</Toast.Description>
  118. </Show>
  119. <Show when={opts.actions?.length}>
  120. <Toast.Actions>
  121. {opts.actions!.map((action) => (
  122. <button
  123. data-slot="toast-action"
  124. onClick={() => {
  125. if (typeof action.onClick === "function") {
  126. action.onClick()
  127. }
  128. toaster.dismiss(props.toastId)
  129. }}
  130. >
  131. {action.label}
  132. </button>
  133. ))}
  134. </Toast.Actions>
  135. </Show>
  136. </Toast.Content>
  137. <Toast.CloseButton />
  138. </Toast>
  139. ))
  140. }
  141. export interface ToastPromiseOptions<T, U = unknown> {
  142. loading?: JSX.Element
  143. success?: (data: T) => JSX.Element
  144. error?: (error: U) => JSX.Element
  145. }
  146. export function showPromiseToast<T, U = unknown>(
  147. promise: Promise<T> | (() => Promise<T>),
  148. options: ToastPromiseOptions<T, U>,
  149. ) {
  150. return toaster.promise(promise, (props) => (
  151. <Toast
  152. toastId={props.toastId}
  153. data-variant={props.state === "pending" ? "loading" : props.state === "fulfilled" ? "success" : "error"}
  154. >
  155. <Toast.Content>
  156. <Toast.Description>
  157. {props.state === "pending" && options.loading}
  158. {props.state === "fulfilled" && options.success?.(props.data!)}
  159. {props.state === "rejected" && options.error?.(props.error)}
  160. </Toast.Description>
  161. </Toast.Content>
  162. <Toast.CloseButton />
  163. </Toast>
  164. ))
  165. }