You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-6Lines changed: 42 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ 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
+
Any Preact component can be registered as a custom element simply by importing `register` and calling it with your component, a tag name<strong>*</strong>, and a list of attribute names you want to observe:
// Component definition Observed attributes Encapsulation mode for the shadow DOM tree
18
21
```
19
22
20
23
> _**\* Note:** as per the [Custom Elements specification](https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name), the tag name must contain a hyphen._
@@ -31,9 +34,14 @@ Output:
31
34
<p>Hello, Billy Jo!</p>
32
35
```
33
36
34
-
### Prop Names and Automatic Prop Names
37
+
### Observed Attributes
38
+
39
+
The Custom Elements v1 specification requires explicitly listing the names of attributes you want to observe in order to respond when their values are changed. These can be specified via the third parameter that's passed to the `register()` function:
35
40
36
-
The Custom Elements V1 specification requires explictly stating the names of any attributes you want to observe. From your Preact component perspective, `props` could be an object with any keys at runtime, so it's not always clear which props should be accepted as attributes.
41
+
```js
42
+
// Listen to changes to the `name` attribute
43
+
register(Greeting, 'x-greeting', ['name']);
44
+
```
37
45
38
46
If you omit the third parameter to `register()`, the list of attributes to observe can be specified using a static `observedAttributes` property on your Component. This also works for the Custom Element's name, which can be specified using a `tagName` static property:
39
47
@@ -59,16 +67,44 @@ If no `observedAttributes` are specified, they will be inferred from the keys of
59
67
60
68
```js
61
69
// Other option: use PropTypes:
62
-
functionFullName(props) {
63
-
return<span>{props.first} {props.last}</span>
70
+
functionFullName({ first, last }) {
71
+
return<span>{first} {last}</span>
64
72
}
73
+
65
74
FullName.propTypes= {
66
75
first:Object, // you can use PropTypes, or this
67
76
last:Object// trick to define untyped props.
68
77
};
78
+
69
79
register(FullName, 'full-name');
70
80
```
71
81
82
+
### Passing slots as props
83
+
84
+
The `register()` function also accepts an optional fourth parameter, an options bag. At present, it allows you to opt-in to using shadow DOM for your custom element by setting the `shadow` property to `true`, and if so, you can also specify the encapsulation mode with `mode`, which can be either `'open'` or `'closed'`.
85
+
86
+
When using shadow DOM, you can make use of named `<slot>` elements in your component to forward the custom element's children into specific places in the shadow tree.
0 commit comments