Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Toast): useToast's fallback warning and SSR error name only LayerProvider (#10875)

Cindy Zhang00c1b374bace
diff --git a/.changeset/use-toast-warning-names-layer-provider.md b/.changeset/use-toast-warning-names-layer-provider.md
new file mode 100644
index 0000000000..578f0d463d
--- /dev/null
+++ b/.changeset/use-toast-warning-names-layer-provider.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Toast: the `useToast` fallback warning and its server-rendering error no longer tell you to wrap the app in `<AppShell>`. AppShell provides no layer context, so that advice left the warning in place. They now name only `<LayerProvider>`, which removes it.
+
+@cixzhang
diff --git a/packages/core/src/Toast/useToast.fallbackMessages.test.tsx b/packages/core/src/Toast/useToast.fallbackMessages.test.tsx
new file mode 100644
index 0000000000..32257121aa
--- /dev/null
+++ b/packages/core/src/Toast/useToast.fallbackMessages.test.tsx
@@ -0,0 +1,49 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file useToast.fallbackMessages.test.tsx
+ * @input useToast with no LayerProvider above it
+ * @output Unit tests that the fallback viewport's warning and its SSR error
+ * name only LayerProvider, the provider that removes them
+ * @position Testing; validates useToast.tsx. A file of its own because the
+ * fallback viewport is a module singleton: its warning and error fire only
+ * on the first toast in a module instance.
+ */
+
+import {describe, it, expect, vi, afterEach} from 'vitest';
+import {renderHook} from '@testing-library/react';
+import {useToast} from './useToast';
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+describe('useToast fallback messages', () => {
+ // AppShell renders no LayerProvider, so advice to wrap the app in AppShell
+ // does not remove the warning or the error.
+ it('names only LayerProvider when a toast cannot mount during SSR', () => {
+ const {result} = renderHook(() => useToast());
+ let message = '';
+ vi.stubGlobal('document', undefined);
+ try {
+ result.current({body: 'Saved'});
+ } catch (error) {
+ message = (error as Error).message;
+ }
+ vi.unstubAllGlobals();
+ expect(message).toContain('<LayerProvider>');
+ expect(message).not.toContain('AppShell');
+ });
+
+ it('names only LayerProvider in the fallback viewport warning', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ const {result} = renderHook(() => useToast());
+ result.current({body: 'Saved', isAutoHide: false});
+ const messages = warn.mock.calls.map(call => String(call[0]));
+ const fallback = messages.find(text => text.includes('fallback viewport'));
+ expect(fallback).toBeDefined();
+ expect(fallback).toContain('<LayerProvider>');
+ expect(fallback).not.toContain('AppShell');
+ });
+});
diff --git a/packages/core/src/Toast/useToast.tsx b/packages/core/src/Toast/useToast.tsx
index cb523c8db0..0bae457f35 100644
--- a/packages/core/src/Toast/useToast.tsx
+++ b/packages/core/src/Toast/useToast.tsx
@@ -68,7 +68,7 @@ function getFallbackContext(): ToastContextValue {
if (typeof document === 'undefined') {
throw new Error(
'useToast: Cannot create fallback viewport during SSR. ' +
- 'Wrap your app with <LayerProvider> or <AppShell>.',
+ 'Wrap your app with <LayerProvider>.',
);
}
@@ -76,7 +76,7 @@ function getFallbackContext(): ToastContextValue {
'toast-fallback-viewport',
'useToast',
'No LayerProvider found. Using fallback viewport. ' +
- 'Wrap your app with <LayerProvider> or <AppShell> for full control.',
+ 'Wrap your app with <LayerProvider> for full control.',
);
const container = document.createElement('div');