@@ -5,14 +5,12 @@ Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemen
5
5
6
6
## Usage
7
7
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:
9
9
10
10
``` javascript
11
11
import register from ' preact-custom-element' ;
12
12
13
- const Greeting = ({ name = ' World' }) => (
14
- < p> Hello, {name}! < / p>
15
- );
13
+ const Greeting = ({ name = ' World' }) => < p> Hello, {name}! < / p> ;
16
14
17
15
register (Greeting, ' x-greeting' , [' name' ]);
18
16
```
@@ -42,15 +40,15 @@ import register from 'preact-custom-element';
42
40
43
41
// <x-greeting name="Bo"></x-greeting>
44
42
class Greeting extends Component {
45
- // Register as <x-greeting>:
46
- static tagName = ' x-greeting' ;
43
+ // Register as <x-greeting>:
44
+ static tagName = ' x-greeting' ;
47
45
48
- // Track these attributes:
49
- static observedAttributes = [' name' ];
46
+ // Track these attributes:
47
+ static observedAttributes = [' name' ];
50
48
51
- render ({ name }) {
52
- return < p> Hello, {name}! < / p> ;
53
- }
49
+ render ({ name }) {
50
+ return < p> Hello, {name}! < / p> ;
51
+ }
54
52
}
55
53
register (Greeting);
56
54
```
@@ -60,15 +58,41 @@ If no `observedAttributes` are specified, they will be inferred from the keys of
60
58
``` js
61
59
// Other option: use PropTypes:
62
60
function FullName (props ) {
63
- return < span> {props .first } {props .last }< / span>
61
+ return (
62
+ < span>
63
+ {props .first } {props .last }
64
+ < / span>
65
+ );
64
66
}
65
67
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.
68
70
};
69
71
register (FullName, ' full-name' );
70
72
```
71
73
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
+ ```
72
96
73
97
## Related
74
98
0 commit comments