An open source design system that's fully customizable and agent ready
TypeScript
fix(Toast): a Theme in fallback-viewport toast content stays nested and never rewrites <html> (#9253)
Cindy Zhang995036e69e0e
diff --git a/.changeset/toast-fallback-viewport-nested-theme.md b/.changeset/toast-fallback-viewport-nested-theme.mdnew file mode 100644index 0000000000..da61131f85--- /dev/null+++ b/.changeset/toast-fallback-viewport-nested-theme.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Toast: a `Theme` inside toast content shown by the fallback viewport (no `LayerProvider`) now themes only its own subtree. It no longer rewrites the page's `<html>` theme as if it were the app's root `Theme`. First fixed in #5617.++@cixzhang @AKnassadiff --git a/packages/core/src/Toast/useToast.test.tsx b/packages/core/src/Toast/useToast.test.tsxindex 4f12555acb..5fae53c906 100644--- a/packages/core/src/Toast/useToast.test.tsx+++ b/packages/core/src/Toast/useToast.test.tsx@@ -3,11 +3,14 @@/*** @file useToast.test.tsx* @input Uses vitest, @testing-library/react, useToast/ToastViewport/Theme- * @output Unit tests for the fallback viewport's theme mode resolution+ * @output Unit tests for the fallback viewport: its theme mode resolution,+ * and the Theme nesting its root provides (a <Theme> inside toast content+ * scopes its own subtree and never rewrites <html>)* @position Testing; validates useToast.tsx's attribute mirroring onto the- * fallback container, plus useTheme.ts's <html data-theme> fallback (which- * is what actually resolves Toast's JS-computed mode; see useTheme.test.tsx- * for direct coverage of that half)+ * fallback container and the ThemeNestingContext its root renders under,+ * plus useTheme.ts's <html data-theme> fallback (which is what actually+ * resolves Toast's JS-computed mode; see useTheme.test.tsx for direct+ * coverage of that half)** SYNC: When useToast.tsx's fallback container setup changes, update these tests*@@ -104,6 +107,65 @@ describe('useToast fallback viewport theme mode', () => {await dismissAllFallbackToasts();});+ it('keeps a Theme inside fallback toast content nested: it never rewrites <html>', async () => {+ // The fallback root is a separate React root, so a brand Theme in toast+ // content sees no Theme ancestor. It must not act as the app's root+ // Theme: <html data-theme> / data-astryx-theme stay the app's, while+ // the brand Theme still scopes its own subtree.+ const brand = defineTheme({name: 'fallback-brand', tokens: {}});+ function BrandedToastButton() {+ const toast = useToast();+ return (+ <button+ type="button"+ onClick={() =>+ toast({+ body: 'branded',+ isAutoHide: false,+ endContent: (+ <Theme theme={brand} mode="dark">+ <span>brand</span>+ </Theme>+ ),+ })+ }>+ Trigger branded+ </button>+ );+ }++ render(+ <Theme theme={testTheme} mode="light">+ <BrandedToastButton />+ </Theme>,+ );+ await waitFor(() => {+ expect(document.documentElement).toHaveAttribute('data-theme', 'light');+ });+ // Whichever test clicks first in this file finds the fallback proxy still+ // pending: addToast queues the entry and kicks off the ctxReady.then(...)+ // microtask that later delivers it to the real context. Awaiting an async+ // act() flushes that chain instead of letting it resolve outside any act+ // scope.+ await act(async () => {+ fireEvent.click(screen.getByText('Trigger branded'));+ });+ await waitFor(() => {+ expect(screen.getByText('brand')).toBeInTheDocument();+ });+ await act(async () => {});++ expect(document.documentElement).toHaveAttribute('data-theme', 'light');+ expect(document.documentElement).toHaveAttribute(+ 'data-astryx-theme',+ 'test',+ );+ const brandScope = document.querySelector(+ '[data-astryx-toast-fallback] [data-astryx-theme="fallback-brand"]',+ );+ expect(brandScope).toHaveAttribute('data-theme', 'dark');+ });+it('resolves the app mode (light) instead of OS preference (dark) with no LayerProvider', async () => {mockMatchMedia(true); // OS prefers dark@@ -113,11 +175,8 @@ describe('useToast fallback viewport theme mode', () => {</Theme>,);- // This is the very first click in the whole file, so it's the one that- // finds the fallback proxy still pending: addToast queues the entry and- // kicks off the ctxReady.then(...) microtask that later delivers it to- // the real context. Awaiting an async act() here flushes that chain- // instead of letting it resolve outside any act scope.+ // Async act for the same reason as the first test above: under .only, -t+ // or a shuffled order this can be the click that finds the proxy pending.await act(async () => {fireEvent.click(screen.getByText('Trigger'));});diff --git a/packages/core/src/Toast/useToast.tsx b/packages/core/src/Toast/useToast.tsxindex 87b61bb604..e93d48318e 100644--- a/packages/core/src/Toast/useToast.tsx+++ b/packages/core/src/Toast/useToast.tsx@@ -7,6 +7,7 @@ import {createRoot} from 'react-dom/client';import {dataAttr} from '../naming';import {ToastContext, type ToastContextValue} from './ToastContext';import {ToastViewport} from './ToastViewport';+import {ThemeNestingContext} from '../theme/Theme';import {warnOnce} from '../utils/devWarning';import type {ToastOptions,@@ -101,10 +102,14 @@ function getFallbackContext(): ToastContextValue {};fallbackRoot = createRoot(container);+ // This root sits inside the app, so a <Theme> in toast content is nested:+ // it themes its own subtree and leaves the app's <html> sync alone.fallbackRoot.render(- <ToastViewport>- <FallbackCapture />- </ToastViewport>,+ <ThemeNestingContext value={true}>+ <ToastViewport>+ <FallbackCapture />+ </ToastViewport>+ </ThemeNestingContext>,);// Proxy that queues calls until real context is captureddiff --git a/packages/core/src/theme/Theme.tsx b/packages/core/src/theme/Theme.tsxindex 4038421c37..8b3e98d0a1 100644--- a/packages/core/src/theme/Theme.tsx+++ b/packages/core/src/theme/Theme.tsx@@ -83,11 +83,15 @@ const wrapperStyles = stylex.create({// =============================================================================/**- * Context to detect whether this Theme is nested inside another.- * The root provider (no parent context) syncs data-theme to <html>.- * @internal+ * True below a Theme; the root Theme (no parent context) syncs data-theme+ * to <html>. A detached React root that conceptually sits inside the app+ * (useToast's fallback viewport) provides it as `true` itself, so a Theme+ * rendered in toast content themes its subtree without claiming <html> as+ * if it were the app's root Theme.+ * @internal Not exported from the theme barrel; useToast imports it from+ * './theme/Theme' directly.*/-const ThemeNestingContext = React.createContext(false);+export const ThemeNestingContext = React.createContext(false);ThemeNestingContext.displayName = 'ThemeNestingContext';// =============================================================================