diff --git a/README.md b/README.md index 2123a09..69f733b 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,12 @@ Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemen ## Usage -Import `register` and call it with your component, a tag name*, and a list of attribute names you want to observe: +Import `register` and call it with your component, a tag name\*, and a list of attribute names you want to observe: ```javascript import register from 'preact-custom-element'; -const Greeting = ({ name = 'World' }) => ( -

Hello, {name}!

-); +const Greeting = ({ name = 'World' }) =>

Hello, {name}!

; register(Greeting, 'x-greeting', ['name']); ``` @@ -42,15 +40,15 @@ import register from 'preact-custom-element'; // class Greeting extends Component { - // Register as : - static tagName = 'x-greeting'; + // Register as : + static tagName = 'x-greeting'; - // Track these attributes: - static observedAttributes = ['name']; + // Track these attributes: + static observedAttributes = ['name']; - render({ name }) { - return

Hello, {name}!

; - } + render({ name }) { + return

Hello, {name}!

; + } } register(Greeting); ``` @@ -60,15 +58,41 @@ If no `observedAttributes` are specified, they will be inferred from the keys of ```js // Other option: use PropTypes: function FullName(props) { - return {props.first} {props.last} + return ( + + {props.first} {props.last} + + ); } FullName.propTypes = { - first: Object, // you can use PropTypes, or this - last: Object // trick to define untyped props. + first: Object, // you can use PropTypes, or this + last: Object, // trick to define untyped props. }; register(FullName, 'full-name'); ``` +### Using the shadow DOM + +It is possible to attach a shadow root to the custom element to have your component rendered in a separate subtree. +Customization of the shadow root `mode` is also possible on element basis. +Read more about the shadow DOM on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot). + +```tsx +function WithShadow(props) { + return Hello from shadow root!; +} + +// use shadow root in "open" mode +register(FullName, 'full-name', [], { + shadow: true, +}); + +// use shadow root in "closed" mode +register(FullName, 'full-name-encapsulated', [], { + shadow: true, + mode: 'closed', +}); +``` ## Related