@zag-js/toast 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,43 +4,34 @@ import { runIfFn, uuid } from "@zag-js/utils"
4
4
  import { parts } from "./toast.anatomy"
5
5
  import { dom } from "./toast.dom"
6
6
  import type {
7
+ GroupMachineApi,
7
8
  GroupMachineContext,
8
9
  GroupProps,
9
10
  GroupSend,
10
11
  GroupState,
12
+ Options,
11
13
  Placement,
12
14
  PromiseOptions,
13
- Toaster,
14
- Options,
15
15
  } from "./toast.types"
16
16
  import { getGroupPlacementStyle, getToastsByPlacement } from "./toast.utils"
17
17
 
18
- export let toaster = {} as Toaster
18
+ export let toaster = {} as GroupMachineApi
19
19
 
20
- export function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>) {
20
+ export function groupConnect<T extends PropTypes>(
21
+ state: GroupState,
22
+ send: GroupSend,
23
+ normalize: NormalizeProps<T>,
24
+ ): GroupMachineApi<T> {
25
+ //
21
26
  const group = {
22
- /**
23
- * The total number of toasts
24
- */
25
27
  count: state.context.count,
26
- /**
27
- * The active toasts
28
- */
29
28
  toasts: state.context.toasts,
30
- /**
31
- * The active toasts by placement
32
- */
33
29
  toastsByPlacement: getToastsByPlacement(state.context.toasts),
34
- /**
35
- * Returns whether the toast id is visible
36
- */
37
30
  isVisible(id: string) {
38
31
  if (!state.context.toasts.length) return false
39
32
  return !!state.context.toasts.find((toast) => toast.id == id)
40
33
  },
41
- /**
42
- * Function to create a toast.
43
- */
34
+
44
35
  create(options: Options) {
45
36
  const uid = `toast:${uuid()}`
46
37
  const id = options.id ? options.id : uid
@@ -50,9 +41,7 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
50
41
 
51
42
  return id
52
43
  },
53
- /**
54
- * Function to create or update a toast.
55
- */
44
+
56
45
  upsert(options: Options) {
57
46
  const { id } = options
58
47
  const isVisible = id ? group.isVisible(id) : false
@@ -62,10 +51,7 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
62
51
  return group.create(options)
63
52
  }
64
53
  },
65
- /**
66
- * Function to dismiss a toast by id.
67
- * If no id is provided, all toasts will be dismissed.
68
- */
54
+
69
55
  dismiss(id?: string) {
70
56
  if (id == null) {
71
57
  send("DISMISS_ALL")
@@ -73,10 +59,7 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
73
59
  send({ type: "DISMISS_TOAST", id })
74
60
  }
75
61
  },
76
- /**
77
- * Function to remove a toast by id.
78
- * If no id is provided, all toasts will be removed.
79
- */
62
+
80
63
  remove(id?: string) {
81
64
  if (id == null) {
82
65
  send("REMOVE_ALL")
@@ -84,49 +67,35 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
84
67
  send({ type: "REMOVE_TOAST", id })
85
68
  }
86
69
  },
87
- /**
88
- * Function to dismiss all toasts by placement.
89
- */
70
+
90
71
  dismissByPlacement(placement: Placement) {
91
72
  const toasts = group.toastsByPlacement[placement]
92
73
  if (toasts) {
93
74
  toasts.forEach((toast) => group.dismiss(toast.id))
94
75
  }
95
76
  },
96
- /**
97
- * Function to update a toast's options by id.
98
- */
77
+
99
78
  update(id: string, options: Options) {
100
79
  if (!group.isVisible(id)) return
101
80
  send({ type: "UPDATE_TOAST", id, toast: options })
102
81
  return id
103
82
  },
104
- /**
105
- * Function to create a loading toast.
106
- */
83
+
107
84
  loading(options: Options) {
108
85
  options.type = "loading"
109
86
  return group.upsert(options)
110
87
  },
111
- /**
112
- * Function to create a success toast.
113
- */
88
+
114
89
  success(options: Options) {
115
90
  options.type = "success"
116
91
  return group.upsert(options)
117
92
  },
118
- /**
119
- * Function to create an error toast.
120
- */
93
+
121
94
  error(options: Options) {
122
95
  options.type = "error"
123
96
  return group.upsert(options)
124
97
  },
125
- /**
126
- * Function to create a toast from a promise.
127
- * - When the promise resolves, the toast will be updated with the success options.
128
- * - When the promise rejects, the toast will be updated with the error options.
129
- */
98
+
130
99
  promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared: Options = {}) {
131
100
  const id = group.loading({ ...shared, ...options.loading })
132
101
 
@@ -142,9 +111,7 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
142
111
 
143
112
  return promise
144
113
  },
145
- /**
146
- * Function to pause a toast by id.
147
- */
114
+
148
115
  pause(id?: string) {
149
116
  if (id == null) {
150
117
  send("PAUSE_ALL")
@@ -152,9 +119,7 @@ export function groupConnect<T extends PropTypes>(state: GroupState, send: Group
152
119
  send({ type: "PAUSE_TOAST", id })
153
120
  }
154
121
  },
155
- /**
156
- * Function to resume a toast by id.
157
- */
122
+
158
123
  resume(id?: string) {
159
124
  if (id == null) {
160
125
  send("RESUME_ALL")
@@ -1,9 +1,9 @@
1
1
  import type { NormalizeProps, PropTypes } from "@zag-js/types"
2
2
  import { parts } from "./toast.anatomy"
3
3
  import { dom } from "./toast.dom"
4
- import type { PublicApi, Send, State } from "./toast.types"
4
+ import type { MachineApi, Send, State } from "./toast.types"
5
5
 
6
- export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T> {
6
+ export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
7
7
  const isVisible = state.hasTag("visible")
8
8
  const isPaused = state.hasTag("paused")
9
9
 
@@ -142,7 +142,120 @@ type GroupPublicContext = SharedContext &
142
142
  offsets: string | Record<"left" | "right" | "bottom" | "top", string>
143
143
  }
144
144
 
145
- export type PublicApi<T extends PropTypes = PropTypes> = {
145
+ export type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">
146
+
147
+ type GroupComputedContext = Readonly<{
148
+ /**
149
+ * @computed
150
+ * The total number of toasts in the group
151
+ */
152
+ count: number
153
+ }>
154
+
155
+ type GroupPrivateContext = Context<{
156
+ /**
157
+ * @internal
158
+ * The child toast machines (spawned by the toast group)
159
+ */
160
+ toasts: Service[]
161
+ }>
162
+
163
+ export type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext
164
+
165
+ export type GroupState = S.State<GroupMachineContext>
166
+
167
+ export type GroupSend = (event: S.Event<S.AnyEventObject>) => void
168
+
169
+ type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value)
170
+
171
+ export type PromiseOptions<Value> = {
172
+ loading: ToastOptions
173
+ success: MaybeFunction<ToastOptions, Value>
174
+ error: MaybeFunction<ToastOptions, Error>
175
+ }
176
+
177
+ export type GroupProps = {
178
+ placement: Placement
179
+ label?: string
180
+ }
181
+
182
+ export type GroupMachineApi<T extends PropTypes = PropTypes> = {
183
+ /**
184
+ * The total number of toasts
185
+ */
186
+ count: number
187
+ /**
188
+ * The active toasts
189
+ */
190
+ toasts: Service[]
191
+ /**
192
+ * The active toasts by placement
193
+ */
194
+ toastsByPlacement: Partial<Record<Placement, Service[]>>
195
+ /**
196
+ * Returns whether the toast id is visible
197
+ */
198
+ isVisible(id: string): boolean
199
+ /**
200
+ * Function to create a toast.
201
+ */
202
+ create(options: Options): string | undefined
203
+ /**
204
+ * Function to create or update a toast.
205
+ */
206
+ upsert(options: Options): string | undefined
207
+ /**
208
+ * Function to update a toast's options by id.
209
+ */
210
+ update(id: string, options: Options): void
211
+ /**
212
+ * Function to create a success toast.
213
+ */
214
+ success(options: Options): string | undefined
215
+ /**
216
+ * Function to create an error toast.
217
+ */
218
+ error(options: Options): string | undefined
219
+ /**
220
+ * Function to create a loading toast.
221
+ */
222
+ loading(options: Options): string | undefined
223
+ /**
224
+ * Function to resume a toast by id.
225
+ */
226
+ resume(id?: string | undefined): void
227
+ /**
228
+ * Function to pause a toast by id.
229
+ */
230
+ pause(id?: string | undefined): void
231
+ /**
232
+ * Function to dismiss a toast by id.
233
+ * If no id is provided, all toasts will be dismissed.
234
+ */
235
+ dismiss(id?: string | undefined): void
236
+ /**
237
+ * Function to dismiss all toasts by placement.
238
+ */
239
+ dismissByPlacement(placement: Placement): void
240
+ /**
241
+ * Function to remove a toast by id.
242
+ * If no id is provided, all toasts will be removed.
243
+ */
244
+ remove(id?: string | undefined): void
245
+ /**
246
+ * Function to create a toast from a promise.
247
+ * - When the promise resolves, the toast will be updated with the success options.
248
+ * - When the promise rejects, the toast will be updated with the error options.
249
+ */
250
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>
251
+ /**
252
+ * Function to subscribe to the toast group.
253
+ */
254
+ subscribe(callback: (toasts: Service[]) => void): VoidFunction
255
+ getGroupProps(options: GroupProps): T["element"]
256
+ }
257
+
258
+ export type MachineApi<T extends PropTypes = PropTypes> = {
146
259
  /**
147
260
  * The type of the toast.
148
261
  */
@@ -192,53 +305,3 @@ export type PublicApi<T extends PropTypes = PropTypes> = {
192
305
  descriptionProps: T["element"]
193
306
  closeTriggerProps: T["button"]
194
307
  }
195
-
196
- export type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">
197
-
198
- type GroupComputedContext = Readonly<{
199
- /**
200
- * @computed
201
- * The total number of toasts in the group
202
- */
203
- readonly count: number
204
- }>
205
-
206
- type GroupPrivateContext = Context<{
207
- /**
208
- * @internal
209
- * The child toast machines (spawned by the toast group)
210
- */
211
- toasts: Service[]
212
- }>
213
-
214
- export type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext
215
-
216
- export type GroupState = S.State<GroupMachineContext>
217
-
218
- export type GroupSend = (event: S.Event<S.AnyEventObject>) => void
219
-
220
- type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value)
221
-
222
- export type PromiseOptions<Value> = {
223
- loading: ToastOptions
224
- success: MaybeFunction<ToastOptions, Value>
225
- error: MaybeFunction<ToastOptions, Error>
226
- }
227
-
228
- export type GroupProps = {
229
- placement: Placement
230
- label?: string
231
- }
232
-
233
- export type Toaster = {
234
- count: number
235
- isVisible(id: string): boolean
236
- upsert(options: ToastOptions): string | undefined
237
- create(options: ToastOptions): string | undefined
238
- success(options: ToastOptions): string | undefined
239
- error(options: ToastOptions): string | undefined
240
- loading(options: ToastOptions): string | undefined
241
- dismiss(id?: string | undefined): void
242
- remove(id?: string | undefined): void
243
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>
244
- }