Skip to content

fix(web): keep styles used by elements outside the DOM - #1398

Merged
jpudysz merged 1 commit into
jpudysz:mainfrom
lugg:eric/web-keep-styles-in-use
Nov 24, 2026
Merged

jpudysz merged 1 commit into
jpudysz:mainfrom
lugg:eric/web-keep-styles-in-use

Conversation

@erickreutz

@erickreutz erickreutz commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1397.

UnistylesRegistry.remove deletes a style's CSS rule once its last connected element is removed, unless an element with the class is in the document a microtask later. Elements connected in the same commit inside a portal whose container isn't attached to the document yet, such as map overlay content the map appends to its pane later, are missed by that DOM check. So their rule is deleted while they're on screen, until a later render of the style re-adds it.

The cleanup now also keeps the rule when an element has connected to the hash since it was dropped (this.stylesCounter.has(hash)). A connected element still uses the style whether or not it's in the document yet. The new web-registry-cleanup.spec.ts covers both paths: a rule with no elements left is still dropped, and a rule whose new element connected outside the document is kept.

Summary by CodeRabbit

  • Bug Fixes
    • Styles are now retained when another element still uses them, including elements outside the DOM. Unused styles are removed as expected.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The web registry cleanup now retains styles when their hash is reconnected before deferred cleanup. Tests cover removal of unused styles and retention when another element still uses the style.

Changes

Web registry cleanup

Layer / File(s) Summary
Deferred cleanup and tests
packages/unistyles/src/web/registry.ts, packages/unistyles/src/__tests__/web-registry-cleanup.spec.ts
Deferred cleanup checks whether the hash was reconnected or a matching DOM element exists before removing the CSS rule and cache entry. Tests verify removal of an unused style and retention when another element still uses it.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: jpudysz, brentlok

Merge Risk: Minimal · up to 5933e

This change prevents web styles from being deleted while portal elements still use them. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(web): keep styles used by elements outside the DOM" describes the main change. The registry change keeps a style hash when a matching element is connected but not yet attached to the do…
Linked Issues check Passed [#1397] The issue requires that UnistylesRegistry.remove keep a style rule when a replacement element connects to the same hash before the deferred cleanup runs, even if document.querySelector can…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes two files: the registry cleanup condition in packages/unistyles/src/web/registry.ts (a one-line behavior change plus comment updates) and the new spe…
Full details: Title check

Explanation

The title "fix(web): keep styles used by elements outside the DOM" describes the main change. The registry change keeps a style hash when a matching element is connected but not yet attached to the document, such as a portal container added later. The title names the web scope, the fix type, and the affected case. It is concise and specific, and it matches the linked issue and the test additions.

Full details: Linked Issues check

Explanation

[#1397] The issue requires that UnistylesRegistry.remove keep a style rule when a replacement element connects to the same hash before the deferred cleanup runs, even if document.querySelector cannot find that element. The diff changes the deferred check in packages/unistyles/src/web/registry.ts to this.stylesCounter.has(hash) || document.querySelector(...). connect creates or updates the Set for the hash synchronously, so a reconnect before the microtask makes has(hash) true and the rule and cache entry are kept. The rule is still removed when no element remains, because remove deletes the hash from stylesCounter before scheduling the check. The issue's requested regression test is added in web-registry-cleanup.spec.ts. The second case connects a new element after remove is called and before the microtask, and expects false, the rule kept, and add to report existingHash: true. The first case covers the drop path. I did not execute the jsdom tests, so the pass result rests on source reading.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes two files: the registry cleanup condition in packages/unistyles/src/web/registry.ts (a one-line behavior change plus comment updates) and the new spec packages/unistyles/src/__tests__/web-registry-cleanup.spec.ts. The spec directly tests the cleanup path that the change modifies. No unrelated files or behavior changes appear in the diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a40339be-e04f-4b67-ad58-2ac0a9845a84
Commits

Reviewing files that changed from the base of the PR and between 155a7a0 and 5933eca.

Files selected for processing: 2

  • packages/unistyles/src/__tests__/web-registry-cleanup.spec.ts
  • packages/unistyles/src/web/registry.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@erickreutz
erickreutz marked this pull request as ready for review November 24, 2026 17:49
@jpudysz
jpudysz merged commit 8aab43f into jpudysz:main Nov 24, 2026
3 checks passed
@jpudysz

jpudysz commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks!

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.

Web: CSS rules are deleted while elements in a not-yet-attached portal still use them

2 participants