-
-
Notifications
You must be signed in to change notification settings - Fork 948
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
Declarative Shadow DOM #1834
Comments
As a test I just tried in Chrome: const d = document.createElement("div")
d.innerHTML = `<div>
<template shadowrootmode="open">
<p>This is in the shadow DOM</p>
<slot>
<p>This is in the light DOM</p>
<button>Light button</button>
</slot>
<button>Shadow button</button>
</template>
</div>` And I got:
I was just trying to figure out what the parsing would look like for this. I think it might start with identifying with template elements we need to walk into |
This section of this article might answer that |
This functionality is already present for Portals using the Is it possible to expand this functionality to a generic utility component, eg something like |
A simple workaround is to make a Something like the following (I wrote it quickly, didn't test it, but it shows idea): function ShadowRoot(props) {
let div
onMount(() => {
const root = div.attachShadow(...)
render(props.children, root) // or similar
})
return <div ref={div}></div>
}
function UsageExample() {
return <ShadowRoot>
<div>hello<div>
<style>{`
/* scoped style */
div { background: blue }
`}</style>
</ShadowRoot>
} But this is not compatible with SSR, it creates a new root, and contexts won't automatically flow into the new root, etc. Other than that it has worked great in some places where I used it. Declarative shadow roots will be, and that'll be really nice! Every framework that doesn't have scoped styles out of the box is gonna now have scoped styles out of the box, with the same sorts of patterns as custom elements. |
Revisiting this. There is a new |
The documentation for all of this is currently pretty terrible so I wrote about it here: https://fullystacked.net/innerhtml-alternatives/ |
This would epic to have it working. Combine it with solidjs/solid-router#459 and we can get awesome power of Solid.js together with Server Actions and Server Rendering. ❤️ |
This seems to work well: /**
* A declarative shadow root component
*
* Hooks into SolidJS' Portal's `useShadow` prop
* to handle shadow DOM and the component lifecycle
*/
const ShadowRoot: ParentComponent = (props) => {
let div: HTMLDivElement;
return (
<div ref={div!}>
<Portal mount={div!} useShadow={true}>
{props.children}
</Portal>
</div>
);
}; |
Describe the bug
I would like to have the ability to use declarative shadow DOM (without custom elements) within Solid components just to get style scoping. It doesn't work and I get the error Uncaught TypeError: Cannot read properties of null (reading 'nextSibling')
Your Example Website or App
https://codesandbox.io/p/sandbox/awesome-sky-8fp8z5
Steps to Reproduce the Bug or Issue
Expected behavior
It should render a shadow tree.
Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: