Targetting web component parts using ::part in the shadow dom itself

We can use ::part outside for styling a part in a web component but for the life of me I can't find out how to use this selector in the shadow dom <style> block itself. [part=thing] works until you have multiple parts. I've tried looking around and absolutely nobody talks about using this selector in the shadow dom but I refuse to believe it's not usable...
8 Replies
Rägnar O'ock
Rägnar O'ock3mo ago
Can't you select the elements like usual when you're in a shadow DOM fragment?
Rägnar O'ock
Rägnar O'ock3mo ago
MDN Web Docs
Using shadow DOM - Web APIs | MDN
An important aspect of custom elements is encapsulation, because a custom element, by definition, is a piece of reusable functionality: it might be dropped into any web page and be expected to work. So it's important that code running in the page should not be able to accidentally break a custom element by modifying its internal implementation. ...
knighty
knightyOP3mo ago
yeah but the ::part selector doesn't work i tried :host(::part(thing)) too, doesn't work (busy atm so cant respond much)
Rägnar O'ock
Rägnar O'ock3mo ago
::part is for when you are outside of the shadow DOM, from inside you should use normal CSS. And if you want to use the part name just use an attribute selector [part*=thing]
knighty
knightyOP3mo ago
if that's the case then fair enough but it seems uttyerly bizarre to me that you cant use the actual part selector (which probbaly has much better performance) inside the element itself i can of course just add a class but part="thing" class="thing" feels very stupid lol
Rägnar O'ock
Rägnar O'ock3mo ago
Attribute selectors don't have any perceptible impact on performance, use them
knighty
knightyOP3mo ago
maybe not serious but still gonna be worse probably because it's going to have to string match rather than use a hash map, plus there's potential for overlap there anyway whatever, if you can't use them so be it, thanks for the input
Rägnar O'ock
Rägnar O'ock3mo ago
It really doesn't matter, a * selector anywhere in your stylesheets will have a worse impact that tens of attribute selectors Happy to be of help

Did you find this page helpful?