Newly available across major browsers (Baseline since 2024)
A utility for the Shadow DOM in React
Caution
Development has been suspended due to the discovery of a critical issue.
Shadow DOM is a powerful web feature. Slot and style encapsulation work exceptionally well with JSX as templates. Unfortunately, React's Shadow DOM support is limited.
This project provides utilities for working with shadow DOM in React.
Deno:
deno add jsr:@miyauci/react-shadow-domNode.js
npx jsr add @miyauci/react-shadow-domThis library provides two main components: Template and ShadowRoot.
Provides a method for implementing Declarative Shadow DOM (DSD) in React.
This is <template>. However, it adjusts shadow DOM hydration.
When shadowRootMode is specified, the browser automatically attaches the
ShadowRoot. The
Template adjusts the VDOM on the client side to prevent hydration
errors.
import { Template } from "@miyauci/react-shadow-dom";
<div>
<Template shadowRootMode="open">
<style>
{`* {
color: gray;
}
`}
</style>
<button>
<slot name="icon" />
<slot />
</button>
</Template>
<span slot="icon" className="my-icon" />
Label
</div>;This is a container attached as a shadow root on the client side. In other
words, using the ShadowRoot as a boundary, render children into
the parent element's shadow root.
import { ShadowRoot } from "@miyauci/react-shadow-dom";
<div>
<ShadowRoot mode="open">
<style>
{`* {
color: gray;
}
`}
</style>
<button>
<slot name="icon" />
<slot />
</button>
</ShadowRoot>
<span slot="icon" className="my-icon" />
Label
</div>;Template implements DSD. ShadowRoot implements a client-side-only shadow root.
Note
Note that DSD is newly available across major browsers (baseline since 2024).
See deno docs
See CONTRIBUTING
MIT © Tomoki Miyauchi