diff --git a/src/components/charts/ChartsCatalogDocExample.client.tsx b/src/components/charts/ChartsCatalogDocExample.client.tsx index a96327b4c..c4d903e64 100644 --- a/src/components/charts/ChartsCatalogDocExample.client.tsx +++ b/src/components/charts/ChartsCatalogDocExample.client.tsx @@ -13,23 +13,25 @@ const LazyExampleWorkbench = React.lazy(() => export function ChartsCatalogDocExampleClient({ caseId, + edit, fallback, height, source, title, }: { caseId: string + edit: boolean fallback: React.ReactNode height: number source: 'hidden' | 'collapsed' | 'expanded' title?: string }) { const [definition, setDefinition] = React.useState() - const [editing, setEditing] = React.useState(source === 'expanded') + const [editing, setEditing] = React.useState(edit) React.useEffect(() => { - setEditing(source === 'expanded') - }, [caseId, source]) + setEditing(edit) + }, [caseId, edit]) React.useEffect(() => { let cancelled = false diff --git a/src/components/charts/ChartsCatalogDocExample.tsx b/src/components/charts/ChartsCatalogDocExample.tsx index 708993580..3937bfce5 100644 --- a/src/components/charts/ChartsCatalogDocExample.tsx +++ b/src/components/charts/ChartsCatalogDocExample.tsx @@ -1,6 +1,7 @@ import { ClientOnly } from '@tanstack/react-router' import * as React from 'react' import { ChartsCatalogPreview } from './ChartsCatalogPreview' +import { Button } from '~/components/ds/ui' const LazyChartsCatalogDocExample = React.lazy(() => import('./ChartsCatalogDocExample.client').then((module) => ({ @@ -19,45 +20,30 @@ export function ChartsCatalogDocExample({ source?: 'hidden' | 'collapsed' | 'expanded' title?: string }) { - const containerRef = React.useRef(null) - const [shouldLoad, setShouldLoad] = React.useState(false) + const [activation, setActivation] = React.useState<{ + caseId: string + edit: boolean + }>() + const isActive = activation?.caseId === caseId const fallback = ( setActivation({ caseId, edit: true })} + onRun={() => setActivation({ caseId, edit: false })} source={source} title={title} /> ) - React.useEffect(() => { - const container = containerRef.current - if (shouldLoad || !container) return - - if (!('IntersectionObserver' in window)) { - setShouldLoad(true) - return - } - - const observer = new IntersectionObserver( - (entries) => { - if (!entries.some((entry) => entry.isIntersecting)) return - setShouldLoad(true) - observer.disconnect() - }, - { rootMargin: '320px 0px' }, - ) - observer.observe(container) - return () => observer.disconnect() - }, [shouldLoad]) - return ( -
- {shouldLoad ? ( +
+ {isActive ? ( void + onRun: () => void source: 'hidden' | 'collapsed' | 'expanded' title?: string }) { @@ -90,19 +80,25 @@ function ChartsCatalogDocExampleFallback({ className="overflow-hidden rounded-lg border border-border-default bg-background-default" data-chart-example={caseId} > - {source !== 'hidden' ? ( -
+
+ {source !== 'hidden' ? ( {label} - - Open example - + ) : null} +
+ {source !== 'hidden' ? ( + + ) : null} +
- ) : null} +
diff --git a/tests/charts-catalog-frame-embedding.test.ts b/tests/charts-catalog-frame-embedding.test.ts index b791f30fd..193479786 100644 --- a/tests/charts-catalog-frame-embedding.test.ts +++ b/tests/charts-catalog-frame-embedding.test.ts @@ -163,7 +163,8 @@ test('chart example comments retain a useful static document', () => { assert.match(html, /data-chart-example="01-line-gaps"/) assert.match(html, /data-catalog-preview-case="01-line-gaps"/) assert.match(html, /height:480px/) - assert.match(html, /Open example/) + assert.match(html, />EditRun