-
-
Notifications
You must be signed in to change notification settings - Fork 470
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
insertInto and ShadowDom #256
Comments
I could use this too. It seems querySelector('#selectorUnitlShadow').shadowRoot or querySelector('#selectorUnitlShadow').shadowRoot.querySelector('.possibleDeeperQueries') respectively. Would the maintainers care for such a PR? |
This works: master...LukasBombach:allow-insertInto-shadow-dom I'm gonna test this a little bit tomorrow, write unit tests and might open a PR if you want to. |
I just copied this from the README I take it this means it's a CSS selector and you have to write a really specific selector for a shadow root. However, this selector doesn't function in recent versions of Chrome (no longer shows a deprecation warning, even, which may mean it's been removed). A very specific selector...since IDs are unique and used by all the examples, there's an implication that you can only put the styles inside one shadow root? Very little about this appears tenable to me for a web component. Since you don't control what IDs or classes a component consumer puts on the hosting element, this would require every consumer to do a webpack config that supports how you as the author intend your component to be built, because you can't (so far as I can tell) effectively distribute your component with the styles already built in. And how are you supposed to place the styles for a web component composed from other web components in the right places? There are multiple shadow roots encapsulated inside other shadow roots. |
@morewry Regarding the CSS selector for finding your web component. When you setup your project you are in control of the HTML, so you can write any specific CSS selector that fits your HTML, you don't have to account for users using your web component differently. In the end you compile everything and ship your component with the styles included independent of what selector you used during development. I hope I understood your remark right. Regarding nested custom elements. With the code I provided in the post before yours, you could do {
insertInto: 'my-component::shadow > #whatever > my-nested-component::shadow'
} |
Looks like
Instead of this: https://github.com/webpack-contrib/style-loader/blob/master/lib/addStyles.js#L46-L48 |
@savelichalex A solution would be to check if the target element has a shadow root. |
@herodrigues Yeah, you absolutely right 😄 And this is why I want to select target with custom function. Maybe I misspelling a little, check out this
And now in this place loader will be append new styles |
@savelichalex that makes sense. it might even be memoized for performance reasons. |
Add PR for this #279 |
Is there a way to use insertInto if the HTML element it would insert into isn't created until the application is launched? I'm using this with a chrome extension content script and so I don't have access to the document immediately. Because of this the style-loader is trying to insert the styles before I actually get to create the element! |
Neat! But I'm blocked the same way @austinwalter is: I create the shadow root programmatically at the beginning of my script :
but it gets inserted after style-loader execution of insertInto, so I get the error: |
@austinwalter @DiesIrae I'm not using import shadowCSS from 'css/shadow';
root = document.createElement('div');
root = container.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(shadowCSS));
root.appendChild(style);
document.body.appendChild(root); Of course this code goes after the DOM is loaded. |
@herodrigues |
@herodrigues |
@austinwalter @DiesIrae , were you guys able to find a work around ? I am currently struggling with this. |
i used the url-loader: <template>
<link href="./my-component.scss" rel="stylesheet">
<button class="btn"><slot>replace me</slot></button>
</template> my-component.scss .btn {
background-color: var(--btn-background-color, red);
} webpack.config.js ...
module: {
rules: [
{
test: /\.scss$/i,
issuer: [{ test: /\.html$/i }],
use: [{
loader: 'url-loader',
options: {
mimetype: 'text/css',
},
}, 'sass-loader']
},
{
test: /\.html$/i,
loader: 'html-loader',
options: {
attrs: ['img:src', 'link:href'],
},
},
... |
Now in 2020, was having this issue with Also tried Building a Chrome extension where style encapsulation is necessary for an overlayed TipsContainer component looks like this: import tawilwindCss from 'styles/tailwind.css'
export const Container = () => {
return (
<>
<style>{tawilwindCss}</style>
<div className="fixed inset-0 bg-overlay w-screen h-screen text-white flex items-center justify-center font-sans">
<div className="bg-dark rounded p-4">
{/* ... */}
</div>
</div>
</>
)
} Render the component as follows: export const renderApp = () => {
const el = document.createElement('div')
el.id = 'container'
el.style.height = '100vh'
el.style.width = '100vw'
el.style.position = 'fixed'
el.style.top = '0px'
el.style.left = '0px'
el.style.zIndex = '2147483647'
el.style.pointerEvents = 'none'
const shadowRoot = el.attachShadow({ mode: 'open' })
shadowRoot && retargetEvents(shadowRoot), (shadowRoot.innerHTML = `<div id="app"></div>`)
const renderInShadowDOM = () => {
document.body.append(el)
import('../components/container').then(({ Container }) =>
render(createElement(Container), shadowRoot.getElementById('app'))
)
}
if (document.readyState === 'complete' || document.readyState === 'interactive') {
renderInShadowDOM()
} else {
window.addEventListener('DOMContentLoaded', renderInShadowDOM)
}
} Probably will want to reset styles via: #app {
all: initial;
* {
all: unset;
}
} |
Regarding #135 , "insertInto" seems to me incomplete or obsolete (or missing extra features) at the moment when combining it with ShadowDom.
In the ReadME there is the following example:
My concern is that the pseudo selector "::shadow" is going (probably it is) to be deprecated along with "/deep/".
Are you aware of any other way to replicate the example above using css selectors?
Maybe, an improved/extended way would be a direct access to the element's shadowRoot - declaring or inspecting the element's 'nature' - , of course supporting also simple elements such as the one in the tests https://github.com/webpack-contrib/style-loader/blob/master/test/basicTest.js#L98 (a simple div with "target" as class).
The text was updated successfully, but these errors were encountered: