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

Web component host reset #56

Open
OhiraKyou opened this issue Dec 28, 2022 · 3 comments
Open

Web component host reset #56

OhiraKyou opened this issue Dec 28, 2022 · 3 comments

Comments

@OhiraKyou
Copy link

OhiraKyou commented Dec 28, 2022

I've been using this reset with web components (via LitElement). In doing so, I've found that the reset (both unset: all and display: revert) causes a lot of problems related to styling the :host (shadow root) selector.

Problem

Research suggests that the specificity for the :host selector is intended to make it act as the default style for a custom element. Therefore, it is expected to be overridden by any other styles.

That places the shadow root in quite an awkward position following a reset. After all, the purpose of a reset is to strip browser styling. After that, any user-authored styling (including the defaults of custom elements) should be respected.

Solution

My solution is to add display: contents !important to the :host selector in each component. Then, if a custom root is desired, it can be implemented as a separate class, applied to a container element (like a div).

/* Pass through root */
:host {
	display: contents !important;
}

/* Custom root */
.container {
	display: flex;
}

This solution is ideal in my case, because I don't like that shadow roots participate in the document layout in the first place; I prefer the pass-through behavior of React fragments (<></>).

Related

Related external links:

@OhiraKyou OhiraKyou changed the title Web component display reversion Web component host reset Dec 28, 2022
@tomprince
Copy link

If I am reading this correctly, the issue descried above is discussing the use of the-new-css-reset within a component. It appears that it also can occur when using it in a page that is consuming a web component.

For example,

<script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js" />
<ion-icon name="heart" />

displays a heart inline, where as

<link rel="stylesheet" href="https://unpkg.com/the-new-css-reset@1.8.2/css/reset.css" />
<script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js" />
<ion-icon name="heart" />

displays a huge heart.

@OhiraKyou
Copy link
Author

Yep. Because use of the :host selector is treated as default element styling—rather than authored styling—by its specificity, it gets overridden by resets (which override default element styling) anywhere they are used. The display: contents !important solution has been working pretty well, though.

@Darrenbydesign
Copy link

I too am running into this problem and its making it very difficult to even consider using this reset as a result. I wish there was a way to include the :host selector in the where clause like so :where(:not([elements here] , :host):not([other stuff]))

Anyone else find a way around this? The reset does a 90% good job for my design system needs but this is a big 10% problem

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

No branches or pull requests

3 participants