You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(web): keep styles used by elements outside the DOM - #1398
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.
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.
This change prevents web styles from being deleted while portal elements still use them. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
[#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
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1397.
UnistylesRegistry.removedeletes 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 newweb-registry-cleanup.spec.tscovers 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