An open source design system that's fully customizable and agent ready
TypeScript
fix(core): Overlay, AppShell and MobileNav work with no matchMedia (#10327)
Christopher Chedeaua9c911421bd6
diff --git a/.changeset/media-queries-without-matchmedia.md b/.changeset/media-queries-without-matchmedia.mdnew file mode 100644index 0000000000..cf8c502d0c--- /dev/null+++ b/.changeset/media-queries-without-matchmedia.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Overlay, AppShell and MobileNav work in a DOM with no `matchMedia`, such as an app's jsdom test. Before, Overlay threw on its first render, AppShell as it mounted (with React 19.2 or later), and MobileNav as it closed. Where `matchMedia` exists, nothing changes.++@vjeuxdiff --git a/packages/core/src/AppShell/AppShell.tsx b/packages/core/src/AppShell/AppShell.tsxindex 2fbff087f5..747a085504 100644--- a/packages/core/src/AppShell/AppShell.tsx+++ b/packages/core/src/AppShell/AppShell.tsx@@ -79,6 +79,7 @@ import {focusOutlineStyles,} from '../utils/focusOutline.stylex';import {useMediaQuery} from '../hooks/useMediaQuery';+import {matchesMediaQuery} from '../utils/coarsePointer';import {observeResize} from '../utils/sharedResizeObserver';import {themeProps} from '../utils/themeProps';import {useThemeDefinition} from '../theme/useTheme';@@ -653,8 +654,10 @@ export function AppShell({if (!HasActivity || isMobileNavOpen) {return;}- const delayMs = window.matchMedia('(prefers-reduced-motion: reduce)')- .matches+ const delayMs = matchesMediaQuery(+ window,+ '(prefers-reduced-motion: reduce)',+ )? 0: MOBILE_NAV_MAX_CLOSE_DELAY_MS + ACTIVITY_HIDE_BUFFER_MS;const timeout = setTimeout(() => {diff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsxindex 897895d05c..884490ef58 100644--- a/packages/core/src/MobileNav/MobileNav.tsx+++ b/packages/core/src/MobileNav/MobileNav.tsx@@ -77,6 +77,7 @@ import {ModalAnnounceProvider} from '../hooks/ModalAnnounceProvider';import {useLayerDismissal} from '../Layer/useLayerDismissal';import type {BaseProps} from '../BaseProps';import {themeProps} from '../utils/themeProps';+import {matchesMediaQuery} from '../utils/coarsePointer';import {useMergedRefs} from '../hooks/useMergedRefs';import {useScrollLock} from '../hooks/useScrollLock';@@ -341,7 +342,10 @@ function resolveCloseDelay(dialog: HTMLDialogElement): number {// Reduced motion makes the close sooner; it must not make the hold shorter.// Shrinking both leaves no slack — one slow frame between the commit and this// macrotask and the drawer has already stopped being rendered.- const cap = window.matchMedia('(prefers-reduced-motion: reduce)').matches+ const cap = matchesMediaQuery(+ dialog.ownerDocument.defaultView,+ '(prefers-reduced-motion: reduce)',+ )? 0: MAX_CLOSE_DELAY_MS;diff --git a/packages/core/src/Overlay/useOverlay.tsx b/packages/core/src/Overlay/useOverlay.tsxindex 7fdeb597ef..25deb478c1 100644--- a/packages/core/src/Overlay/useOverlay.tsx+++ b/packages/core/src/Overlay/useOverlay.tsx@@ -28,6 +28,7 @@ import {} from 'react';import * as stylex from '@stylexjs/stylex';import {useClickableContainer} from '../hooks/useClickableContainer';+import {matchesMediaQuery, watchMediaQuery} from '../utils/coarsePointer';import {overlayScope, overlayContainerStyles} from './overlay.markers.stylex';import {OverlayScrim} from './OverlayScrim';import type {@@ -45,16 +46,14 @@ function getIsTouchDevice(): boolean {if (typeof window === 'undefined') {return false;}- return window.matchMedia('(hover: none)').matches;+ return matchesMediaQuery(window, '(hover: none)');}function subscribeToHoverChange(callback: () => void): () => void {if (typeof window === 'undefined') {return () => {};}- const mql = window.matchMedia('(hover: none)');- mql.addEventListener('change', callback);- return () => mql.removeEventListener('change', callback);+ return watchMediaQuery(window, '(hover: none)', callback);}function getServerSnapshot(): boolean {diff --git a/packages/core/src/__tests__/withoutMatchMedia.test.tsx b/packages/core/src/__tests__/withoutMatchMedia.test.tsxnew file mode 100644index 0000000000..b45f03e677--- /dev/null+++ b/packages/core/src/__tests__/withoutMatchMedia.test.tsx@@ -0,0 +1,90 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file withoutMatchMedia.test.tsx+ * @input vitest, React Testing Library, Overlay, AppShell and MobileNav, and+ * a window with no matchMedia, as in a plain test DOM+ * @output Overlay renders, AppShell mounts and MobileNav closes where the+ * window has no matchMedia, and none of them throws+ * @position Unit tests of the media-query reads in Overlay/useOverlay.tsx,+ * AppShell/AppShell.tsx and MobileNav/MobileNav.tsx+ */++import {act, render, screen} from '@testing-library/react';+import {+ afterEach,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from 'vitest';+import {AppShell} from '../AppShell/AppShell';+import {MobileNav} from '../MobileNav/MobileNav';+import {Overlay} from '../Overlay/Overlay';++// jsdom has no modal dialog: MobileNav opens and closes one.+beforeAll(() => {+ HTMLDialogElement.prototype.showModal =+ HTMLDialogElement.prototype.showModal ||+ function (this: HTMLDialogElement) {+ this.setAttribute('open', '');+ };+ HTMLDialogElement.prototype.close =+ HTMLDialogElement.prototype.close ||+ function (this: HTMLDialogElement) {+ this.removeAttribute('open');+ };+});++beforeEach(() => {+ // The shared test setup gives jsdom a matchMedia. An app's test DOM may+ // have none.+ vi.stubGlobal('matchMedia', undefined);+});++afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+});++function Nav({isOpen}: {isOpen: boolean}) {+ return (+ <MobileNav isOpen={isOpen} onOpenChange={() => {}} data-testid="nav">+ <span>Nav content</span>+ </MobileNav>+ );+}++describe('a window with no matchMedia', () => {+ it('renders an Overlay over its content', () => {+ render(+ <Overlay content={<span>badge</span>}>+ <img alt="hero" src="x.jpg" />+ </Overlay>,+ );+ expect(screen.getByAltText('hero')).toBeInTheDocument();+ });++ it('mounts an AppShell with its content', () => {+ render(+ <AppShell topNav={<div>Nav</div>}>+ <div>Content</div>+ </AppShell>,+ );+ expect(screen.getByText('Content')).toBeInTheDocument();+ });++ it('closes an open MobileNav after its slide-out', () => {+ vi.useFakeTimers();+ const {rerender} = render(<Nav isOpen />);+ const dialog = screen.getByTestId('nav');+ expect(dialog).toHaveAttribute('open');+ rerender(<Nav isOpen={false} />);+ act(() => {+ vi.advanceTimersByTime(300);+ });+ expect(dialog).not.toHaveAttribute('open');+ });+});