Skip to content

Commit ac02aac

Browse files
committed
add docs for shadow dom options
1 parent d0a8f88 commit ac02aac

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

README.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemen
55

66
## Usage
77

8-
Import `register` and call it with your component, a tag name<strong>*</strong>, and a list of attribute names you want to observe:
8+
Import `register` and call it with your component, a tag name<strong>\*</strong>, and a list of attribute names you want to observe:
99

1010
```javascript
1111
import register from 'preact-custom-element';
1212

13-
const Greeting = ({ name = 'World' }) => (
14-
<p>Hello, {name}!</p>
15-
);
13+
const Greeting = ({ name = 'World' }) => <p>Hello, {name}!</p>;
1614

1715
register(Greeting, 'x-greeting', ['name']);
1816
```
@@ -42,15 +40,15 @@ import register from 'preact-custom-element';
4240

4341
// <x-greeting name="Bo"></x-greeting>
4442
class Greeting extends Component {
45-
// Register as <x-greeting>:
46-
static tagName = 'x-greeting';
43+
// Register as <x-greeting>:
44+
static tagName = 'x-greeting';
4745

48-
// Track these attributes:
49-
static observedAttributes = ['name'];
46+
// Track these attributes:
47+
static observedAttributes = ['name'];
5048

51-
render({ name }) {
52-
return <p>Hello, {name}!</p>;
53-
}
49+
render({ name }) {
50+
return <p>Hello, {name}!</p>;
51+
}
5452
}
5553
register(Greeting);
5654
```
@@ -60,15 +58,41 @@ If no `observedAttributes` are specified, they will be inferred from the keys of
6058
```js
6159
// Other option: use PropTypes:
6260
function FullName(props) {
63-
return <span>{props.first} {props.last}</span>
61+
return (
62+
<span>
63+
{props.first} {props.last}
64+
</span>
65+
);
6466
}
6567
FullName.propTypes = {
66-
first: Object, // you can use PropTypes, or this
67-
last: Object // trick to define untyped props.
68+
first: Object, // you can use PropTypes, or this
69+
last: Object, // trick to define untyped props.
6870
};
6971
register(FullName, 'full-name');
7072
```
7173

74+
### Using the shadow DOM
75+
76+
It is possible to attach a shadow root to the custom element to have your component rendered in a separate subtree.
77+
Customization of the shadow root `mode` is also possible on element basis.
78+
Read more about the shadow DOM on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot).
79+
80+
```tsx
81+
function WithShadow(props) {
82+
return <span>Hello from shadow root!</span>;
83+
}
84+
85+
// use shadow root in "open" mode
86+
register(FullName, 'full-name', [], {
87+
shadow: true,
88+
});
89+
90+
// use shadow root in "closed" mode
91+
register(FullName, 'full-name-encapsulated', [], {
92+
shadow: true,
93+
mode: 'closed',
94+
});
95+
```
7296

7397
## Related
7498

0 commit comments

Comments
 (0)