Adds a shadow root to React components
npm install react-shadow-rootnpm install --save react-shadow-rootjsx
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 {; return (
{/ The shadow root will be attached to this DIV /}
{this.state.cnt}
);
}
}
`
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.$3
| 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. |$3
| 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. |