Skip to content

fix(useMedia): share one MediaQueryList and change listener per query - #8777

Open
Ultroncn wants to merge 2 commits into
primer:mainfrom
Ultroncn:fix/use-media-shared-cache
Open

Ultroncn wants to merge 2 commits into
primer:mainfrom
Ultroncn:fix/use-media-shared-cache

Conversation

@Ultroncn

Copy link
Copy Markdown

Closes #8720

Changelog

New

None.

Changed

  • useMedia now shares one MediaQueryList and one change listener per distinct media query string instead of allocating a new list and registering a new listener for every hook instance. The cached match state is updated from the change event, and the entry is dropped (listener detached) when the last subscriber unmounts.
  • Added regression tests: two subscribers of the same query trigger a single matchMedia call and a single change listener, different queries keep separate lists, and unmounting the last subscriber detaches the listener so the next mount allocates a fresh list.

Removed

None.

Rollout strategy

  • No changes needed in github/github-ui
  • Needs changes in github/github-ui
  • Ships with feature flag(s)
  • Not sure
  • None

Testing & Reviewing

  • npx vitest run packages/react/src/hooks/__tests__/useMedia.test.tsx packages/react/src/hooks/__tests__/useResponsiveValue.test.tsx: 12 passed (7 useMedia, including the 2 new sharing tests, and 5 useResponsiveValue).
  • npx eslint packages/react/src/hooks/useMedia.ts packages/react/src/hooks/__tests__/useMedia.test.tsx: clean.
  • npx tsc --noEmit in packages/react: 17 errors, all pre-existing on unmodified main (duplicate @babel/types and missing @primer/doc-gen build artifacts in this environment); none in the changed files.

Notes for reviewers:

  • I did not take the optional useResponsiveValue half of useMedia, useResponsiveValue: share MediaQueryList instances and listeners #8720 (skipping its three useMedia calls for non-responsive values): hooks cannot be called conditionally, so that would need a restructure of the hook rather than this sharing change. Happy to follow up separately if maintainers want it.
  • The shared entry is keyed to the matchMedia implementation that created it, so replacing matchMedia (for example, a test mock) rebuilds the entry and carries live subscribers over, instead of serving a stale list.

Closes primer#8720

Each useMedia instance called window.matchMedia on every render and
registered its own change listener, so one closed ActionMenu held 9
live listeners across 3 distinct queries. Keep a module-level cache of
one MediaQueryList plus one shared change listener per query string,
with reference-counted subscribers; the cached match state is updated
from the change event, and the entry is dropped when the last
subscriber unmounts.
@Ultroncn
Ultroncn requested a review from a team as a code owner November 17, 2026 02:55
@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f87ef07

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@llastflowers llastflowers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we read the cached MediaQueryList.matches directly here? If a component suspends and the viewport changes before it subscribes, the cached boolean stays stale. I reproduced this by resizing from 1000px to 500px while suspended—the hook still returned true for (min-width: 800px) after loading. A regression test for that case would be helpful too.

@Ultroncn

Copy link
Copy Markdown
Author

useMedia now reads .matches directly from the shared MediaQueryList. I added a regression test where the match changes while the first subscriber is suspended. It failed before the fix and now passes in Chromium.

This branch had an error being deployed

1 failed deployment
github-pages — f87ef07a Deployed Nov 20, 2026 by Ultroncn via deploy-preview #30789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useMedia, useResponsiveValue: share MediaQueryList instances and listeners

2 participants