Skip to content

Commit 6c04794

Browse files
zernoniaclaude
andauthored
feat(Toast): add toast manager (#2985)
* feat(Toast): add toast manager Add `useToastManager()` and `createToastManager()` to add, update, close and promise toasts from anywhere, following Base UI's toast manager API. Pass a managed toast to `ToastRoot` with the new `toast` prop. Also add stacking support for every toast (`limit`, `data-limited`, `data-expanded`, and index/offset/height CSS variables) and anchored toasts with `ToastPositioner` and `ToastArrow`. Closes #2482 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(Toast): clarify client-only queueing and conditional altText Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 43a8e6a commit 6c04794

25 files changed

Lines changed: 2041 additions & 40 deletions

‎docs/content/docs/components/toast.md‎

Lines changed: 369 additions & 1 deletion
Large diffs are not rendered by default.

‎docs/content/meta/ToastAction.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<PropsTable :data="[
55
{
66
'name': 'altText',
7-
'description': '<p>A short description for an alternate way to carry out the action. For screen reader users\nwho will not be able to navigate to the button easily/quickly.</p>\n',
7+
'description': '<p>A short description for an alternate way to carry out the action. For screen reader users\nwho will not be able to navigate to the button easily/quickly.</p>\n<p>A rendered action requires <code>altText</code>, unless the toast passed to <code>ToastRoot</code> has <code>actionProps.altText</code>.</p>\n',
88
'type': 'string',
9-
'required': true
9+
'required': false
1010
},
1111
{
1212
'name': 'as',
@@ -37,7 +37,7 @@
3737

3838
| Name | Description | Type | Required | Default |
3939
| --- | --- | --- | --- | --- |
40-
| `altText` | A short description for an alternate way to carry out the action. For screen reader users who will not be able to navigate to the button easily/quickly. | `string` | Yes | - |
40+
| `altText` | A short description for an alternate way to carry out the action. For screen reader users who will not be able to navigate to the button easily/quickly. A rendered action requires altText, unless the toast passed to ToastRoot has actionProps.altText. | `string` | No | - |
4141
| `as` | The element or component this component should render as. Can be overwritten by asChild. | `AsTag \| Component` | No | `"button"` |
4242
| `asChild` | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | `boolean` | No | - |
4343
| `closeOnClick` | Whether the action should close the toast when clicked. | `boolean` | No | `true` |

‎docs/content/meta/ToastArrow.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!-- This file was automatically generated. Do not edit it manually -->
2+
3+
<llm-exclude>
4+
<PropsTable :data="[
5+
{
6+
'name': 'as',
7+
'description': '<p>The element or component this component should render as. Can be overwritten by <code>asChild</code>.</p>\n',
8+
'type': 'AsTag | Component',
9+
'required': false,
10+
'default': '\'svg\''
11+
},
12+
{
13+
'name': 'asChild',
14+
'description': '<p>Change the default rendered element for the one passed as a child, merging their props and behavior.</p>\n<p>Read our <a href=\'https://www.reka-ui.com/docs/guides/composition\'>Composition</a> guide for more details.</p>\n',
15+
'type': 'boolean',
16+
'required': false
17+
},
18+
{
19+
'name': 'height',
20+
'description': '<p>The height of the arrow in pixels.</p>\n',
21+
'type': 'number',
22+
'required': false,
23+
'default': '5'
24+
},
25+
{
26+
'name': 'width',
27+
'description': '<p>The width of the arrow in pixels.</p>\n',
28+
'type': 'number',
29+
'required': false,
30+
'default': '10'
31+
}
32+
]" />
33+
</llm-exclude>
34+
35+
<llm-only>
36+
37+
**Props**
38+
39+
| Name | Description | Type | Required | Default |
40+
| --- | --- | --- | --- | --- |
41+
| `as` | The element or component this component should render as. Can be overwritten by asChild. | `AsTag \| Component` | No | `"svg"` |
42+
| `asChild` | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | `boolean` | No | - |
43+
| `height` | The height of the arrow in pixels. | `number` | No | `5` |
44+
| `width` | The width of the arrow in pixels. | `number` | No | `10` |
45+
46+
</llm-only>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<!-- This file was automatically generated. Do not edit it manually -->
2+
3+
<llm-exclude>
4+
<PropsTable :data="[
5+
{
6+
'name': 'align',
7+
'description': '<p>The preferred alignment against the trigger.\nMay change when collisions occur.</p>\n',
8+
'type': '\'start\' | \'center\' | \'end\'',
9+
'required': false
10+
},
11+
{
12+
'name': 'alignFlip',
13+
'description': '<p>Flip alignment when colliding with boundary.\nMay only occur when <code>prioritizePosition</code> is true.</p>\n',
14+
'type': 'boolean',
15+
'required': false
16+
},
17+
{
18+
'name': 'alignOffset',
19+
'description': '<p>An offset in pixels from the <code>start</code> or <code>end</code> alignment options.</p>\n',
20+
'type': 'number',
21+
'required': false
22+
},
23+
{
24+
'name': 'anchor',
25+
'description': '<p>The element to position the toast against. Defaults to <code>toast.positionerProps.anchor</code>.</p>\n',
26+
'type': 'ReferenceElement | null',
27+
'required': false
28+
},
29+
{
30+
'name': 'arrowPadding',
31+
'description': '<p>The padding between the arrow and the edges of the content.\nIf your content has border-radius, this will prevent it from\noverflowing the corners.</p>\n',
32+
'type': 'number',
33+
'required': false
34+
},
35+
{
36+
'name': 'as',
37+
'description': '<p>The element or component this component should render as. Can be overwritten by <code>asChild</code>.</p>\n',
38+
'type': 'AsTag | Component',
39+
'required': false,
40+
'default': '\'div\''
41+
},
42+
{
43+
'name': 'asChild',
44+
'description': '<p>Change the default rendered element for the one passed as a child, merging their props and behavior.</p>\n<p>Read our <a href=\'https://www.reka-ui.com/docs/guides/composition\'>Composition</a> guide for more details.</p>\n',
45+
'type': 'boolean',
46+
'required': false
47+
},
48+
{
49+
'name': 'avoidCollisions',
50+
'description': '<p>When <code>true</code>, overrides the side and align preferences\nto prevent collisions with boundary edges.</p>\n',
51+
'type': 'boolean',
52+
'required': false
53+
},
54+
{
55+
'name': 'collisionBoundary',
56+
'description': '<p>The element used as the collision boundary. By default\nthis is the viewport, though you can provide additional\nelement(s) to be included in this check.</p>\n',
57+
'type': 'Element | (Element | null)[] | null',
58+
'required': false
59+
},
60+
{
61+
'name': 'collisionPadding',
62+
'description': '<p>The distance in pixels from the boundary edges where collision\ndetection should occur. Accepts a number (same for all sides),\nor a partial padding object, for example: { top: 20, left: 20 }.</p>\n',
63+
'type': 'number | Partial&lt;Record&lt;\'top\' | \'right\' | \'bottom\' | \'left\', number&gt;&gt;',
64+
'required': false
65+
},
66+
{
67+
'name': 'dir',
68+
'description': '<p>The reading direction of the popper content when applicable. &lt;br&gt; If omitted, inherits globally from <code>ConfigProvider</code> or assumes LTR (left-to-right) reading mode.</p>\n',
69+
'type': '\'ltr\' | \'rtl\'',
70+
'required': false
71+
},
72+
{
73+
'name': 'disableUpdateOnLayoutShift',
74+
'description': '<p>Whether to disable the update position for the content when the layout shifted.</p>\n',
75+
'type': 'boolean',
76+
'required': false
77+
},
78+
{
79+
'name': 'hideShiftedArrow',
80+
'description': '<p>When <code>true</code>, hides the arrow when it cannot be centered\nto the reference element.</p>\n',
81+
'type': 'boolean',
82+
'required': false
83+
},
84+
{
85+
'name': 'hideWhenDetached',
86+
'description': '<p>Whether to hide the content when the trigger becomes fully occluded.</p>\n',
87+
'type': 'boolean',
88+
'required': false
89+
},
90+
{
91+
'name': 'positionStrategy',
92+
'description': '<p>The type of CSS position property to use.</p>\n',
93+
'type': '\'fixed\' | \'absolute\'',
94+
'required': false
95+
},
96+
{
97+
'name': 'prioritizePosition',
98+
'description': '<p>Force content to be position within the viewport.</p>\n<p>Might overlap the reference element, which may not be desired.</p>\n',
99+
'type': 'boolean',
100+
'required': false
101+
},
102+
{
103+
'name': 'side',
104+
'description': '<p>The preferred side of the trigger to render against when open.\nWill be reversed when collisions occur and avoidCollisions\nis enabled.</p>\n',
105+
'type': '\'top\' | \'right\' | \'bottom\' | \'left\'',
106+
'required': false
107+
},
108+
{
109+
'name': 'sideFlip',
110+
'description': '<p>Flip to the opposite side when colliding with boundary.</p>\n',
111+
'type': 'boolean',
112+
'required': false
113+
},
114+
{
115+
'name': 'sideOffset',
116+
'description': '<p>The distance in pixels from the trigger.</p>\n',
117+
'type': 'number',
118+
'required': false
119+
},
120+
{
121+
'name': 'sticky',
122+
'description': '<p>The sticky behavior on the align axis. <code>partial</code> will keep the\ncontent in the boundary as long as the trigger is at least partially\nin the boundary whilst &quot;always&quot; will keep the content in the boundary\nregardless.</p>\n',
123+
'type': '\'partial\' | \'always\'',
124+
'required': false
125+
},
126+
{
127+
'name': 'toast',
128+
'description': '<p>The toast to position. Its <code>positionerProps</code> are used for any prop that is not set.</p>\n',
129+
'type': 'ToastObject&lt;any&gt;',
130+
'required': true
131+
},
132+
{
133+
'name': 'updatePositionStrategy',
134+
'description': '<p>Strategy to update the position of the floating element on every animation frame.</p>\n',
135+
'type': '\'always\' | \'optimized\'',
136+
'required': false
137+
}
138+
]" />
139+
</llm-exclude>
140+
141+
<llm-only>
142+
143+
**Props**
144+
145+
| Name | Description | Type | Required | Default |
146+
| --- | --- | --- | --- | --- |
147+
| `align` | The preferred alignment against the trigger. May change when collisions occur. | `"start" \| "center" \| "end"` | No | - |
148+
| `alignFlip` | Flip alignment when colliding with boundary. May only occur when prioritizePosition is true. | `boolean` | No | - |
149+
| `alignOffset` | An offset in pixels from the start or end alignment options. | `number` | No | - |
150+
| `anchor` | The element to position the toast against. Defaults to toast.positionerProps.anchor. | `ReferenceElement \| null` | No | - |
151+
| `arrowPadding` | The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. | `number` | No | - |
152+
| `as` | The element or component this component should render as. Can be overwritten by asChild. | `AsTag \| Component` | No | `"div"` |
153+
| `asChild` | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | `boolean` | No | - |
154+
| `avoidCollisions` | When true, overrides the side and align preferences to prevent collisions with boundary edges. | `boolean` | No | - |
155+
| `collisionBoundary` | The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check. | `Element \| (Element \| null)[] \| null` | No | - |
156+
| `collisionPadding` | The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. | `number \| Partial<Record<"top" \| "right" \| "bottom" \| "left", number>>` | No | - |
157+
| `dir` | The reading direction of the popper content when applicable. <br> If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode. | `"ltr" \| "rtl"` | No | - |
158+
| `disableUpdateOnLayoutShift` | Whether to disable the update position for the content when the layout shifted. | `boolean` | No | - |
159+
| `hideShiftedArrow` | When true, hides the arrow when it cannot be centered to the reference element. | `boolean` | No | - |
160+
| `hideWhenDetached` | Whether to hide the content when the trigger becomes fully occluded. | `boolean` | No | - |
161+
| `positionStrategy` | The type of CSS position property to use. | `"fixed" \| "absolute"` | No | - |
162+
| `prioritizePosition` | Force content to be position within the viewport. Might overlap the reference element, which may not be desired. | `boolean` | No | - |
163+
| `side` | The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled. | `"top" \| "right" \| "bottom" \| "left"` | No | - |
164+
| `sideFlip` | Flip to the opposite side when colliding with boundary. | `boolean` | No | - |
165+
| `sideOffset` | The distance in pixels from the trigger. | `number` | No | - |
166+
| `sticky` | The sticky behavior on the align axis. partial will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. | `"partial" \| "always"` | No | - |
167+
| `toast` | The toast to position. Its positionerProps are used for any prop that is not set. | `ToastObject<any>` | Yes | - |
168+
| `updatePositionStrategy` | Strategy to update the position of the floating element on every animation frame. | `"always" \| "optimized"` | No | - |
169+
170+
</llm-only>

‎docs/content/meta/ToastProvider.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
'required': false,
2323
'default': '\'Notification\''
2424
},
25+
{
26+
'name': 'limit',
27+
'description': '<p>The maximum number of toasts shown at once. Older toasts beyond the limit get\n<code>data-limited</code> and <code>inert</code> rather than being removed, so they can be hidden or animated.\nNo limit is applied when unset.</p>\n',
28+
'type': 'number',
29+
'required': false
30+
},
2531
{
2632
'name': 'swipeDirection',
2733
'description': '<p>Direction of pointer swipe that should close the toast.</p>\n',
@@ -35,6 +41,12 @@
3541
'type': 'number',
3642
'required': false,
3743
'default': '50'
44+
},
45+
{
46+
'name': 'toastManager',
47+
'description': '<p>A manager created with <code>createToastManager()</code>, to add toasts from outside\ncomponents. Its toasts are listed by <code>useToastManager()</code>.</p>\n',
48+
'type': 'GlobalToastManager&lt;any&gt;',
49+
'required': false
3850
}
3951
]" />
4052
</llm-exclude>
@@ -48,7 +60,9 @@
4860
| `disableSwipe` | Whether to disable the ability to swipe to close the toast. | `boolean` | No | - |
4961
| `duration` | Time in milliseconds that each toast should remain visible for. | `number` | No | `5000` |
5062
| `label` | An author-localized label for each toast. Used to help screen reader users associate the interruption with a toast. | `string` | No | `"Notification"` |
63+
| `limit` | The maximum number of toasts shown at once. Older toasts beyond the limit get data-limited and inert rather than being removed, so they can be hidden or animated. No limit is applied when unset. | `number` | No | - |
5164
| `swipeDirection` | Direction of pointer swipe that should close the toast. | `"right" \| "left" \| "down" \| "up"` | No | `"right"` |
5265
| `swipeThreshold` | Distance in pixels that the swipe must pass before a close is triggered. | `number` | No | `50` |
66+
| `toastManager` | A manager created with createToastManager(), to add toasts from outside components. Its toasts are listed by useToastManager(). | `GlobalToastManager<any>` | No | - |
5367

