JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 18325
  • Score
    100M100P100Q140612F
  • License MIT

Adds a shadow root to React components

Package Exports

  • react-shadow-root

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-shadow-root) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-shadow-root

Lets you add a shadow root to React components allowing you to use the shadow DOM.

Installation

npm install --save react-shadow-root

Examples

https://apearce.github.io/react-shadow-root/

Usage

import React form 'react';
import ShadowRoot 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
        <ShadowRoot>
          <style>{style}</style>
          <span>{this.state.cnt}</span> <button onClick={this.increment}>Click Me</button>
        </ShadowRoot>
      </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 ShadowRoot tag.

Props

Prop Values Default Description
mode open or closed closed Sets the mode of the shadow root

Notes

  • A minimum of React 16 is required
  • Not all browsers support the shadow DOM. Click here for current browser support.
  • Not all HTML elements support the shadow DOM. Click here for more information.
  • It has been tested with the 'new' Context API and it worked fine. It has not been tested with the legacy API.