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

feature: element.getShadow(opts) #1046

Closed
trusktr opened this issue Jan 14, 2022 · 3 comments
Closed

feature: element.getShadow(opts) #1046

trusktr opened this issue Jan 14, 2022 · 3 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: shadow Relates to shadow trees (as defined in DOM)

Comments

@trusktr
Copy link

trusktr commented Jan 14, 2022

This would be an alternative to element.attachShadow(opts). It would be similar to canvas.getContext in that if a root is not already created/attached, it will create/attach the root before returning the root. If a root is already created, the existing root will be returned. This would make it easier to write code like this:

class MyEl extends HTMLElement {
  connectedCallback() {
    this.getShadow(opts).innerHTML = `...`
  }
}

without having to write extra code to handle whether or not a root already exists.

On a related note, maybe a similar utility would be nice for ElementInternals?

@trusktr
Copy link
Author

trusktr commented Jan 14, 2022

Hmm, that makes closed roots easy to get, doesn't it. Who actually uses closed roots?

Maybe this is better as an ElementInternals API? For example:

#internals = this.attachInternals()

connectedCallback() {
  this.#internals.getShadow(opts).innerHTML = `...`
}

@annevk
Copy link
Member

annevk commented Jan 17, 2022

Why create it on document connection and not when the element is created?

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: shadow Relates to shadow trees (as defined in DOM) labels Jan 17, 2022
@Jamesernator
Copy link

Jamesernator commented Jan 31, 2022

You could just do the following right? (See here)

const shadowRoot = this.#internals.shadowRoot ?? this.attachShadow({ ...opts });

Whether it is worth abstracting this into a new method I'm not sure. Although I would ask are empty shadow roots really so expensive that you can't just attach them unconditionally in the constructor? i.e.:

#internals = this.attachInternals();

constructor() {
    this.attachShadow({ ...opts });
}

connectedCallback() {
    this.#internals.shadowRoot.innerHTML = `...`;
}

@trusktr trusktr closed this as completed Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: shadow Relates to shadow trees (as defined in DOM)
Development

No branches or pull requests

3 participants