5468
</llm-only>

‎docs/content/meta/ToastRoot.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
'type': 'boolean',
4141
'required': false
4242
},
43+
{
44+
'name': 'toast',
45+
'description': '<p>A toast from <code>useToastManager()</code>. Its <code>open</code>, <code>duration</code>, <code>type</code> and <code>status</code> drive the toast,\nand <code>ToastTitle</code>, <code>ToastDescription</code> and <code>ToastAction</code> render its content.</p>\n',
46+
'type': 'ToastObject&lt;any&gt;',
47+
'required': false
48+
},
4349
{
4450
'name': 'type',
4551
'description': '<p>Control the sensitivity of the toast for accessibility purposes.</p>\n<p>For toasts that are the result of a user action, choose <code>foreground</code>. Toasts generated from background tasks should use <code>background</code>.</p>\n',
@@ -123,6 +129,7 @@
123129
| `duration` | Time in milliseconds that toast should remain visible for. Overrides value given to ToastProvider. | `number` | No | - |
124130
| `forceMount` | Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | `boolean` | No | - |
125131
| `open` | The controlled open state of the dialog. Can be bind as v-model:open. | `boolean` | No | - |
132+
| `toast` | A toast from useToastManager(). Its open, duration, type and status drive the toast, and ToastTitle, ToastDescription and ToastAction render its content. | `ToastObject<any>` | No | - |
126133
| `type` | Control the sensitivity of the toast for accessibility purposes. For toasts that are the result of a user action, choose foreground. Toasts generated from background tasks should use background. | `"foreground" \| "background"` | No | `"foreground"` |
127134

128135
**Events**

‎packages/core/constant/components.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ export const components = {
515515
'ToastViewport',
516516
'ToastTitle',
517517
'ToastDescription',
518+
'ToastPositioner',
519+
'ToastArrow',
518520
] as const,
519521

520522
toggle: [
@@ -623,6 +625,8 @@ export const utilities = {
623625
'useLocale',
624626
'withDefault',
625627
'createContext',
628+
'createToastManager',
629+
'useToastManager',
626630
] as const,
627631
}
628632

‎packages/core/src/Toast/ToastAction.vue‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ export interface ToastActionProps extends ToastCloseProps {
55
/**
66
* A short description for an alternate way to carry out the action. For screen reader users
77
* who will not be able to navigate to the button easily/quickly.
8+
*
9+
* A rendered action requires `altText`, unless the toast passed to `ToastRoot` has `actionProps.altText`.
810
* @example <ToastAction altText="Goto account settings to upgrade">Upgrade</ToastAction>
911
* @example <ToastAction altText="Undo (Alt+U)">Undo</ToastAction>
1012
*/
11-
altText: string
13+
altText?: string
1214
/**
1315
* Whether the action should close the toast when clicked.
1416
*
@@ -19,6 +21,7 @@ export interface ToastActionProps extends ToastCloseProps {
1921
</script>
2022

2123
<script setup lang="ts">
24+
import { computed, useSlots } from 'vue'
2225
import { Primitive } from '@/Primitive'
2326
import { useForwardExpose } from '@/shared'
2427
import ToastAnnounceExclude from './ToastAnnounceExclude.vue'
@@ -29,16 +32,28 @@ const props = withDefaults(defineProps<ToastActionProps>(), {
2932
closeOnClick: true,
3033
})
3134
32-
if (!props.altText)
33-
throw new Error('Missing prop `altText` expected on `ToastAction`')
34-
3535
const rootContext = injectToastRootContext()
3636
const { forwardRef } = useForwardExpose()
37+
const slots = useSlots()
38+
39+
const actionProps = computed(() => rootContext.toast.value?.actionProps)
40+
// A managed toast without `actionProps` renders no action, unless slot content is given.
41+
const isRendered = computed(() => !!slots.default || !rootContext.toast.value || !!actionProps.value)
42+
const altText = computed(() => props.altText ?? actionProps.value?.altText)
43+
44+
if (isRendered.value && !altText.value)
45+
throw new Error('Missing prop `altText` expected on `ToastAction`')
46+
47+
function handleClick(event: MouseEvent) {
48+
actionProps.value?.onClick?.(event)
49+
if (actionProps.value?.closeOnClick ?? props.closeOnClick)
50+
rootContext.onClose()
51+
}
3752
</script>
3853

3954
<template>
4055
<ToastAnnounceExclude
41-
v-if="altText"
56+
v-if="isRendered && altText"
4257
:alt-text="altText"
4358
as-child
4459
>
@@ -47,9 +62,9 @@ const { forwardRef } = useForwardExpose()
4762
:as="as"
4863
:as-child="asChild"
4964
:type="as === 'button' ? 'button' : undefined"
50-
@click="closeOnClick ? rootContext.onClose() : undefined"
65+
@click="handleClick"
5166
>
52-
<slot />
67+
<slot>{{ actionProps?.label }}</slot>
5368
</Primitive>
5469
</ToastAnnounceExclude>
5570
</template>

0 commit comments

Comments
 (0)