Skip to content

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

TypeScript

fix(docsite): prevent stale template previews on opening and reopening (#6167)

Angadslr2982c61577f9
diff --git a/apps/docsite/src/__tests__/template-preview-dialog.test.ts b/apps/docsite/src/__tests__/template-preview-dialog.test.ts
new file mode 100644
index 0000000000..89288b932d
--- /dev/null
+++ b/apps/docsite/src/__tests__/template-preview-dialog.test.ts
@@ -0,0 +1,307 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+// @vitest-environment jsdom
+
+/**
+ * @file Template preview selection and transition regressions.
+ * @input Mounted TemplatePreviewDialog with real React scheduling and mocked header, content, and footer UI.
+ * @output Records every committed visible preview and verifies close stays responsive.
+ * @position Docsite regression coverage for gallery preview selection and navigation.
+ */
+
+import {
+ createElement,
+ Suspense,
+ startTransition,
+ useLayoutEffect,
+ useRef,
+ useState,
+ type ReactNode,
+} from 'react';
+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
+import {act, cleanup, fireEvent, render, screen} from '@testing-library/react';
+import {TemplatePreviewDialog} from '../components/TemplatePreviewDialog';
+
+vi.mock('@stylexjs/stylex', () => ({
+ create: (styles: unknown) => styles,
+ props: () => ({}),
+}));
+vi.mock('@astryxdesign/core/Icon', () => ({Icon: () => null}));
+vi.mock('@astryxdesign/core/Text', () => ({Text: Box, Heading: Box}));
+vi.mock('@astryxdesign/core/Code', () => ({Code: Box}));
+vi.mock('@astryxdesign/core/Tooltip', () => ({Tooltip: Box}));
+vi.mock('@astryxdesign/core/Layout', () => ({
+ VStack: Box,
+ HStack: Box,
+ LayoutHeader: Box,
+ LayoutContent: Box,
+ LayoutFooter: Box,
+ Layout: ({
+ header,
+ content,
+ footer,
+ }: {
+ header: ReactNode;
+ content: ReactNode;
+ footer: ReactNode;
+ }) => createElement('div', null, header, content, footer),
+}));
+vi.mock('@astryxdesign/core/Button', () => ({
+ Button: ({
+ label,
+ onClick,
+ href,
+ }: {
+ label: string;
+ onClick?: () => void;
+ href?: string;
+ }) =>
+ href
+ ? createElement('a', {href, onClick}, label)
+ : createElement('button', {onClick}, label),
+}));
+vi.mock('@astryxdesign/core/Skeleton', () => ({
+ Skeleton: () => createElement('div', {'data-testid': 'pending-preview'}),
+}));
+// Keep children mounted while closed without modeling native dialog teardown.
+// React's actual deferred-value and transition hooks are deliberately not mocked.
+vi.mock('@astryxdesign/core/Dialog', () => ({
+ Dialog: ({
+ isOpen,
+ children,
+ variant,
+ }: {
+ isOpen: boolean;
+ children: ReactNode;
+ variant?: string;
+ }) =>
+ createElement(
+ 'div',
+ {role: 'dialog', hidden: !isOpen, 'data-variant': variant},
+ children,
+ ),
+}));
+vi.mock('../lib/analytics', () => ({
+ trackCopy: vi.fn(),
+ trackOpenPlayground: vi.fn(),
+ trackNavigate: vi.fn(),
+}));
+vi.mock('../components/TemplatePreviewSurface', () => ({
+ TemplatePreviewSurface: PreviewProbe,
+}));
+
+const commits: {slug: string; covered: boolean}[] = [];
+const items = [
+ {slug: 'template-a', name: 'Template A'},
+ {slug: 'template-b', name: 'Template B'},
+ {slug: 'template-c', name: 'Template C'},
+];
+
+function Box({children}: {children?: ReactNode}) {
+ return createElement('div', null, children);
+}
+
+function PreviewProbe({slug}: {slug: string}) {
+ const ref = useRef<HTMLDivElement>(null);
+ // Observe commits, not speculative renders that React may discard. Every
+ // layout effect runs before RTL's act can flush the deferred follow-up.
+ useLayoutEffect(() => {
+ const dialog = ref.current?.closest<HTMLElement>('[role="dialog"]');
+ if (dialog && !dialog.hidden) {
+ commits.push({
+ slug,
+ covered:
+ dialog.querySelector('[data-testid="pending-preview"]') !== null,
+ });
+ }
+ });
+ return createElement('div', {ref, 'data-testid': 'preview'}, slug);
+}
+
+function preview(index: number, isOpen = true, variant?: 'fullscreen') {
+ return createElement(TemplatePreviewDialog, {
+ items,
+ index,
+ isOpen,
+ variant,
+ onOpenChange: vi.fn(),
+ onIndexChange: vi.fn(),
+ });
+}
+
+function expectOnlyVisible(slug: string) {
+ const visible = commits
+ .filter(commit => !commit.covered)
+ .map(commit => commit.slug);
+ expect(visible[0]).toBe(slug);
+ expect(new Set(visible)).toEqual(new Set([slug]));
+}
+
+beforeEach(() => {
+ commits.length = 0;
+});
+
+afterEach(cleanup);
+
+describe('template preview selection', () => {
+ it.each([undefined, 'fullscreen'] as const)(
+ 'opens the selected template immediately (variant: %s)',
+ variant => {
+ const view = render(preview(0, false, variant));
+ expect(commits).toEqual([]);
+ view.rerender(preview(1, true, variant));
+ expectOnlyVisible('template-b');
+ expect(
+ screen.getByTestId('preview').closest<HTMLElement>('[role="dialog"]')
+ ?.dataset.variant,
+ ).toBe(variant);
+ },
+ );
+
+ it('opens a different template after closing without unmounting', () => {
+ const view = render(preview(1));
+ view.rerender(preview(0, false));
+ commits.length = 0;
+ view.rerender(preview(2));
+ expectOnlyVisible('template-c');
+ });
+
+ it('immediately follows direct index updates from URL synchronization', () => {
+ const view = render(preview(0));
+ for (const index of [2, 1, 0]) {
+ commits.length = 0;
+ view.rerender(preview(index));
+ expectOnlyVisible(items[index].slug);
+ }
+ });
+
+ it('opens an initial deep-linked index', () => {
+ render(preview(2));
+ expectOnlyVisible('template-c');
+ });
+
+ it('navigates both directions and covers the old preview while pending', () => {
+ function Gallery() {
+ const [index, setIndex] = useState(0);
+ return createElement(TemplatePreviewDialog, {
+ items,
+ index,
+ isOpen: true,
+ onIndexChange: setIndex,
+ onOpenChange: vi.fn(),
+ });
+ }
+ render(createElement(Gallery));
+ for (const [label, oldSlug, nextSlug] of [
+ ['Next template', 'template-a', 'template-b'],
+ ['Previous template', 'template-b', 'template-a'],
+ ['Previous template', 'template-a', 'template-c'],
+ ]) {
+ commits.length = 0;
+ fireEvent.click(screen.getByRole('button', {name: label}));
+ expect(commits).toContainEqual({slug: oldSlug, covered: true});
+ expectOnlyVisible(nextSlug);
+ expect(screen.queryByTestId('pending-preview')).toBeNull();
+ }
+ });
+
+ it('keeps close responsive during a suspended navigation', async () => {
+ let resolve!: () => void;
+ let ready = false;
+ const navigation = new Promise<void>(done => {
+ resolve = done;
+ });
+ function Navigation({index}: {index: number}) {
+ if (index === 1 && !ready) {
+ throw navigation;
+ }
+ return null;
+ }
+ function Gallery() {
+ const [index, setIndex] = useState(0);
+ const [isOpen, setOpen] = useState(true);
+ return createElement(
+ Suspense,
+ {fallback: 'Navigating'},
+ createElement(TemplatePreviewDialog, {
+ items,
+ index,
+ isOpen,
+ onIndexChange: setIndex,
+ onOpenChange: setOpen,
+ }),
+ createElement(Navigation, {index}),
+ );
+ }
+ render(createElement(Gallery));
+ fireEvent.click(screen.getByRole('button', {name: 'Next template'}));
+ expect(screen.getByTestId('pending-preview')).not.toBeNull();
+ expect(screen.getByTestId('preview').textContent).toBe('template-a');
+ fireEvent.click(screen.getByRole('button', {name: 'Close preview'}));
+ expect(screen.getByRole('dialog', {hidden: true}).hidden).toBe(true);
+ expect(screen.queryByTestId('preview')).toBeNull();
+ await act(async () => {
+ ready = true;
+ resolve();
+ await navigation;
+ });
+ expect(screen.getByRole('dialog', {hidden: true}).hidden).toBe(true);
+ expect(screen.queryByTestId('preview')).toBeNull();
+ });
+
+ it('opens C after closing a pending A-to-B navigation', async () => {
+ let resolve!: () => void;
+ let ready = false;
+ const navigation = new Promise<void>(done => {
+ resolve = done;
+ });
+ function Navigation({index}: {index: number | null}) {
+ if (index !== 0 && !ready) {
+ throw navigation;
+ }
+ return null;
+ }
+ function Gallery() {
+ const [selection, setSelection] = useState<number | null>(0);
+ const [routeSelection, setRouteSelection] = useState<number | null>(0);
+ // Match the gallery: selection updates immediately, while router.replace
+ // schedules separate transition work that can remain suspended.
+ const select = (index: number | null) => {
+ setSelection(index);
+ startTransition(() => setRouteSelection(index));
+ };
+ return createElement(
+ Suspense,
+ {fallback: 'Navigating'},
+ createElement('button', {onClick: () => select(2)}, 'Open C'),
+ createElement(TemplatePreviewDialog, {
+ items,
+ index: selection ?? 0,
+ isOpen: selection !== null,
+ onIndexChange: select,
+ onOpenChange: (open: boolean) => {
+ if (!open) {
+ select(null);
+ }
+ },
+ }),
+ createElement(Navigation, {index: routeSelection}),
+ );
+ }
+ render(createElement(Gallery));
+ fireEvent.click(screen.getByRole('button', {name: 'Next template'}));
+ expect(screen.getByTestId('pending-preview')).not.toBeNull();
+ fireEvent.click(screen.getByRole('button', {name: 'Close preview'}));
+ commits.length = 0;
+ fireEvent.click(screen.getByRole('button', {name: 'Open C'}));
+ expectOnlyVisible('template-c');
+ expect(commits.every(commit => commit.slug === 'template-c')).toBe(true);
+ expect(screen.queryByTestId('pending-preview')).toBeNull();
+ await act(async () => {
+ ready = true;
+ resolve();
+ await navigation;
+ });
+ expectOnlyVisible('template-c');
+ });
+});
diff --git a/apps/docsite/src/components/TemplatePreviewDialog.tsx b/apps/docsite/src/components/TemplatePreviewDialog.tsx
index cba223c992..21da10caa7 100644
--- a/apps/docsite/src/components/TemplatePreviewDialog.tsx
+++ b/apps/docsite/src/components/TemplatePreviewDialog.tsx
@@ -9,7 +9,8 @@
* display order. Arrow keys (←/→) also navigate; Escape closes.
*
* @input Template metadata, selected index, open state, and navigation callbacks.
- * @output A responsive dialog with a fill-mode live preview up to 1440×900 and template actions.
+ * @output A responsive dialog with immediate selected fill-mode previews up to
+ * 1440×900 while open, selection-scoped pending navigation, and template actions.
* @position Shared preview controller for the templates gallery.
*
* The header surfaces template metadata (name, description) with the close
@@ -296,6 +297,8 @@ export function TemplatePreviewDialog({
}: TemplatePreviewDialogProps) {
const [copiedCommand, setCopiedCommand] = useState<CopiedCommand>(null);
const [isPending, startTransition] = useTransition();
+ const [pendingPreviewItem, setPendingPreviewItem] =
+ useState<TemplatePreviewItem | null>(null);
// Release the top layer when this dialog is torn down while still open.
//
@@ -323,11 +326,22 @@ export function TemplatePreviewDialog({
const count = items.length;
const current = items[index];
- // The deferred index drives the heavy preview surface — it lags behind
- // the committed index during a transition, keeping the old template
- // visible and the dialog interactive while the next one loads.
+ // Router work may outlive closing or selecting another card. Retire its
+ // preview state during render so it cannot cover a newer selection.
+ if (
+ pendingPreviewItem !== null &&
+ (!isOpen || pendingPreviewItem !== current)
+ ) {
+ setPendingPreviewItem(null);
+ }
+ const isPreviewPending =
+ isPending && isOpen && pendingPreviewItem === current;
+ // Only pending prev/next navigation may retain the deferred preview beneath
+ // its skeleton. Opening a card or syncing a URL must show the current item
+ // immediately, even if the mounted dialog's deferred index is still stale.
const deferredIndex = useDeferredValue(index);
const deferredCurrent = items[deferredIndex];
+ const previewCurrent = isPreviewPending ? deferredCurrent : current;
const go = (delta: number) => {
if (count === 0) {
@@ -340,6 +354,7 @@ export function TemplatePreviewDialog({
direction: delta > 0 ? 'next' : 'prev',
item: items[nextIndex]?.slug,
});
+ setPendingPreviewItem(current);
startTransition(() => {
onIndexChange(nextIndex);
});
@@ -412,11 +427,11 @@ export function TemplatePreviewDialog({
<div {...stylex.props(styles.body)} ref={hostRef}>
{isOpen && (
<TemplatePreviewSurface
- key={deferredCurrent.slug}
- slug={deferredCurrent.slug}
+ key={previewCurrent.slug}
+ slug={previewCurrent.slug}
/>
)}
- {isPending && (
+ {isPreviewPending && (
<div {...stylex.props(styles.skeletonOverlay)}>
<Skeleton width="100%" height="100%" />
</div>