An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): prevent SideNav preview navigation (#2827)
Cindy Zhang874c5a6a1380
diff --git a/apps/docsite/src/components/component-detail/ComponentDetailClient.tsx b/apps/docsite/src/components/component-detail/ComponentDetailClient.tsxindex 5e5c55cad8..d98c9b14a3 100644--- a/apps/docsite/src/components/component-detail/ComponentDetailClient.tsx+++ b/apps/docsite/src/components/component-detail/ComponentDetailClient.tsx@@ -104,7 +104,7 @@ function OverviewContent({</XDSVStack><XDSVStack gap={10}>{(exampleRegistry[comp.name] || []).map((entry, i) => (- <ExampleBlock key={i} entry={entry} />+ <ExampleBlock key={i} entry={entry} componentName={comp.name} />))}</XDSVStack></>diff --git a/apps/docsite/src/components/component-detail/ExampleBlock.tsx b/apps/docsite/src/components/component-detail/ExampleBlock.tsxindex f1e8e97ecd..e8daf88f63 100644--- a/apps/docsite/src/components/component-detail/ExampleBlock.tsx+++ b/apps/docsite/src/components/component-detail/ExampleBlock.tsx@@ -17,8 +17,15 @@ import {ComponentPreviewTheme} from './ComponentPreviewTheme';import {buildPlaygroundHref} from '../playgroundLink';import {trackOpenPlayground} from '../../lib/analytics';import {MarkdownText} from '../MarkdownText';+import {preventPreviewNavigation} from './previewNavigation';-function LivePreview({entry}: {entry: ExampleEntry}) {+function LivePreview({+ entry,+ componentName,+}: {+ entry: ExampleEntry;+ componentName: string;+}) {const [Component, setComponent] = useState<ComponentType | null>(null);const [error, setError] = useState(false);@@ -29,6 +36,11 @@ function LivePreview({entry}: {entry: ExampleEntry}) {.catch(() => setError(true));}, [entry]);+ const previewNavigationProps =+ componentName === 'SideNav'+ ? {onClickCapture: preventPreviewNavigation}+ : {};+if (error) {return (<XDSCenter style={{minHeight: 200, width: '100%'}}>@@ -56,7 +68,8 @@ function LivePreview({entry}: {entry: ExampleEntry}) {display: 'flex',alignItems: 'center',justifyContent: 'center',- }}>+ }}+ {...previewNavigationProps}><div style={{minWidth: 'fit-content', padding: 'var(--spacing-4)'}}><Component /></div>@@ -66,9 +79,10 @@ function LivePreview({entry}: {entry: ExampleEntry}) {interface ExampleBlockProps {entry: ExampleEntry;+ componentName: string;}-export function ExampleBlock({entry}: ExampleBlockProps) {+export function ExampleBlock({entry, componentName}: ExampleBlockProps) {const [tab, setTab] = useState<string>('description');return (@@ -78,7 +92,7 @@ export function ExampleBlock({entry}: ExampleBlockProps) {{entry.name}</XDSText>- <LivePreview entry={entry} />+ <LivePreview entry={entry} componentName={componentName} /><XDSSection variant="muted" padding={1} dividers={['top']}><XDSHStackdiff --git a/apps/docsite/src/components/component-detail/ShowcasePreview.tsx b/apps/docsite/src/components/component-detail/ShowcasePreview.tsxindex c499d1d9cc..ad1264922b 100644--- a/apps/docsite/src/components/component-detail/ShowcasePreview.tsx+++ b/apps/docsite/src/components/component-detail/ShowcasePreview.tsx@@ -8,6 +8,7 @@ import {XDSText} from '@xds/core/Text';import {XDSSpinner} from '@xds/core/Spinner';import {useMediaQuery} from '@xds/core/hooks';import {showcaseRegistry} from '../../generated/showcaseRegistry';+import {preventPreviewNavigation} from './previewNavigation';interface ShowcasePreviewProps {name: string;@@ -29,6 +30,9 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {.catch(() => setError(true));}, [name]);+ const previewNavigationProps =+ name === 'SideNav' ? {onClickCapture: preventPreviewNavigation} : {};+const placeholderStyle = isSmall? {minHeight: 160, width: '100%'}: {aspectRatio: '16 / 9', width: '100%'};@@ -61,7 +65,8 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {display: 'flex',alignItems: 'center',justifyContent: 'center',- }}>+ }}+ {...previewNavigationProps}><div style={{minWidth: 'fit-content'}}><Component /></div>@@ -78,7 +83,8 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {display: 'flex',alignItems: 'center',justifyContent: 'center',- }}>+ }}+ {...previewNavigationProps}><Component /></div>);diff --git a/apps/docsite/src/components/component-detail/previewNavigation.ts b/apps/docsite/src/components/component-detail/previewNavigation.tsnew file mode 100644index 0000000000..cc5b3bda84--- /dev/null+++ b/apps/docsite/src/components/component-detail/previewNavigation.ts@@ -0,0 +1,21 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import type {MouseEvent} from 'react';++/**+ * Prevents example preview links from navigating the docsite while preserving+ * the realistic hrefs shown in the source example.+ */+export function preventPreviewNavigation(event: MouseEvent<HTMLElement>) {+ const target = event.target;+ if (!(target instanceof Element)) {+ return;+ }++ const anchor = target.closest('a[href]');+ if (anchor == null) {+ return;+ }++ event.preventDefault();+}