@aleph-alpha/ui-library 3.1.0 → 3.2.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.
Files changed (45) hide show
  1. package/README.md +41 -32
  2. package/config.js +9 -10
  3. package/dist/system/components/UiSnackbar/UiSnackbar.vue.d.ts +1 -0
  4. package/dist/system/components/UiSnackbar/UiSnackbarContent.vue.d.ts +29 -0
  5. package/dist/system/components/UiSnackbar/UiSnackbarIcon.vue.d.ts +3 -0
  6. package/dist/system/components/UiSnackbar/index.d.ts +3 -0
  7. package/dist/system/components/UiSnackbar/showSnackbar.d.ts +55 -0
  8. package/dist/system/components/UiSnackbar/types.d.ts +76 -17
  9. package/dist/system/lib.js +8218 -7978
  10. package/package.json +4 -4
  11. package/src/@types/shims-vue.d.ts +5 -0
  12. package/src/components/UiButton/UiButton.stories.ts +4 -20
  13. package/src/components/UiCheckbox/UiCheckbox.stories.ts +4 -20
  14. package/src/components/UiField/UiField.stories.ts +4 -20
  15. package/src/components/UiIcon/UiIcon.stories.ts +2 -0
  16. package/src/components/UiItem/UiItem.stories.ts +4 -19
  17. package/src/components/UiSnackbar/UiSnackbar.stories.ts +281 -255
  18. package/src/components/UiSnackbar/UiSnackbar.vue +71 -241
  19. package/src/components/UiSnackbar/UiSnackbarContent.vue +114 -0
  20. package/src/components/UiSnackbar/UiSnackbarIcon.vue +78 -0
  21. package/src/components/UiSnackbar/__tests__/UiSnackbar.test.ts +154 -1
  22. package/src/components/UiSnackbar/__tests__/showSnackbar.test.ts +258 -0
  23. package/src/components/UiSnackbar/assets/error.svg +3 -0
  24. package/src/components/UiSnackbar/assets/info.svg +3 -0
  25. package/src/components/UiSnackbar/assets/loading.svg +14 -0
  26. package/src/components/UiSnackbar/assets/success.svg +5 -0
  27. package/src/components/UiSnackbar/assets/warning.svg +3 -0
  28. package/src/components/UiSnackbar/index.ts +3 -0
  29. package/src/components/UiSnackbar/showSnackbar.ts +174 -0
  30. package/src/components/UiSnackbar/types.ts +90 -17
  31. package/src/components/core/sonner/Sonner.vue +3 -20
  32. package/src/directives/vHover.stories.ts +1 -0
  33. package/src/lib/useDevice.stories.ts +2 -1
  34. package/src/lib/useDrag.stories.ts +2 -1
  35. package/src/theme/Background.stories.ts +2 -0
  36. package/src/theme/Border.stories.ts +2 -0
  37. package/src/theme/BorderRadius.stories.ts +2 -0
  38. package/src/theme/Extended.stories.ts +2 -0
  39. package/src/theme/Flex.stories.ts +2 -0
  40. package/src/theme/Grid.stories.ts +2 -0
  41. package/src/theme/HoverLayer.stories.ts +2 -0
  42. package/src/theme/Overlay.stories.ts +2 -0
  43. package/src/theme/Shadow.stories.ts +2 -0
  44. package/src/theme/Theme.stories.ts +2 -0
  45. package/src/theme/Typography.stories.ts +2 -0
@@ -1,13 +1,19 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
- import { onMounted, ref } from 'vue';
3
- import { toast, UiSnackbar } from './index';
2
+ import { ref } from 'vue';
3
+ import { showSnackbar, showSnackbarPromise, UiSnackbar } from './index';
4
4
  import { UiButton } from '../UiButton';
5
5
  import { UiDialog, UiDialogContent, UiDialogTrigger } from '../UiDialog';
6
6
 
