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

AdoptedStyleSheets Update: Attach styles to document or this.shadowRoot #431

Merged
merged 10 commits into from
Feb 16, 2024

Conversation

himerus
Copy link
Contributor

@himerus himerus commented Jan 18, 2024

Summary by CodeRabbit

  • New Features
    • Enhanced style management efficiency by introducing a method for attaching encapsulated stylesheets to web components.
  • Refactor
    • Updated the AdoptedStylesheets controller for improved handling of CSSStyleSheet objects and connection lifecycle.
  • Documentation
    • Added documentation for the new createRenderRoot method in the AdoptedStylesheets controller.

Copy link

changeset-bot bot commented Jan 18, 2024

⚠️ No Changeset found

Latest commit: d5637cd

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

netlify bot commented Jan 18, 2024

Deploy Preview for outlinejs failed.

Name Link
🔨 Latest commit d5637cd
🔍 Latest deploy log https://app.netlify.com/sites/outlinejs/deploys/65ce629f706a9b0008990919

Copy link

coderabbitai bot commented Jan 18, 2024

Important

Auto Review Skipped

Review was skipped due to path filters

Files ignored due to path filters (1)
  • packages/controllers/adopted-stylesheets/package.json is excluded by: !**/*.json

Walkthrough

The update introduces a refined approach to managing styles within web components through the AdoptedStylesheets controller. This enhancement focuses on the seamless attachment of encapsulated stylesheets via the createRenderRoot method. By leveraging modern browser capabilities, it ensures a more efficient and standardized inclusion of styles, significantly improving the style management process in web components.

Changes

File Path Change Summary
.../adopted-stylesheets/README.md Updated documentation to reflect the new method for attaching encapsulated stylesheets using createRenderRoot.
.../adopted-stylesheets/src/adopted-stylesheets.ts - Introduced constructor with globalStyles, root, and host parameters.
- Added adoptedSheet private property.
- Implemented logic to manage CSSStyleSheet objects, ensuring they are efficiently included and removed from adoptedStyleSheets.
- Renamed class from AdoptedStyleSheets to AdoptedStylesheets.
- Added root and host as new private properties.
- Updated hostConnected and hostDisconnected methods to align with the new property structure and logic.

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.
  • 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 from git 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

@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 a37a2af and 7fbdaee.
Files selected for processing (2)
  • packages/controllers/adopted-stylesheets/README.md (2 hunks)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Additional comments: 2
packages/controllers/adopted-stylesheets/README.md (2)
  • 33-33: The documentation correctly reflects the updated constructor signature with the root parameter. This aligns with the changes in the source code.
  • 41-101: The usage examples in the README have been updated to demonstrate the new functionality of attaching stylesheets to both the document and shadow roots. The examples are clear and correctly show the use of the root parameter in the constructor. However, ensure that the examples are tested to confirm that they work as intended, especially since the direct assignment to adoptedStyleSheets in the source code is not possible.

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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7fbdaee and 6328804.
Files selected for processing (1)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts

@glitchgirl glitchgirl marked this pull request as draft February 6, 2024 14:17
@himerus himerus marked this pull request as ready for review February 6, 2024 21:19
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 6328804 and 12be159.
Files selected for processing (2)
  • packages/controllers/adopted-stylesheets/README.md (2 hunks)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Additional comments: 5
packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (3)
  • 1-10: The import statement and class definition are correct and follow TypeScript conventions.
  • 69-75: Ensure that the logic in hostConnected correctly updates the adoptedStyleSheets array without directly mutating it, as this follows best practices for immutability.
  • 85-87: The filtering logic in hostDisconnected is correct and efficiently removes the stylesheet from the adoptedStyleSheets array.
packages/controllers/adopted-stylesheets/README.md (2)
  • 9-19: The installation instructions are clear and correctly specify commands for Yarn, NPM, and PNPM package managers.
  • 2-27: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [24-28]

The overview provides useful links to MDN Web Docs for further reading on adoptedStyleSheets. This is helpful for users seeking more in-depth information.

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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 12be159 and 6bafd84.
Files selected for processing (1)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts

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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6bafd84 and 41c46cf.
Files ignored due to path filters (2)
  • package.json is excluded by: !**/*.json
  • packages/controllers/adopted-stylesheets/package.json is excluded by: !**/*.json
Files selected for processing (1)
  • packages/controllers/adopted-stylesheets/README.md (1 hunks)
Additional comments: 3
packages/controllers/adopted-stylesheets/README.md (3)
  • 41-41: The constructor's documentation correctly reflects the new root parameter, enhancing the controller's flexibility. This change aligns with the PR objectives to allow attaching stylesheets to either the main document or a shadow root.
  • 104-105: The use of this.shadowRoot to conditionally create an instance of AdoptedStylesheets for encapsulated styles is a good practice. It ensures styles are only attached if a shadow root is available, aligning with the modern standards for web components. This example effectively demonstrates the flexibility introduced by the root parameter.
  • 121-121: The additional information section provides valuable resources for further reading on adopted stylesheets. Including links to MDN Web Docs is helpful for developers seeking more in-depth knowledge. This section is well-curated and relevant.

Comment on lines 11 to 35
To install the new package, use the following command, specific to your package manager:

### With Yarn

> For more information on using Yarn, visit the [Yarn documentation](https://classic.yarnpkg.com/en/docs/).

```bash
yarn add @phase2/outline-adopted-stylesheets-controller
```

## Overview
### With NPM

Adopted stylesheets are a method to apply styles to a document or a shadow root. They are a part of the CSS Shadow Parts specification. Unfortunately, the original documentation link is no longer available. However, you can find more information about adopted stylesheets and their usage in web components in the following resources:
> For more information on using NPM, visit the [NPM documentation](https://docs.npmjs.com/).

- [MDN Web Docs: Document adoptedStyleSheets](https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets): This documentation provides an in-depth look at the `adoptedStyleSheets` property of the `Document` interface.
- [MDN Web Docs: ShadowRoot adoptedStyleSheets](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/adoptedStyleSheets): This documentation provides an in-depth look at the `adoptedStyleSheets` property of the `ShadowRoot` interface.
```bash
npm i --save-dev @phase2/outline-adopted-stylesheets-controller
```

### With PNPM

> For more information on using PNPM, visit the [PNPM documentation](https://pnpm.io/usage).

```bash
pnpm add --save-dev @phase2/outline-adopted-stylesheets-controller
```
Copy link

Choose a reason for hiding this comment

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

The installation instructions are clear and cover multiple package managers (Yarn, NPM, PNPM). However, for NPM and PNPM, the flag --save-dev is used, which suggests these dependencies are only needed during development. This might not be accurate for a library intended to be used in production. Consider using --save for NPM and removing --save-dev for PNPM to correctly reflect that this package should be part of the production dependencies.

- npm i --save-dev @phase2/outline-adopted-stylesheets-controller
+ npm i @phase2/outline-adopted-stylesheets-controller
- pnpm add --save-dev @phase2/outline-adopted-stylesheets-controller
+ pnpm add @phase2/outline-adopted-stylesheets-controller

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
To install the new package, use the following command, specific to your package manager:
### With Yarn
> For more information on using Yarn, visit the [Yarn documentation](https://classic.yarnpkg.com/en/docs/).
```bash
yarn add @phase2/outline-adopted-stylesheets-controller
```
## Overview
### With NPM
Adopted stylesheets are a method to apply styles to a document or a shadow root. They are a part of the CSS Shadow Parts specification. Unfortunately, the original documentation link is no longer available. However, you can find more information about adopted stylesheets and their usage in web components in the following resources:
> For more information on using NPM, visit the [NPM documentation](https://docs.npmjs.com/).
- [MDN Web Docs: Document adoptedStyleSheets](https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets): This documentation provides an in-depth look at the `adoptedStyleSheets` property of the `Document` interface.
- [MDN Web Docs: ShadowRoot adoptedStyleSheets](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/adoptedStyleSheets): This documentation provides an in-depth look at the `adoptedStyleSheets` property of the `ShadowRoot` interface.
```bash
npm i --save-dev @phase2/outline-adopted-stylesheets-controller
```
### With PNPM
> For more information on using PNPM, visit the [PNPM documentation](https://pnpm.io/usage).
```bash
pnpm add --save-dev @phase2/outline-adopted-stylesheets-controller
```
To install the new package, use the following command, specific to your package manager:
### With Yarn
> For more information on using Yarn, visit the [Yarn documentation](https://classic.yarnpkg.com/en/docs/).
```bash
yarn add @phase2/outline-adopted-stylesheets-controller

With NPM

For more information on using NPM, visit the NPM documentation.

npm i @phase2/outline-adopted-stylesheets-controller

With PNPM

For more information on using PNPM, visit the PNPM documentation.

pnpm add @phase2/outline-adopted-stylesheets-controller

</details>
<!-- suggestion_end -->

<!-- This is an auto-generated comment by CodeRabbit -->

Comment on lines 85 to 88
GlobalStylesheets: AdoptedStylesheets | undefined = new AdoptedStylesheets(
this, // The host that this controller is associated with
globalStyles, // A string that contains the CSS styles to be adopted
document, // The root where the stylesheet will be adopted
Copy link

Choose a reason for hiding this comment

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

In the example of attaching a global stylesheet, the this keyword is incorrectly passed as the first argument to the AdoptedStylesheets constructor. This seems to be a mistake since the constructor expects a CSSResult and a root (Document or ShadowRoot) as parameters. The this keyword in this context refers to the component instance, not a stylesheet or root. This needs correction to match the constructor's expected parameters.

- GlobalStylesheets: AdoptedStylesheets | undefined = new AdoptedStylesheets(
-   this, // The host that this controller is associated with
-   globalStyles, // A string that contains the CSS styles to be adopted
-   document, // The root where the stylesheet will be adopted
+ GlobalStylesheets: AdoptedStylesheets | undefined = new AdoptedStylesheets(globalStyles, document);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
GlobalStylesheets: AdoptedStylesheets | undefined = new AdoptedStylesheets(
this, // The host that this controller is associated with
globalStyles, // A string that contains the CSS styles to be adopted
document, // The root where the stylesheet will be adopted
GlobalStylesheets: AdoptedStylesheets | undefined = new AdoptedStylesheets(globalStyles, document);

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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 41c46cf and 568a692.
Files selected for processing (2)
  • packages/controllers/adopted-stylesheets/README.md (1 hunks)
  • packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/controllers/adopted-stylesheets/README.md
Additional comments: 3
packages/controllers/adopted-stylesheets/src/adopted-stylesheets.ts (3)
  • 44-47: The constructor's signature has been correctly updated to include ReactiveControllerHost, cssText, and an optional root parameter. This aligns with the PR objectives for more flexible stylesheet management.
  • 69-75: The hostConnected method correctly adopts the CSSStyleSheet object into the root's adopted stylesheets if not already included. This implementation is efficient and aligns with modern browser standards for managing styles.
  • 85-87: The hostDisconnected method properly removes the CSSStyleSheet object from the root's adopted stylesheets if included. This ensures clean-up and prevents potential memory leaks, aligning with best practices for dynamic style management.

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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 568a692 and 3dbe9bb.
Files selected for processing (1)
  • packages/controllers/adopted-stylesheets/README.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/controllers/adopted-stylesheets/README.md

@himerus himerus merged commit fad8aba into next Feb 16, 2024
5 of 10 checks passed
@himerus himerus deleted the feature/adopted-stylesheets-update branch February 16, 2024 17:18
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

2 participants