From d5885738daa274583facc9fcc24e1042e6f07efd Mon Sep 17 00:00:00 2001 From: Milind Chatrabhuji Date: Fri, 27 Nov 2026 12:58:48 +0530 Subject: [PATCH 1/2] fix(docs): defer runtime clock live previews until client hydration --- .../mdx-components/react-live/index.tsx | 35 ++++- .../react-live/react-live.test.tsx | 145 ++++++++++++++++++ 2 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 design-system/docs/components/mdx-components/react-live/react-live.test.tsx diff --git a/design-system/docs/components/mdx-components/react-live/index.tsx b/design-system/docs/components/mdx-components/react-live/index.tsx index cd0a47c43..dd2b705fd 100644 --- a/design-system/docs/components/mdx-components/react-live/index.tsx +++ b/design-system/docs/components/mdx-components/react-live/index.tsx @@ -1,7 +1,14 @@ 'use client'; import copy from 'clipboard-copy'; import { createUrl } from 'playroom/utils'; -import { ReactNode, useEffect, useId, useState, type JSX } from 'react'; +import { + ReactNode, + useEffect, + useId, + useState, + useSyncExternalStore, + type JSX, +} from 'react'; import { ActionButton } from '@keystar/ui/button'; import { chevronDownIcon } from '@keystar/ui/icon/icons/chevronDownIcon'; @@ -142,10 +149,24 @@ function RenderedCodeExample({ children }: { children: ReactNode }) { ); } +const emptySubscribe = () => () => {}; +// Defer live previews that depend on runtime clock/timezone until after hydration +// to avoid SSR/client text mismatches on statically generated docs pages. +const RUNTIME_CLOCK_OR_TIMEZONE_PATTERN = + /\b(?:today|now|getLocalTimeZone)\s*\(|\bnew\s+Date\b|\bDate\.now\s*\(/; + export function ReactLive(props: { content: string; live: LiveCodeInfo; }): JSX.Element { + const isClient = useSyncExternalStore( + emptySubscribe, + () => true, + () => false + ); + const shouldRenderLiveCode = + isClient || !RUNTIME_CLOCK_OR_TIMEZONE_PATTERN.test(props.live.code); + return ( - + {shouldRenderLiveCode ? ( + + ) : null} { + const internationalizedDate = await import('@internationalized/date'); + const { DatePicker } = await import('@keystar/ui/date-time'); + const { Flex } = await import('@keystar/ui/layout'); + + return { + scope: { + DatePicker, + Flex, + ...internationalizedDate, + }, + }; +}); + +type LiveProp = ComponentProps['live']; + +function makeLive(code: string): LiveProp { + return { + code, + exampleType: 'jsx', + location: 'date-picker.mdoc', + }; +} + +describe('ReactLive hydration', () => { + beforeEach(() => { + vi.useFakeTimers(); + if (!window.matchMedia) { + Object.defineProperty(window, 'matchMedia', { + writable: true, + value: vi.fn().mockImplementation((query: string) => ({ + matches: query.includes('min-width'), + media: query, + onchange: null, + addListener: vi.fn(), + removeListener: vi.fn(), + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + })), + }); + } + if (!globalThis.ResizeObserver) { + globalThis.ResizeObserver = class { + observe() {} + unobserve() {} + disconnect() {} + }; + } + if (!globalThis.IntersectionObserver) { + globalThis.IntersectionObserver = class { + root = null; + rootMargin = ''; + thresholds = []; + observe() {} + unobserve() {} + disconnect() {} + takeRecords() { + return []; + } + }; + } + }); + + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + }); + + it('hydrates runtime-clock previews without mismatches when SSR and client dates differ', async () => { + const content = ``; + const live = makeLive( + `return React.createElement(DatePicker, { label: "Date", value: today(getLocalTimeZone()), isReadOnly: true });` + ); + + vi.setSystemTime(new Date('2026-06-01T12:00:00Z')); + const serverHtml = renderToString( + + + + ); + + vi.setSystemTime(new Date('2026-07-19T12:00:00Z')); + const container = document.createElement('div'); + container.innerHTML = serverHtml; + document.body.appendChild(container); + + const recoverableErrors: unknown[] = []; + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + + let root: ReturnType; + await act(async () => { + root = hydrateRoot( + container, + + + , + { + onRecoverableError(err: unknown) { + recoverableErrors.push(err); + }, + } + ); + }); + + expect(recoverableErrors).toHaveLength(0); + expect(errorSpy).not.toHaveBeenCalled(); + + const spinbuttons = Array.from( + container.querySelectorAll('[role="spinbutton"]') + ).map(el => el.getAttribute('aria-valuenow')); + expect(spinbuttons).toEqual(['7', '19', '2026']); + + await act(async () => { + root!.unmount(); + }); + container.remove(); + }); + + it('continues server-rendering deterministic live previews', () => { + const content = ``; + const live = makeLive( + `return React.createElement(DatePicker, { label: "Static", defaultValue: parseDate("2023-04-14") });` + ); + + const serverHtml = renderToString( + + + + ); + + expect(serverHtml).toContain('2023'); + expect(serverHtml).toContain('Static'); + }); +}); From 85a3d49f4ea1dd3581708c93d99b842345cd6a5d Mon Sep 17 00:00:00 2001 From: Milind Chatrabhuji Date: Fri, 27 Nov 2026 13:06:04 +0530 Subject: [PATCH 2/2] test(docs): restrict fake timers to Date in react-live hydration test --- .../components/mdx-components/react-live/react-live.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-system/docs/components/mdx-components/react-live/react-live.test.tsx b/design-system/docs/components/mdx-components/react-live/react-live.test.tsx index 3eb71cc36..83fcd6de3 100644 --- a/design-system/docs/components/mdx-components/react-live/react-live.test.tsx +++ b/design-system/docs/components/mdx-components/react-live/react-live.test.tsx @@ -34,7 +34,7 @@ function makeLive(code: string): LiveProp { describe('ReactLive hydration', () => { beforeEach(() => { - vi.useFakeTimers(); + vi.useFakeTimers({ toFake: ['Date'] }); if (!window.matchMedia) { Object.defineProperty(window, 'matchMedia', { writable: true,