Skip to content

feat(headless): hold Popover contents while it closes - #9310

Merged
alexcarpenter merged 1 commit into
carp/account-button-switcherfrom
carp/popover-freeze-on-close
Jul 31, 2026
Merged

feat(headless): hold Popover contents while it closes#9310
alexcarpenter merged 1 commit into
carp/account-button-switcherfrom
carp/popover-freeze-on-close

Conversation

@alexcarpenter

@alexcarpenter alexcarpenter commented Jul 31, 2026

Copy link
Copy Markdown
Member

Description

Stacked on #9184 (carp/account-button-switcher).

A popover's popup outlives open by the length of its exit animation, and whatever closed it has usually changed the data behind it — switching account or organization from the UserButton closes the menu and you watch its contents swap while it animates out.

Popover.Popup now wraps its children in a new Freeze utility (@clerk/headless/utils): a Suspense boundary plus a sibling that throws a never-settling promise, so React keeps rendering the subtree but holds the commit. The DOM keeps painting its last frame until frozen flips back off, at which point the pending render commits. The popup element itself is outside the freeze, so data-closed / data-ending-style still land and the exit animation is unchanged.

The technique is from https://barvian.me/react-exit-animations, adapted for React 18, which this package still supports (react: ^18.0.0 || ~19.0.3 || …):

  • React.use(infinitePromise)throw never.
  • React 19.2 Fragment Refs → a display: contents wrapper the component owns. React hides a suspended boundary's host children with display: none !important; an insertion effect restores display: contents on the same commit, before paint. React also detaches refs of hidden children, so the wrapper's node is captured through a ref callback that ignores null rather than read back from a plain ref.

Trade-offs, both documented in the popover README:

  • Every popover gains a display: contents element between the popup and its content.
  • Refs inside popup content are null while the popover is closing, until it reopens.

Freeze is unit tested. The popover wiring is not covered end to end: happy-dom has no animations, so mounted flips to false immediately on close and there is no exit window to assert against.

BEFORE

before.mov

AFTER

after.mov

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

The popup outlives `open` by the length of its exit animation, and whatever
closed it has usually changed the data behind it, so the contents swapped under
the animation. Wrap the popup's children in a new `Freeze` utility: a Suspense
boundary plus a child that throws a never-settling promise, so React keeps
rendering the subtree but holds the commit until the popover reopens. The popup
element itself stays live, so `data-closed` / `data-ending-style` still land.
@vercel

vercel Bot commented Jul 31, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview Jul 31, 2026 8:05pm
swingset Ready Ready Preview Jul 31, 2026 8:05pm

Request Review

@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5d76bdc

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

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

@austincalvelage austincalvelage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Such a clever trick

@alexcarpenter
alexcarpenter merged commit 9656849 into carp/account-button-switcher Jul 31, 2026
13 checks passed
@alexcarpenter
alexcarpenter deleted the carp/popover-freeze-on-close branch July 31, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants