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

[reactive-element] Normalize adoptStyles so that it works x-browser when called to modify instance styles #3010

Closed
sorvell opened this issue Jun 8, 2022 · 2 comments
Assignees

Comments

@sorvell
Copy link
Member

sorvell commented Jun 8, 2022

ReactiveElement supports static styles that are not modified per instance. This was done so that support for Web Components polyfilled styling via ShadyCSS (which does not support instance styling) would work. The adoptStyles function was designed for this use case.

However, now that a significant portion of users do not need Web Components polyfills, it's reasonable to start introducing better instance based styling. Currently adoptStyles works inconsistently when the underlying adoptedStylesheets is supported or not. This behavior should be normalized so that it works the same x-browser when native Shadow DOM is used.

Separately, users have asked for a way to add styles rather than re-apply them. This could be done as a separate method or an alternative signature.

@sorvell
Copy link
Member Author

sorvell commented Sep 30, 2022

We've decided not to address this issue. On browsers that support adoptedStyleSheets, you can call this.shadowRoot.adoptedStyleSheets.push. Only Safari doesn't currently support this.

However, it looks like Safari support will be added soon, see WebKit/WebKit#4872.

Given that and the code size impact of #3060, we've elected to wait for native support.

@sorvell sorvell closed this as completed Sep 30, 2022
@jimmywarting
Copy link

jimmywarting commented Jul 4, 2023

It was unclear to me if lit did support

import style from './style.css' assert { type: 'css' }

class NameTag extends LitElement {
  static styles = style
}

but it was able to understand it. i guess the example confused me a bit as to why your example was more like:

import {LitElement, css} from 'lit'

export class MyElement extends LitElement {
  static styles = css`p { color: green; }`
}

i think that the example should be like have been more like the constructible CSSStyleSheet.

import { LitElement } from 'lit'

const css = new CSSStyleSheet()
css.replaceSync(`p { color: green; }`)

export class MyElement extends LitElement {
  static styles = css
}

(or as the first example at the very top)
so i blame this doc for not using native approaches or isn't refering to how to use native approaches instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants