Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: simplify adopted stylesheets controller #441

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

shaal
Copy link
Contributor

@shaal shaal commented Mar 12, 2024

Summary by CodeRabbit

  • Refactor
    • Renamed AdoptedStylesheets class to AdoptedStyleSheets and updated its properties and methods for improved management of global and encapsulated CSS styles in Lit elements.

@shaal shaal self-assigned this Mar 12, 2024
Copy link

changeset-bot bot commented Mar 12, 2024

⚠️ No Changeset found

Latest commit: 68f2aa8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

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

Click here to learn what changesets are, and how to add one.

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

Copy link

coderabbitai bot commented Mar 12, 2024

Walkthrough

The recent update involves a significant renaming and restructuring of the AdoptedStylesheets class, now known as AdoptedStyleSheets. This change enhances the management of both global and encapsulated CSS styles within Lit elements. The updated class introduces refined logic that adeptly applies styles to either the light DOM or shadow DOM, based on the CSS strings provided. This overhaul simplifies the process of handling different types of CSS, making style management more intuitive and efficient.

Changes

File Path Change Summary
packages/.../adopted-stylesheets.ts Renamed AdoptedStylesheets to AdoptedStyleSheets. Updated property names and method signatures. Refactored logic for applying global and encapsulated CSS styles to light or shadow DOM.

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

netlify bot commented Mar 12, 2024

Deploy Preview for outlinejs failed.

Name Link
🔨 Latest commit 68f2aa8
🔍 Latest deploy log https://app.netlify.com/sites/outlinejs/deploys/65f0627b1823c80009f8dbb9

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b6bf0f1 and 68f2aa8.
Files selected for processing (1)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Additional comments: 2
packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (2)
  • 8-12: The class AdoptedStyleSheets is well-documented, clearly stating its purpose and functionality. However, consider adding more detail to the documentation of the css property to explain the distinction between globalCSS and encapsulatedCSS more clearly. This will enhance the readability and maintainability of the code.
  • 74-80: The hostDisconnected callback currently takes no action. While this might be appropriate given the current implementation, consider if there's a need to clean up or remove applied stylesheets from the light DOM when the last instance of a component is removed from the document. This could help prevent potential memory leaks or style conflicts.

Comment on lines +14 to +15
// A set to track the stylesheets applied to the light DOM.
private static appliedLightDomStylesheets: Set<string> = new Set();
Copy link

Choose a reason for hiding this comment

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

Using a static property appliedLightDomStylesheets to track stylesheets applied to the light DOM is a good approach to prevent duplicate styles from being applied. However, ensure that this does not lead to memory leaks by holding references to styles that are no longer needed. Consider implementing a cleanup mechanism if necessary.

Comment on lines +39 to +55
private applyCssToDom(cssText: string, target: Document | ShadowRoot) {
if (target instanceof Document) {
const store = AdoptedStyleSheets.appliedLightDomStylesheets;

if (store.has(cssText)) {
// If the stylesheet has already been applied, no further action is required.
return;
}
store.add(cssText); // Store the stylesheet with the provided key.
}
this.adoptedSheet =
AdoptedStylesheets.styleSheetMap.get(cssText) || new CSSStyleSheet();

// Create a new stylesheet and replace its contents with the provided CSS text.
const sheet = new CSSStyleSheet();
sheet.replaceSync(cssText);

// Apply the stylesheet to the target's adoptedStyleSheets.
target.adoptedStyleSheets = [...target.adoptedStyleSheets, sheet];
Copy link

Choose a reason for hiding this comment

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

The method applyCssToDom is well-implemented, with clear separation of logic for applying styles to the Document and ShadowRoot. However, using CSSStyleSheet and adoptedStyleSheets requires the Shadow DOM to be enabled and might not be supported in all browsers. Ensure that there's a fallback mechanism or a polyfill for browsers that do not support these features.

Comment on lines +63 to +69
if (this.css.globalCSS) {
this.applyCssToDom(this.css.globalCSS, document); // Apply global CSS to the document if it exists.
}

// Apply encapsulated CSS to the host's shadow root if it exists.
if (this.css.encapsulatedCSS && this.host.shadowRoot) {
this.applyCssToDom(this.css.encapsulatedCSS, this.host.shadowRoot); // Apply encapsulated CSS to the host's shadow root if it exists.
Copy link

Choose a reason for hiding this comment

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

The hostConnected lifecycle callback efficiently applies global and encapsulated CSS to the appropriate DOM targets. This implementation aligns with the PR's objective to improve the management of CSS styles. Ensure that the CSS strings are sanitized or validated to prevent potential security issues like CSS injection.

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.

None yet

1 participant