Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Toaster.tsx

49 lines1.0 KB

1import { normalizeProps, useMachine } from '@zag-js/preact'
2import * as toast from '@zag-js/toast'
3import { useId } from 'preact/hooks'
4import './toaster.css'
5​
6export const toaster = toast.createStore({
7 placement: 'top',
8 overlap: true,
9})
10​
11function Toast({
12 toast: options,
13 parent,
14 index,
15}: {
16 toast: toast.Options
17 parent: toast.GroupService
18 index: number
19}) {
20 const service = useMachine(toast.machine, { ...options, parent, index })
21 const api = toast.connect(service, normalizeProps)
22​
23 return (
24 <div {...api.getRootProps()}>
25 <span {...api.getTitleProps()}>{api.title}</span>
26 </div>
27 )
29​
30export function Toaster() {
31 const service = useMachine(toast.group.machine, {
32 id: useId(),
33 store: toaster,
34 })
35 const api = toast.group.connect(service, normalizeProps)
36​
37 return (
38 <div {...api.getGroupProps()}>
39 {api.getToasts().map((options, index) => (
40 <Toast
41 key={options.id}
42 toast={options}
43 parent={service}
44 index={index}
45 />
46 ))}
47 </div>
48 )