7
+ /**
8
+ * UiSnackbar is a transient toast notification for non-blocking feedback.
9
+ * Mount `<UiSnackbar />` once near the app root, then call `showSnackbar()`
10
+ * for title, description, status icon, and action content. For inline page
11
+ * feedback use `UiAlert`; for blocking confirmations use `UiAlertDialog`.
12
+ */
7
13
  const meta: Meta<typeof UiSnackbar> = {
8
14
  title: 'Components/UiSnackbar',
9
15
  component: UiSnackbar,
10
- tags: ['autodocs'],
16
+ tags: ['autodocs', 'experimental'],
11
17
  argTypes: {
12
18
  position: {
13
19
  control: 'select',
@@ -24,11 +30,6 @@ const meta: Meta<typeof UiSnackbar> = {
24
30
  control: 'select',
25
31
  options: ['light', 'dark', 'system'],
26
32
  },
27
- richColors: {
28
- control: 'boolean',
29
- description:
30
- 'High hierarchy (vivid background) when true, Low/Soft hierarchy when false.',
31
- },
32
33
  expand: { control: 'boolean' },
33
34
  closeButton: { control: 'boolean' },
34
35
  duration: { control: 'number' },
@@ -38,7 +39,6 @@ const meta: Meta<typeof UiSnackbar> = {
38
39
  args: {
39
40
  position: 'bottom-right',
40
41
  theme: 'light',
41
- richColors: true,
42
42
  expand: false,
43
43
  closeButton: true,
44
44
  visibleToasts: 3,
@@ -49,274 +49,268 @@ const meta: Meta<typeof UiSnackbar> = {
49
49
  export default meta;
50
50
  type Story = StoryObj<typeof UiSnackbar>;
51
51
 
52
- const FIGMA_MESSAGE = 'You have a new notification.';
53
- const FIGMA_ACTION_LABEL = 'Action';
54
-
55
- const noopAction = {
56
- label: FIGMA_ACTION_LABEL,
57
- // eslint-disable-next-line no-console
58
- onClick: () => console.log('Action clicked'),
59
- };
52
+ const SNACKBAR_ACTION_TITLE = 'Event has been created';
60
53
 
61
- type TypedSnackbar = 'success' | 'info' | 'warning' | 'error';
62
-
63
- function showTypedSnackbar(
64
- type: TypedSnackbar,
65
- toasterId: string,
66
- richColors: boolean,
67
- ) {
68
- return toast[type](FIGMA_MESSAGE, {
69
- toasterId,
70
- richColors,
71
- action: noopAction,
72
- });
73
- }
54
+ const SNACKBAR_DESCRIPTION = 'Sunday, December 03, 2023 at 9:00 AM';
55
+ const SNACKBAR_ACTION_LABEL = 'Undo';
74
56
 
75
- function makeTypedStory(
76
- type: TypedSnackbar,
77
- hierarchy: 'high' | 'low',
78
- storyName: string,
79
- ): Story {
80
- const richColors = hierarchy === 'high';
81
- const toasterId = `story-${type}-${hierarchy}`;
82
- const buttonLabel = `${type[0].toUpperCase()}${type.slice(1)} ${
83
- hierarchy === 'high' ? 'High' : 'Low'
84
- }`;
85
- const sourceCode = `<script setup lang="ts">
86
- import { UiSnackbar, toast, UiButton } from '@aleph-alpha/ui-library'
57
+ const defaultTemplateSource = `<script setup lang="ts">
58
+ import { UiSnackbar, showSnackbar, UiButton } from '@aleph-alpha/ui-library'
87
59
  </script>
88
60
 
89
61
  <template>
90
62
  <UiButton
91
- @click="toast.${type}('${FIGMA_MESSAGE}', {
92
- richColors: ${richColors},
93
- action: { label: '${FIGMA_ACTION_LABEL}', onClick: () => {} },
63
+ @click="showSnackbar({
64
+ title: 'Event has been created',
65
+ description: '${SNACKBAR_DESCRIPTION}',
66
+ iconMode: 'info',
67
+ actionLabel: '${SNACKBAR_ACTION_LABEL}',
68
+ actionMode: 'default',
69
+ onAction: () => {},
94
70
  })"
95
71
  >
96
- ${buttonLabel}
72
+ Show snackbar
97
73
  </UiButton>
98
- <UiSnackbar close-button${richColors ? ' rich-colors' : ''} />
74
+ <UiSnackbar :close-button="true" />
99
75
  </template>`;
100
76
 
101
- return {
102
- name: storyName,
103
- args: { richColors, closeButton: true },
104
- render: (args) => ({
105
- components: { UiSnackbar, UiButton },
106
- setup() {
107
- const show = () => showTypedSnackbar(type, toasterId, args.richColors);
108
- return { args, show, toasterId, buttonLabel };
109
- },
110
- template: `
111
- <div>
112
- <UiButton @click="show">{{ buttonLabel }}</UiButton>
113
- <UiSnackbar v-bind="args" :id="toasterId" />
114
- </div>
115
- `,
116
- }),
117
- parameters: {
118
- docs: {
119
- source: { code: sourceCode },
120
- },
121
- },
122
- };
123
- }
124
-
125
77
  /**
126
- * Confirm that an operation succeeded - use as the primary success signal
127
- * (saved, sent, completed). High hierarchy: vivid green for emphasis.
78
+ * Full-featured snackbar with a title, description, and action.
128
79
  */
129
- export const SuccessHigh: Story = makeTypedStory(
130
- 'success',
131
- 'high',
132
- 'Success High',
133
- );
134
-
135
- /**
136
- * Confirm a non-critical success in a quieter tone - background-task
137
- * acknowledgements, autosave, sync complete.
138
- */
139
- export const SuccessLow: Story = makeTypedStory(
140
- 'success',
141
- 'low',
142
- 'Success Low',
143
- );
144
-
145
- /**
146
- * Surface neutral information that needs immediate attention - reminders,
147
- * tips, system announcements.
148
- */
149
- export const InfoHigh: Story = makeTypedStory('info', 'high', 'Info High');
150
-
151
- /**
152
- * Surface neutral information that doesn't compete with primary content -
153
- * inline help, contextual hints.
154
- */
155
- export const InfoLow: Story = makeTypedStory('info', 'low', 'Info Low');
156
-
157
- /**
158
- * Warn the user about a non-blocking caution - quota nearing limit, an action
159
- * has consequences. Use when the user can still proceed.
160
- */
161
- export const WarningHigh: Story = makeTypedStory(
162
- 'warning',
163
- 'high',
164
- 'Warning High',
165
- );
80
+ export const Default: Story = {
81
+ render: (args) => ({
82
+ components: { UiSnackbar, UiButton },
83
+ setup() {
84
+ const toasterId = 'story-default';
85
+ const show = () =>
86
+ showSnackbar({
87
+ title: SNACKBAR_ACTION_TITLE,
88
+ description: SNACKBAR_DESCRIPTION,
89
+ actionLabel: SNACKBAR_ACTION_LABEL,
90
+ actionMode: 'default',
91
+ iconMode: 'info',
92
+ onAction: () => {},
93
+ toasterId,
94
+ });
95
+ return { args, show, toasterId };
96
+ },
97
+ template: `
98
+ <div>
99
+ <UiButton @click="show">Show snackbar</UiButton>
100
+ <UiSnackbar v-bind="args" :id="toasterId" />
101
+ </div>
102
+ `,
103
+ }),
104
+ parameters: {
105
+ docs: {
106
+ source: { code: defaultTemplateSource },
107
+ },
108
+ },
109
+ };
166
110
 
167
- /**
168
- * Quieter warning for informational caution that doesn't require immediate
169
- * action.
170
- */
171
- export const WarningLow: Story = makeTypedStory(
172
- 'warning',
173
- 'low',
174
- 'Warning Low',
175
- );
111
+ const withDescriptionTemplateSource = `<script setup lang="ts">
112
+ import { UiSnackbar, showSnackbar, UiButton } from '@aleph-alpha/ui-library'
113
+ </script>
176
114
 
177
- /**
178
- * Report a failure or rejected action - upload failed, request errored, save
179
- * blocked. High hierarchy: vivid red for unmistakable signal.
180
- */
181
- export const ErrorHigh: Story = makeTypedStory('error', 'high', 'Error High');
115
+ <template>
116
+ <UiButton
117
+ @click="showSnackbar({
118
+ title: 'Event has been created',
119
+ description: '${SNACKBAR_DESCRIPTION}',
120
+ })"
121
+ >
122
+ Show snackbar
123
+ </UiButton>
124
+ <UiSnackbar />
125
+ </template>`;
182
126
 
183
127
  /**
184
- * Report a recoverable error in a less alarming tone - retry suggestions,
185
- * non-critical failures.
128
+ * Add a description when the message needs supporting context such as a
129
+ * timestamp, location, or related record.
186
130
  */
187
- export const ErrorLow: Story = makeTypedStory('error', 'low', 'Error Low');
188
-
189
- const allVariantsTemplateSource = `<script setup lang="ts">
190
- import { UiSnackbar, toast } from '@aleph-alpha/ui-library'
131
+ export const WithDescription: Story = {
132
+ render: (args) => ({
133
+ components: { UiSnackbar, UiButton },
134
+ setup() {
135
+ const toasterId = 'story-description';
136
+ const show = () =>
137
+ showSnackbar({
138
+ title: 'Event has been created',
139
+ description: 'Sunday, December 03, 2023 at 9:00 AM',
140
+ toasterId,
141
+ });
142
+ return { args, show, toasterId };
143
+ },
144
+ template: `
145
+ <div>
146
+ <UiButton @click="show">Show snackbar</UiButton>
147
+ <UiSnackbar v-bind="args" :id="toasterId" />
148
+ </div>
149
+ `,
150
+ }),
151
+ parameters: {
152
+ docs: {
153
+ source: { code: withDescriptionTemplateSource },
154
+ },
155
+ },
156
+ };
191
157
 
192
- // Show one snackbar per type x hierarchy via toast.<type>(msg, { richColors })
193
- toast.error('You have a new notification.', { richColors: true, action: { label: 'Action', onClick: () => {} } })
194
- toast.info('You have a new notification.', { richColors: true, action: { label: 'Action', onClick: () => {} } })
195
- // ... and so on for warning, success, plus richColors: false versions
158
+ const withIconModesTemplateSource = `<script setup lang="ts">
159
+ import { UiSnackbar, showSnackbar, UiButton } from '@aleph-alpha/ui-library'
196
160
  </script>
197
161
 
198
162
  <template>
199
- <UiSnackbar close-button rich-colors />
163
+ <div class="flex flex-wrap items-center gap-xs">
164
+ <UiButton @click="showSnackbar({ title: 'Event has been created', iconMode: 'success' })">
165
+ Success
166
+ </UiButton>
167
+ <UiButton @click="showSnackbar({ title: 'Event has been created', iconMode: 'info' })">
168
+ Info
169
+ </UiButton>
170
+ <UiButton @click="showSnackbar({ title: 'Event has been created', iconMode: 'warning' })">
171
+ Warning
172
+ </UiButton>
173
+ <UiButton @click="showSnackbar({ title: 'Event has been created', iconMode: 'error' })">
174
+ Error
175
+ </UiButton>
176
+ <UiButton @click="showSnackbar({ title: 'Event has been created', iconMode: 'loading' })">
177
+ Loading
178
+ </UiButton>
179
+ </div>
180
+ <UiSnackbar />
200
181
  </template>`;
201
182
 
202
- type MatrixCell = {
203
- type: TypedSnackbar;
204
- hierarchy: 'high' | 'low';
205
- id: string;
206
- label: string;
207
- };
208
-
209
- const ALL_CELLS: MatrixCell[] = (
210
- ['error', 'info', 'warning', 'success'] as const
211
- ).flatMap((type) =>
212
- (['low', 'high'] as const).map((hierarchy) => ({
213
- type,
214
- hierarchy,
215
- id: `matrix-${type}-${hierarchy}`,
216
- label: `${type[0].toUpperCase()}${type.slice(1)} ${
217
- hierarchy === 'high' ? 'High' : 'Low'
218
- }`,
219
- })),
220
- );
183
+ const SNACKBAR_ICON_MODES = [
184
+ 'success',
185
+ 'info',
186
+ 'warning',
187
+ 'error',
188
+ 'loading',
189
+ ] as const;
221
190
 
222
191
  /**
223
- * Side-by-side overview of every type x hierarchy combination from the Figma
224
- * matrix. Each cell mounts a real `<UiSnackbar />` and dispatches one toast
225
- * with `duration: Infinity` on mount, so what you see is the actual component,
226
- * not a static mock.
192
+ * Built-in status icons for success, info, warning, error, and loading.
227
193
  */
228
- export const AllVariants: Story = {
229
- name: 'AllVariants',
230
- parameters: {
231
- docs: {
232
- source: { code: allVariantsTemplateSource },
233
- },
234
- },
235
- render: () => ({
236
- components: { UiSnackbar },
194
+ export const IconModes: Story = {
195
+ name: 'Icon Modes',
196
+ render: (args) => ({
197
+ components: { UiSnackbar, UiButton },
237
198
  setup() {
238
- onMounted(() => {
239
- ALL_CELLS.forEach((cell) => {
240
- toast[cell.type](FIGMA_MESSAGE, {
241
- toasterId: cell.id,
242
- richColors: cell.hierarchy === 'high',
243
- duration: Infinity,
244
- action: noopAction,
245
- });
199
+ const toasterId = 'story-icon-modes';
200
+ const show = (iconMode: (typeof SNACKBAR_ICON_MODES)[number]) =>
201
+ showSnackbar({
202
+ title: SNACKBAR_ACTION_TITLE,
203
+ iconMode,
204
+ toasterId,
246
205
  });
247
- });
248
- return { cells: ALL_CELLS };
206
+ return { args, show, toasterId, modes: SNACKBAR_ICON_MODES };
249
207
  },
250
208
  template: `
251
- <div class="snackbar-matrix grid grid-cols-1 md:grid-cols-2 gap-4 p-4">
252
- <div
253
- v-for="cell in cells"
254
- :key="cell.id"
255
- class="flex flex-col gap-2"
256
- >
257
- <span class="text-content-on-surface-secondary text-xs">
258
- {{ cell.label }}
259
- </span>
260
- <UiSnackbar
261
- :id="cell.id"
262
- :rich-colors="cell.hierarchy === 'high'"
263
- close-button
264
- expand
265
- :container-aria-label="'Notifications ' + cell.label"
266
- :visible-toasts="1"
267
- />
209
+ <div>
210
+ <div class="flex flex-wrap items-center gap-xs">
211
+ <UiButton
212
+ v-for="mode in modes"
213
+ :key="mode"
214
+ @click="show(mode)"
215
+ >
216
+ {{ mode[0].toUpperCase() }}{{ mode.slice(1) }}
217
+ </UiButton>
268
218
  </div>
219
+ <UiSnackbar v-bind="args" :id="toasterId" />
269
220
  </div>
270
221
  `,
271
222
  }),
223
+ parameters: {
224
+ docs: {
225
+ source: { code: withIconModesTemplateSource },
226
+ },
227
+ },
272
228
  };
273
229
 
274
- const defaultTemplateSource = `<script setup lang="ts">
275
- import { UiSnackbar, toast, UiButton } from '@aleph-alpha/ui-library'
230
+ const withActionModesTemplateSource = `<script setup lang="ts">
231
+ import { UiSnackbar, showSnackbar, UiButton } from '@aleph-alpha/ui-library'
276
232
  </script>
277
233
 
278
234
  <template>
279
- <UiButton @click="toast('My first snackbar')">Show snackbar</UiButton>
235
+ <div class="flex flex-wrap items-center gap-xs">
236
+ <UiButton
237
+ @click="showSnackbar({
238
+ title: 'Event has been created',
239
+ actionLabel: 'Undo',
240
+ actionMode: 'default',
241
+ onAction: () => {},
242
+ })"
243
+ >
244
+ Default action
245
+ </UiButton>
246
+ <UiButton
247
+ @click="showSnackbar({
248
+ title: 'Event has been created',
249
+ actionLabel: 'Undo',
250
+ actionMode: 'secondary',
251
+ onAction: () => {},
252
+ })"
253
+ >
254
+ Secondary action
255
+ </UiButton>
256
+ </div>
280
257
  <UiSnackbar />
281
258
  </template>`;
282
259
 
260
+ const SNACKBAR_ACTION_MODES = ['default', 'secondary'] as const;
261
+
283
262
  /**
284
- * Plain (untyped) snackbar without a status icon - use for neutral
285
- * confirmations that don't fit success/info/warning/error semantics. Falls
286
- * back to the project's inverse surface tokens for contrast.
263
+ * Built-in action button modes: default (primary) and secondary.
287
264
  */
288
- export const Default: Story = {
289
- args: { richColors: false, closeButton: false },
265
+ export const ActionModes: Story = {
266
+ name: 'Action Modes',
290
267
  render: (args) => ({
291
268
  components: { UiSnackbar, UiButton },
292
269
  setup() {
293
- const toasterId = 'story-default';
294
- const show = () => toast('My first snackbar', { toasterId });
295
- return { args, show, toasterId };
270
+ const toasterId = 'story-action-modes';
271
+ const show = (actionMode: (typeof SNACKBAR_ACTION_MODES)[number]) =>
272
+ showSnackbar({
273
+ title: SNACKBAR_ACTION_TITLE,
274
+ actionLabel: SNACKBAR_ACTION_LABEL,
275
+ actionMode,
276
+ onAction: () => {},
277
+ toasterId,
278
+ });
279
+ return { args, show, toasterId, modes: SNACKBAR_ACTION_MODES };
296
280
  },
297
281
  template: `
298
282
  <div>
299
- <UiButton @click="show">Show snackbar</UiButton>
283
+ <div class="flex flex-wrap items-center gap-xs">
284
+ <UiButton
285
+ v-for="mode in modes"
286
+ :key="mode"
287
+ @click="show(mode)"
288
+ >
289
+ {{ mode[0].toUpperCase() }}{{ mode.slice(1) }} action
290
+ </UiButton>
291
+ </div>
300
292
  <UiSnackbar v-bind="args" :id="toasterId" />
301
293
  </div>
302
294
  `,
303
295
  }),
304
296
  parameters: {
305
297
  docs: {
306
- source: { code: defaultTemplateSource },
298
+ source: { code: withActionModesTemplateSource },
307
299
  },
308
300
  },
309
301
  };
310
302
 
311
- const withDescriptionTemplateSource = `<script setup lang="ts">
312
- import { UiSnackbar, toast, UiButton } from '@aleph-alpha/ui-library'
303
+ const withActionTemplateSource = `<script setup lang="ts">
304
+ import { UiSnackbar, showSnackbar, UiButton } from '@aleph-alpha/ui-library'
313
305
  </script>
314
306
 
315
307
  <template>
316
308
  <UiButton
317
- @click="toast('Event has been created', {
318
- description: 'Sunday, December 03, 2023 at 9:00 AM',
319
- action: { label: 'Undo', onClick: () => console.log('Undo') },
309
+ @click="showSnackbar({
310
+ title: '${SNACKBAR_ACTION_TITLE}',
311
+ actionLabel: '${SNACKBAR_ACTION_LABEL}',
312
+ actionMode: 'default',
313
+ onAction: () => {},
320
314
  })"
321
315
  >
322
316
  Show snackbar
@@ -325,24 +319,21 @@ import { UiSnackbar, toast, UiButton } from '@aleph-alpha/ui-library'
325
319
  </template>`;
326
320
 
327
321
  /**
328
- * Use when the message needs supporting context (timestamp, location, related
329
- * record). Not in the current Figma design but supported by the underlying API.
322
+ * Add a trailing action such as Undo when the user should be able to respond
323
+ * without leaving the current context.
330
324
  */
331
- export const WithDescription: Story = {
332
- args: { richColors: false, closeButton: false },
325
+ export const WithAction: Story = {
333
326
  render: (args) => ({
334
327
  components: { UiSnackbar, UiButton },
335
328
  setup() {
336
- const toasterId = 'story-description';
329
+ const toasterId = 'story-action';
337
330
  const show = () =>
338
- toast('Event has been created', {
331
+ showSnackbar({
332
+ title: SNACKBAR_ACTION_TITLE,
333
+ actionLabel: SNACKBAR_ACTION_LABEL,
334
+ actionMode: 'default',
335
+ onAction: () => {},
339
336
  toasterId,
340
- description: 'Sunday, December 03, 2023 at 9:00 AM',
341
- action: {
342
- label: 'Undo',
343
- // eslint-disable-next-line no-console
344
- onClick: () => console.log('Undo'),
345
- },
346
337
  });
347
338
  return { args, show, toasterId };
348
339
  },
@@ -355,39 +346,74 @@ export const WithDescription: Story = {
355
346
  }),
356
347
  parameters: {
357
348
  docs: {
358
- source: { code: withDescriptionTemplateSource },
349
+ source: { code: withActionTemplateSource },
359
350
  },
360
351
  },
361
352
  };
362
353
 
363
354
  const promiseTemplateSource = `<script setup lang="ts">
364
- import { UiSnackbar, toast, UiButton } from '@aleph-alpha/ui-library'
355
+ import { UiSnackbar, showSnackbarPromise, UiButton } from '@aleph-alpha/ui-library'
365
356
 
366
- function handlePromiseClick() {
367
- toast.promise<{ name: string }>(
357
+ function handleSuccessPromise() {
358
+ showSnackbarPromise(
368
359
  () => new Promise((resolve) => setTimeout(() => resolve({ name: 'Event' }), 2000)),
369
360
  {
370
- loading: 'Loading...',
361
+ loading: 'Saving event...',
362
+ success: (data) => \`\${data.name} has been created\`,
363
+ error: 'Something went wrong',
364
+ },
365
+ )
366
+ }
367
+
368
+ function handleErrorPromise() {
369
+ showSnackbarPromise(
370
+ () => new Promise((_resolve, reject) => setTimeout(() => reject(new Error('Failed')), 2000)),
371
+ {
372
+ loading: 'Saving event...',
371
373
  success: (data) => \`\${data.name} has been created\`,
372
- error: 'Error',
374
+ error: 'Something went wrong',
373
375
  },
374
376
  )
375
377
  }
376
378
  </script>
377
379
 
378
380
  <template>
379
- <UiButton @click="handlePromiseClick">Show promise snackbar</UiButton>
381
+ <div class="flex flex-wrap items-center gap-xs">
382
+ <UiButton @click="handleSuccessPromise">Success promise</UiButton>
383
+ <UiButton @click="handleErrorPromise">Error promise</UiButton>
384
+ </div>
380
385
  <UiSnackbar />
381
386
  </template>`;
382
387
 
388
+ /**
389
+ * Resolves after a delay. Used by the Promise story success path.
390
+ * @param value Value to resolve with.
391
+ * @param ms Delay in milliseconds.
392
+ */
383
393
  function resolveAfter<T>(value: T, ms: number): Promise<T> {
384
394
  return new window.Promise((resolve) => setTimeout(() => resolve(value), ms));
385
395
  }
386
396
 
387
397
  /**
388
- * Use to track an async operation - shows loading, then transitions to success
389
- * or error based on the promise outcome. Good for save/upload flows where you
390
- * want one snackbar to tell the whole story.
398
+ * Rejects after a delay. Used by the Promise story error path.
399
+ * @param ms Delay in milliseconds.
400
+ * @param reason Rejection error.
401
+ */
402
+ function rejectAfter(ms: number, reason: Error): Promise<never> {
403
+ return new window.Promise((_resolve, reject) =>
404
+ setTimeout(() => reject(reason), ms),
405
+ );
406
+ }
407
+
408
+ const SNACKBAR_PROMISE_MESSAGES = {
409
+ loading: 'Saving event...',
410
+ success: (data: { name: string }) => `${data.name} has been created`,
411
+ error: 'Something went wrong',
412
+ } as const;
413
+
414
+ /**
415
+ * Track an async operation with loading, success, and error icon modes in a
416
+ * single snackbar flow.
391
417
  */
392
418
  export const PromiseSnackbar: Story = {
393
419
  name: 'Promise',
@@ -395,22 +421,26 @@ export const PromiseSnackbar: Story = {
395
421
  components: { UiSnackbar, UiButton },
396
422
  setup() {
397
423
  const toasterId = 'story-promise';
398
- const show = () => {
399
- toast.promise<{ name: string }>(
424
+ const showSuccess = () =>
425
+ showSnackbarPromise(
400
426
  () => resolveAfter({ name: 'Event' }, 2000),
401
- {
402
- toasterId,
403
- loading: 'Loading...',
404
- success: (data) => `${data.name} has been created`,
405
- error: 'Error',
406
- },
427
+ SNACKBAR_PROMISE_MESSAGES,
428
+ { toasterId },
407
429
  );
408
- };
409
- return { args, show, toasterId };
430
+ const showError = () =>
431
+ showSnackbarPromise(
432
+ () => rejectAfter(2000, new Error('Failed')),
433
+ SNACKBAR_PROMISE_MESSAGES,
434
+ { toasterId },
435
+ );
436
+ return { args, showSuccess, showError, toasterId };
410
437
  },
411
438
  template: `
412
439
  <div>
413
- <UiButton @click="show">Show promise snackbar</UiButton>
440
+ <div class="flex flex-wrap items-center gap-xs">
441
+ <UiButton @click="showSuccess">Success promise</UiButton>
442
+ <UiButton @click="showError">Error promise</UiButton>
443
+ </div>
414
444
  <UiSnackbar v-bind="args" :id="toasterId" />
415
445
  </div>
416
446
  `,
@@ -425,7 +455,7 @@ export const PromiseSnackbar: Story = {
425
455
  const withDialogTemplateSource = `<script setup lang="ts">
426
456
  import {
427
457
  UiSnackbar,
428
- toast,
458
+ showSnackbar,
429
459
  UiButton,
430
460
  UiDialog,
431
461
  UiDialogTrigger,
@@ -447,22 +477,17 @@ function handleInteractOutside(event: Event) {
447
477
  title="Confirm action"
448
478
  @interact-outside="handleInteractOutside"
449
479
  >
450
- <UiButton
451
- @click="toast.success('${FIGMA_MESSAGE}', {
452
- action: { label: '${FIGMA_ACTION_LABEL}', onClick: () => {} },
453
- })"
454
- >
480
+ <UiButton @click="showSnackbar({ title: 'Event has been created' })">
455
481
  Show snackbar
456
482
  </UiButton>
457
483
  </UiDialogContent>
458
484
  </UiDialog>
459
- <UiSnackbar rich-colors close-button />
485
+ <UiSnackbar />
460
486
  </template>`;
461
487
 
462
488
  /**
463
489
  * Trigger a snackbar from inside a modal dialog. Forward `@interact-outside`
464
- * so clicking the snackbar doesn't dismiss the dialog (the toast renders
465
- * outside the dialog tree).
490
+ * so clicking the snackbar does not dismiss the dialog.
466
491
  */
467
492
  export const WithDialog: Story = {
468
493
  render: (args) => ({
@@ -476,7 +501,8 @@ export const WithDialog: Story = {
476
501
  setup() {
477
502
  const open = ref(false);
478
503
  const toasterId = 'story-dialog';
479
- const show = () => showTypedSnackbar('success', toasterId, true);
504
+ const show = () =>
505
+ showSnackbar({ title: 'Event has been created', toasterId });
480
506
  const handleInteractOutside = (event: Event) => {
481
507
  const target = event.target as HTMLElement | null;
482
508
  if (target?.closest('[data-sonner-toaster]')) event.preventDefault();