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

Styling by ":host.someclass" -- styling is ignored in shadow dom components #7

Closed
Nashorn opened this issue Sep 25, 2020 · 2 comments
Closed
Assignees
Labels
bug Something isn't working syntax

Comments

@Nashorn
Copy link
Collaborator

Nashorn commented Sep 25, 2020

When using :host selector for styling components, you might try a rule like this:

:host.hidden {
    display:none;
}

When your component has the .hidden class, the css rule above works:

<my-component class="WebComponent MyComponent hidden"></my-component>

When your component is in the shadow and has the .hidden class, the css rule above is ignored:

<my-component class="WebComponent MyComponent hidden" shadow></my-component>

SOLUTION:

This is by w3c specification. Your css rule is invalid for shadow dom. The correct syntax is:

:host(.hidden) {
    display:none;
}

The problem is, when not running in shadow, :host(.hidden) is ignored, you'll have to
alter the rule again for non shadow dom.

:host.hidden {
    display:none;
}

[TODO: Fix issue in onTransformStyle()]

NOTE:
The rule, :host(.hidden) is not the same as :host .hidden.

:host .hidden {     /*targets a childNode inside host, with class "hidden", and works as expected*/
    display:none;
}
@Nashorn Nashorn added invalid This doesn't seem right wontfix This will not be worked on syntax labels Sep 25, 2020
@Nashorn Nashorn changed the title Styling by :host.some-class -- styling is ignored Styling by ":host.someclass" -- styling is ignored in shadow dom components Sep 25, 2020
@Nashorn Nashorn added bug Something isn't working and removed wontfix This will not be worked on invalid This doesn't seem right labels Sep 25, 2020
@Nashorn Nashorn self-assigned this Sep 25, 2020
@Nashorn Nashorn pinned this issue Sep 25, 2020
@Nashorn
Copy link
Collaborator Author

Nashorn commented Nov 7, 2020

Fix at framework level. TODO: Move this additional transform into onTransformStyle:

SAMPLE INPUT:
var t = `:host(.test) #button {}`;

TRANSFORM:

var r = /\:host\(([^\)]*)\)/;
t=t.replace(r, function(a1,a2) {
    console.log(arguments)
    return `:host${a2}`
})

OUTPUT:
:host.test #button {}

@Nashorn
Copy link
Collaborator Author

Nashorn commented Jul 1, 2021

FIXED.

@Nashorn Nashorn closed this as completed Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working syntax
Projects
None yet
Development

No branches or pull requests

1 participant