An open source design system that's fully customizable and agent ready
TypeScript
feat(templates): render Toast blocks inline with XDSToast (#1673)
Cindy Zhang9f2a7dad3f28
diff --git a/packages/cli/templates/blocks/components/Toast/ToastAction.tsx b/packages/cli/templates/blocks/components/Toast/ToastAction.tsxindex 2bb456af44..222b541cbf 100644--- a/packages/cli/templates/blocks/components/Toast/ToastAction.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastAction.tsx@@ -1,54 +1,46 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';import {XDSLink} from '@xds/core/Link';-import {XDSStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';+import {XDSVStack} from '@xds/core/Layout';export default function ToastAction() {const toast = useXDSToast();return (- <XDSStack direction="vertical" gap={4}>- <XDSText type="supporting" color="secondary">- Trailing actions for undo or navigation- </XDSText>- <XDSStack direction="horizontal" gap={3} vAlign="center">- <XDSButton- label="Delete with undo"- variant="secondary"- onClick={() =>- toast({- body: 'Item deleted',- isAutoHide: false,- endContent: (- <XDSButton- label="Undo"- variant="secondary"- size="sm"- onClick={() => {}}- />- ),- })- }- />- <XDSButton- label="Report ready"- variant="secondary"- onClick={() =>- toast({- body: 'Your report is ready.',- isAutoHide: false,- endContent: (- <XDSLink href="#" label="View report" hasUnderline>- View report- </XDSLink>- ),- })- }- />- </XDSStack>- </XDSStack>+ <XDSVStack gap={3}>+ <XDSToast+ type="info"+ body="Item deleted"+ endContent={+ <XDSButton+ label="Undo"+ variant="secondary"+ size="sm"+ onClick={() => toast({body: 'Undo successful', type: 'info'})}+ />+ }+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ <XDSToast+ type="info"+ body="Your report is ready."+ endContent={+ <XDSLink href="#" label="View report" hasUnderline>+ View report+ </XDSLink>+ }+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ </XDSVStack>);}diff --git a/packages/cli/templates/blocks/components/Toast/ToastDeduplication.tsx b/packages/cli/templates/blocks/components/Toast/ToastDeduplication.tsxindex d6f4f0f13f..a6e52757f1 100644--- a/packages/cli/templates/blocks/components/Toast/ToastDeduplication.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastDeduplication.tsx@@ -1,22 +1,29 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';-import {XDSStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';+import {XDSVStack, XDSHStack} from '@xds/core/Layout';export default function ToastDeduplication() {const toast = useXDSToast();return (- <XDSStack direction="vertical" gap={4}>- <XDSText type="supporting" color="secondary">- Ignore keeps the first toast; overwrite replaces it- </XDSText>- <XDSStack direction="horizontal" gap={3} vAlign="center">+ <XDSVStack gap={3}>+ <XDSToast+ type="info"+ body="You are offline"+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ <XDSHStack gap={3} vAlign="center"><XDSButtonlabel="Offline (ignore)"variant="secondary"+ size="sm"onClick={() =>toast({body: 'You are offline',@@ -29,16 +36,17 @@ export default function ToastDeduplication() {<XDSButtonlabel="Progress (overwrite)"variant="secondary"+ size="sm"onClick={() =>toast({- body: `Uploading\u2026 ${Math.floor(Math.random() * 100)}%`,+ body: `Uploading… ${Math.floor(Math.random() * 100)}%`,uniqueID: 'upload-progress',collisionBehavior: 'overwrite',isAutoHide: false,})}/>- </XDSStack>- </XDSStack>+ </XDSHStack>+ </XDSVStack>);}diff --git a/packages/cli/templates/blocks/components/Toast/ToastDismiss.tsx b/packages/cli/templates/blocks/components/Toast/ToastDismiss.tsxindex 3859ff7f48..9e78f1d37d 100644--- a/packages/cli/templates/blocks/components/Toast/ToastDismiss.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastDismiss.tsx@@ -1,40 +1,48 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';import {useRef} from 'react';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';-import {XDSStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';+import {XDSVStack, XDSHStack} from '@xds/core/Layout';export default function ToastDismiss() {const toast = useXDSToast();const dismissRef = useRef<(() => void) | null>(null);return (- <XDSStack direction="vertical" gap={4}>- <XDSText type="supporting" color="secondary">- Show a persistent toast, then dismiss it from code- </XDSText>- <XDSStack direction="horizontal" gap={3} vAlign="center">+ <XDSVStack gap={3}>+ <XDSToast+ type="info"+ body="Uploading file…"+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ <XDSHStack gap={3} vAlign="center"><XDSButton- label="Show persistent toast"+ label="Show toast"variant="secondary"+ size="sm"onClick={() => {dismissRef.current = toast({- body: 'Uploading file\u2026',+ body: 'Uploading file…',isAutoHide: false,});}}/><XDSButton- label="Dismiss"+ label="Dismiss via code"variant="ghost"+ size="sm"onClick={() => {dismissRef.current?.();dismissRef.current = null;}}/>- </XDSStack>- </XDSStack>+ </XDSHStack>+ </XDSVStack>);}diff --git a/packages/cli/templates/blocks/components/Toast/ToastShowcase.doc.mjs b/packages/cli/templates/blocks/components/Toast/ToastShowcase.doc.mjsindex ff8b471aeb..16c2641e20 100644--- a/packages/cli/templates/blocks/components/Toast/ToastShowcase.doc.mjs+++ b/packages/cli/templates/blocks/components/Toast/ToastShowcase.doc.mjs@@ -5,4 +5,5 @@ export const doc = {isReady: true,aspectRatio: 16 / 9,isShowcase: true,+ componentsUsed: ['Toast', 'Button'],};diff --git a/packages/cli/templates/blocks/components/Toast/ToastShowcase.tsx b/packages/cli/templates/blocks/components/Toast/ToastShowcase.tsxindex 8664eefe63..f51fa613ee 100644--- a/packages/cli/templates/blocks/components/Toast/ToastShowcase.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastShowcase.tsx@@ -1,14 +1,28 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';export default function ToastShowcase() {const toast = useXDSToast();return (- <XDSButton- label="Show toast"- onClick={() => toast({body: 'This is an info toast'})}+ <XDSToast+ type="info"+ body="Document saved successfully"+ endContent={+ <XDSButton+ label="Show toast"+ variant="ghost"+ size="sm"+ onClick={() => toast({body: 'Document saved successfully'})}+ />+ }+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}/>);}diff --git a/packages/cli/templates/blocks/components/Toast/ToastStacking.tsx b/packages/cli/templates/blocks/components/Toast/ToastStacking.tsxindex b5e7abf934..b361a1fe5b 100644--- a/packages/cli/templates/blocks/components/Toast/ToastStacking.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastStacking.tsx@@ -1,16 +1,16 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';import {useRef} from 'react';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';-import {XDSStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';+import {XDSVStack} from '@xds/core/Layout';const MESSAGES = [{body: 'Changes saved.', type: 'info' as const},{body: 'Failed to upload file.', type: 'error' as const},{body: 'Message sent to Sarah Chen.', type: 'info' as const},- {body: 'Connection lost.', type: 'error' as const},];export default function ToastStacking() {@@ -18,18 +18,28 @@ export default function ToastStacking() {const countRef = useRef(0);return (- <XDSStack direction="vertical" gap={4}>- <XDSText type="supporting" color="secondary">- Click multiple times to see stacking behavior- </XDSText>+ <XDSVStack gap={3}>+ {MESSAGES.map(msg => (+ <XDSToast+ key={msg.body}+ type={msg.type}+ body={msg.body}+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ ))}<XDSButton- label="Add toast"+ label="Show toast"+ variant="secondary"+ size="sm"onClick={() => {const msg = MESSAGES[countRef.current % MESSAGES.length];countRef.current++;toast(msg);}}/>- </XDSStack>+ </XDSVStack>);}diff --git a/packages/cli/templates/blocks/components/Toast/ToastTypes.tsx b/packages/cli/templates/blocks/components/Toast/ToastTypes.tsxindex eaf843262e..be13838a9f 100644--- a/packages/cli/templates/blocks/components/Toast/ToastTypes.tsx+++ b/packages/cli/templates/blocks/components/Toast/ToastTypes.tsx@@ -1,34 +1,52 @@+// In production, use useXDSToast() hook for proper positioning, stacking, and lifecycle.'use client';+import {XDSToast} from '@xds/core/Toast';import {useXDSToast} from '@xds/core/Toast';import {XDSButton} from '@xds/core/Button';-import {XDSStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';+import {XDSVStack} from '@xds/core/Layout';export default function ToastTypes() {const toast = useXDSToast();return (- <XDSStack direction="vertical" gap={4}>- <XDSText type="supporting" color="secondary">- Click each button to see the toast variant- </XDSText>- <XDSStack direction="horizontal" gap={3} vAlign="center">- <XDSButton- label="Info toast"- variant="secondary"- onClick={() =>- toast({body: 'Changes saved successfully.', type: 'info'})- }- />- <XDSButton- label="Error toast"- variant="destructive"- onClick={() =>- toast({body: 'Failed to save changes.', type: 'error'})- }- />- </XDSStack>- </XDSStack>+ <XDSVStack gap={3}>+ <XDSToast+ type="info"+ body="Changes saved successfully."+ endContent={+ <XDSButton+ label="Show toast"+ variant="ghost"+ size="sm"+ onClick={() =>+ toast({body: 'Changes saved successfully.', type: 'info'})+ }+ />+ }+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ <XDSToast+ type="error"+ body="Failed to save changes."+ endContent={+ <XDSButton+ label="Show toast"+ variant="ghost"+ size="sm"+ onClick={() =>+ toast({body: 'Failed to save changes.', type: 'error'})+ }+ />+ }+ isAutoHide={false}+ autoHideDuration={5000}+ isExiting={false}+ onDismiss={() => {}}+ />+ </XDSVStack>);}diff --git a/packages/core/src/Toast/Toast.doc.mjs b/packages/core/src/Toast/Toast.doc.mjsindex e20a265e1c..03a1dcfec1 100644--- a/packages/core/src/Toast/Toast.doc.mjs+++ b/packages/core/src/Toast/Toast.doc.mjs@@ -59,7 +59,7 @@ export const docs = {usage: {description:- 'Toast shows a brief, non-blocking notification to confirm an action or present temporary information. Use it for scenarios where the user needs feedback but not a decision, such as saving, deleting, or changing a status.',+ 'Toast shows a brief, non-blocking notification to confirm an action or present temporary information. Use it for scenarios where the user needs feedback but not a decision, such as saving, deleting, or changing a status.\n\nFor production use, prefer the `useXDSToast()` hook — it handles positioning, stacking, auto-dismiss, and deduplication via `XDSToastViewport`. The `XDSToast` component renders the visual toast element inline and is useful for previews, documentation, and static showcases where the viewport lifecycle is not needed.',bestPractices: [{guidance: true, description: 'Keep messages short - only a few words that tell the user what happened, like "Changes saved" or "Message sent".'},{guidance: true, description: 'Add an undo action in the endContent slot for reversible operations like deleting an item, so the user can recover without navigating away.'},@@ -97,7 +97,7 @@ export const docsZh = {},usage: {description:- 'Toast 显示简短的非阻塞通知,用于确认操作或呈现临时信息。适用于用户需要反馈但不需要做决定的场景,如保存、删除或状态变更。',+ 'Toast 显示简短的非阻塞通知,用于确认操作或呈现临时信息。适用于用户需要反馈但不需要做决定的场景,如保存、删除或状态变更。\n\n生产环境中推荐使用 `useXDSToast()` hook——它通过 `XDSToastViewport` 处理定位、堆叠、自动关闭和去重。`XDSToast` 组件以内联方式渲染 toast 视觉元素,适用于预览、文档和静态展示。',bestPractices: [{guidance: true, description: '保持消息简短——只需几个词告诉用户发生了什么,如"更改已保存"或"消息已发送"。'},{guidance: true, description: '在 endContent 插槽中添加撤销操作,用于可逆操作如删除项目,让用户无需导航即可恢复。'},@@ -121,7 +121,7 @@ export const docsDense = {'toast notification w/ auto-dismiss, stacking, dedup, smooth animations; XDSMediaTheme inverted surface',usage: {description:- 'Brief non-blocking notification for action confirmations and temporary info. Use where user needs feedback not decisions — saves, deletes, status changes.',+ 'Brief non-blocking notification for action confirmations and temporary info. Use where user needs feedback not decisions — saves, deletes, status changes. useXDSToast() hook for production (positioning, stacking, auto-dismiss, dedup via XDSToastViewport). XDSToast renders inline for previews/docs/static showcases.',bestPractices: [{guidance: true, description: 'Short messages — a few words: "Changes saved", "Message sent".'},{guidance: true, description: 'Undo action in endContent for reversible ops like deletes.'},diff --git a/packages/core/src/Toast/XDSToast.tsx b/packages/core/src/Toast/XDSToast.tsxindex 3b1338853e..8a16cc36b2 100644--- a/packages/core/src/Toast/XDSToast.tsx+++ b/packages/core/src/Toast/XDSToast.tsx@@ -26,7 +26,7 @@ const styles = stylex.create({paddingInline: spacingVars['--spacing-4'],borderRadius: radiusVars['--radius-container'],width: 400,- maxWidth: 'calc(100vw - 32px)',+ maxWidth: 'min(100%, calc(100vw - 32px))',boxShadow: shadowVars['--shadow-med'],opacity: 1,fontFamily: typographyVars['--font-family-body'],diff --git a/packages/core/src/Toast/index.ts b/packages/core/src/Toast/index.tsindex 63c8116d85..2a149a8864 100644--- a/packages/core/src/Toast/index.ts+++ b/packages/core/src/Toast/index.ts@@ -15,3 +15,7 @@ export type {// Exported for XDSLayerProvider integrationexport {XDSToastViewport} from './XDSToastViewport';export type {XDSToastViewportProps} from './XDSToastViewport';++// Exported for inline rendering in previews and documentation+export {XDSToast} from './XDSToast';+export type {XDSToastProps} from './XDSToast';