Skip to content

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

TypeScript

chore(sandbox): AlertDialog cases on a page the document scrolls (#10723)

Christopher Chedeau21c90aa3c1a0
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 0f338a474b..278c39b588 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -5,6 +5,7 @@
"/",
"/404/",
"/components-patterns/",
+ "/pages/alert-dialog-cases/",
"/pages/butter-palette/",
"/pages/chat-layout/",
"/pages/codeblock-perf/",
diff --git a/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/caseIds.ts b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/caseIds.ts
new file mode 100644
index 0000000000..4905e965c1
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/caseIds.ts
@@ -0,0 +1,32 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file caseIds.ts
+ * @input None
+ * @output The AlertDialog case ids, their labels, and the parser for `?case=`
+ * @position Shared by the AlertDialog cases page and its cases.
+ *
+ * SYNC: A new case needs a component in cases.tsx.
+ */
+
+export const ALERT_DIALOG_CASES = [
+ 'alert-over-a-toast',
+ 'dialog-closed-after-save',
+] as const;
+
+export type AlertDialogCaseId = (typeof ALERT_DIALOG_CASES)[number];
+
+export const ALERT_DIALOG_CASE_LABELS: Readonly<
+ Record<AlertDialogCaseId, string>
+> = {
+ 'alert-over-a-toast':
+ 'An alert over a toast: Delete raises a toast as the alert closes',
+ 'dialog-closed-after-save':
+ 'A dialog closed after a save: Save raises a toast, and the dialog closes when the save is done',
+};
+
+export function parseAlertDialogCase(value: string | null): AlertDialogCaseId {
+ return (ALERT_DIALOG_CASES as readonly string[]).includes(value ?? '')
+ ? (value as AlertDialogCaseId)
+ : ALERT_DIALOG_CASES[0];
+}
diff --git a/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/cases.tsx b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/cases.tsx
new file mode 100644
index 0000000000..4dd98dd6a5
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/cases.tsx
@@ -0,0 +1,119 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file cases.tsx
+ * @input A case id from caseIds.ts
+ * @output The case: an alert over a toast, or a dialog closed after a save
+ * that raised a toast
+ * @position The AlertDialog cases page's content. Each case is its Storybook
+ * story, on a sandbox page.
+ */
+
+import {useEffect, useRef, useState} from 'react';
+import {AlertDialog} from '@astryxdesign/core/AlertDialog';
+import {Button} from '@astryxdesign/core/Button';
+import {Dialog} from '@astryxdesign/core/Dialog';
+import {Stack} from '@astryxdesign/core/Stack';
+import {Text} from '@astryxdesign/core/Text';
+import {ToastViewport, useToast} from '@astryxdesign/core/Toast';
+import type {ToastDismissFn} from '@astryxdesign/core/Toast';
+import type {AlertDialogCaseId} from './caseIds';
+
+function AlertOverAToast() {
+ const [isOpen, setIsOpen] = useState(false);
+ const [undoCount, setUndoCount] = useState(0);
+ const toast = useToast();
+ // The case's one toast. A new delete replaces it.
+ const dismissRef = useRef<ToastDismissFn | null>(null);
+ // Synchronizes the toast system with the case: leaving the case dismisses
+ // its toast.
+ useEffect(() => () => dismissRef.current?.(), []);
+ return (
+ <Stack gap={2}>
+ <Button
+ label="Delete item"
+ variant="destructive"
+ onClick={() => setIsOpen(true)}
+ />
+ <Text>{`Undo taps: ${undoCount}`}</Text>
+ <AlertDialog
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ title="Delete item?"
+ description="This action cannot be undone. The item and all its data will be permanently removed."
+ actionLabel="Delete"
+ onAction={() => {
+ dismissRef.current = toast({
+ body: 'Item deleted.',
+ uniqueID: 'alert-over-a-toast',
+ isAutoHide: false,
+ endContent: (
+ <Button
+ label="Undo"
+ variant="secondary"
+ onClick={() => setUndoCount(count => count + 1)}
+ />
+ ),
+ });
+ setIsOpen(false);
+ }}
+ />
+ </Stack>
+ );
+}
+
+/** Stands in for a request that the app waits for before it closes. */
+async function saveItem(): Promise<void> {
+ await new Promise<void>(resolve => {
+ setTimeout(resolve, 300);
+ });
+}
+
+function SaveToastContent({onSaved}: {onSaved: () => void}) {
+ const toast = useToast();
+ const save = async () => {
+ toast({body: 'Saving the item.'});
+ await saveItem();
+ onSaved();
+ };
+ return (
+ <Stack gap={3}>
+ <Text>
+ Save shows a toast, then closes this dialog when the save is done.
+ </Text>
+ <Button
+ label="Save"
+ onClick={() => {
+ void save();
+ }}
+ />
+ </Stack>
+ );
+}
+
+function DialogClosedAfterSave() {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <ToastViewport>
+ <Stack gap={2}>
+ <Button label="Open dialog" onClick={() => setIsOpen(true)} />
+ <Dialog
+ isOpen={isOpen}
+ onOpenChange={() => setIsOpen(false)}
+ aria-label="Edit item">
+ <SaveToastContent onSaved={() => setIsOpen(false)} />
+ </Dialog>
+ </Stack>
+ </ToastViewport>
+ );
+}
+
+export function AlertDialogCase({id}: {id: AlertDialogCaseId}) {
+ return id === 'alert-over-a-toast' ? (
+ <AlertOverAToast />
+ ) : (
+ <DialogClosedAfterSave />
+ );
+}
diff --git a/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/page.tsx b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/page.tsx
new file mode 100644
index 0000000000..a8bfe2338f
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/alert-dialog-cases/page.tsx
@@ -0,0 +1,62 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file page.tsx
+ * @input `?case=` (one of the ids in caseIds.ts; the first by default)
+ * @output A bare page with one AlertDialog case above rows that make the
+ * document scroll
+ * @position A raw page, with no sandbox chrome, for checking the cases on a
+ * phone. The page scrolls with the document, as an app's page does.
+ */
+
+import {Suspense} from 'react';
+import {useSearchParams} from 'react-router-dom';
+import {HStack, VStack} from '@astryxdesign/core/Layout';
+import {Link} from '@astryxdesign/core/Link';
+import {Heading, Text} from '@astryxdesign/core/Text';
+import {AlertDialogCase} from './cases';
+import {
+ ALERT_DIALOG_CASE_LABELS,
+ ALERT_DIALOG_CASES,
+ parseAlertDialogCase,
+} from './caseIds';
+
+const ROWS = Array.from({length: 40}, (_, index) => `Row ${index + 1}`);
+
+function AlertDialogCases() {
+ const [searchParams] = useSearchParams();
+ const id = parseAlertDialogCase(searchParams.get('case'));
+ return (
+ <VStack gap={4} padding={4}>
+ <VStack gap={1}>
+ <Heading level={1}>AlertDialog cases</Heading>
+ <Text color="secondary">{ALERT_DIALOG_CASE_LABELS[id]}</Text>
+ <HStack gap={3}>
+ {ALERT_DIALOG_CASES.map(caseId => (
+ <Link key={caseId} href={`?case=${caseId}`}>
+ {caseId}
+ </Link>
+ ))}
+ </HStack>
+ </VStack>
+ <AlertDialogCase key={id} id={id} />
+ <VStack gap={2}>
+ {ROWS.map(row => (
+ <Text key={row} color="secondary">
+ {row}
+ </Text>
+ ))}
+ </VStack>
+ </VStack>
+ );
+}
+
+export default function AlertDialogCasesPage() {
+ return (
+ <Suspense>
+ <AlertDialogCases />
+ </Suspense>
+ );
+}
diff --git a/apps/sandbox/src/app/sandboxPages.ts b/apps/sandbox/src/app/sandboxPages.ts
index 3c2800f341..2d63fd5896 100644
--- a/apps/sandbox/src/app/sandboxPages.ts
+++ b/apps/sandbox/src/app/sandboxPages.ts
@@ -99,6 +99,12 @@ export const categories: SandboxCategory[] = [
description:
'A full page ChatLayout for phone checks. ?case= long (the default), composer or streaming scroll the document; docked scrolls a fixed-height panel',
},
+ {
+ name: 'AlertDialog Cases',
+ href: '/pages/alert-dialog-cases/',
+ description:
+ 'An alert over a toast and a dialog closed after a save, on a page that scrolls with the document, to check on a phone',
+ },
{
name: 'TextArea Counter',
href: '/pages/textarea-counter/',
diff --git a/apps/sandbox/src/hydration-policy.ts b/apps/sandbox/src/hydration-policy.ts
index 47609a6053..6b4e1e2fe8 100644
--- a/apps/sandbox/src/hydration-policy.ts
+++ b/apps/sandbox/src/hydration-policy.ts
@@ -5,7 +5,8 @@
* @input Route and URL search string.
* @output Whether React can safely hydrate instead of client-rendering.
* @position Prevents mismatches for iframe themes and for pages whose first
- * render reads URL controls (Shell Lab, Perf Scenes, Chat Layout).
+ * render reads URL controls (Shell Lab, Perf Scenes, Chat Layout,
+ * AlertDialog Cases).
*/
/** Pages whose first render reads the query string. */
@@ -13,6 +14,7 @@ const QUERY_RENDERED_PAGES = new Set([
'/pages/shell-lab/',
'/pages/perf-scenes/',
'/pages/chat-layout/',
+ '/pages/alert-dialog-cases/',
]);
export function canHydrate(route: string | undefined, search: string): boolean {
diff --git a/apps/sandbox/src/route-manifest.test.ts b/apps/sandbox/src/route-manifest.test.ts
index 89436679a5..aec001ca2e 100644
--- a/apps/sandbox/src/route-manifest.test.ts
+++ b/apps/sandbox/src/route-manifest.test.ts
@@ -114,6 +114,10 @@ describe('Sandbox static export contract', () => {
expect(canHydrate('/pages/chat-layout/', '')).toBe(true);
expect(canHydrate('/pages/perf-scenes/', '?scene=table')).toBe(false);
expect(canHydrate('/pages/perf-scenes/', '')).toBe(true);
+ expect(
+ canHydrate('/pages/alert-dialog-cases/', '?case=alert-over-a-toast'),
+ ).toBe(false);
+ expect(canHydrate('/pages/alert-dialog-cases/', '')).toBe(true);
});
it('does not turn unknown URLs, dynamic segments or traversal into a 200 fallback', () => {