
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-shadow-root
Advanced tools
Lets you add a shadow root to React components allowing you to use the shadow DOM. This provides scoped CSS and includes support for constructable stylesheets.
npm install --save react-shadow-root
https://apearce.github.io/react-shadow-root/
import React from 'react';
import ReactShadowRoot from 'react-shadow-root';
class ShadowCounter extends React.Component {
state = { cnt: 0 };
increment = () => {
this.setState({
cnt: this.state.cnt + 1
});
}
render() {
const style = `span {
background-color: #333;
border-radius: 3px;
color: #fff;
padding: 1px 5px;
}
button {
background-color: #fff;
border: 1px solid currentColor;
border-radius: 3px;
color: #333;
cursor: pointer;
outline: 0;
}
button:active {
background-color: #333;
color: #fff;
}`;
return (
<div> {/* The shadow root will be attached to this DIV */}
<ReactShadowRoot>
<style>{style}</style>
<span>{this.state.cnt}</span> <button onClick={this.increment}>Click Me</button>
</ReactShadowRoot>
</div>
);
}
}
When the shadow root is created on its parent element, all children are copied into the shadow DOM. Styles in the shadow DOM are automatically scoped. You can inspect the element to confirm. Slots work as expected; just be sure to add {this.props.children}
after the closing ReactShadowRoot
tag.
Name | Description |
---|---|
constructableStylesheetsSupported | A boolean telling you if constructable stylesheets are supported by the browser. |
constructibleStylesheetsSupported | An alias of constructableStylesheetsSupported using the 'correct' spelling. |
shadowRootSupported | A boolean telling you if attaching a shadow root is supported by the browser, not the element. |
Prop | Type | Values | Default | Description |
---|---|---|---|---|
declarative | Boolean | true or false | false | Creates a Declarative Shadow Root |
delegatesFocus | Boolean | true or false | false | Expands the focus behavior of elements within the shadow DOM. Click here for more information. |
mode | String | open or closed | open | Sets the mode of the shadow root. |
stylesheets | Array | arrayOf(CSSStyleSheet) | optional | Takes an array of CSSStyleSheet objects for constructable stylesheets. |
FAQs
Adds a shadow root to React components
The npm package react-shadow-root receives a total of 15,525 weekly downloads. As such, react-shadow-root popularity was classified as popular.
We found that react-shadow-root demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.