Skip to content

Commit e7c05b7

Browse files
authored
docs: Update ReadMe to mention shadow & mode options (#102)
1 parent 4a5346a commit e7c05b7

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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+
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:
99

1010
```javascript
1111
import register from 'preact-custom-element';
@@ -14,7 +14,10 @@ const Greeting = ({ name = 'World' }) => (
1414
<p>Hello, {name}!</p>
1515
);
1616

17-
register(Greeting, 'x-greeting', ['name']);
17+
register(Greeting, 'x-greeting', ['name'], { shadow: true, mode: 'open' });
18+
// ^ ^ ^ ^ ^
19+
// | HTML tag name | use shadow-dom |
20+
// Component definition Observed attributes Encapsulation mode for the shadow DOM tree
1821
```
1922

2023
> _**\* 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:
3134
<p>Hello, Billy Jo!</p>
3235
```
3336

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:
3540

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+
```
3745

3846
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:
3947

@@ -59,16 +67,44 @@ If no `observedAttributes` are specified, they will be inferred from the keys of
5967

6068
```js
6169
// Other option: use PropTypes:
62-
function FullName(props) {
63-
return <span>{props.first} {props.last}</span>
70+
function FullName({ first, last }) {
71+
return <span>{first} {last}</span>
6472
}
73+
6574
FullName.propTypes = {
6675
first: Object, // you can use PropTypes, or this
6776
last: Object // trick to define untyped props.
6877
};
78+
6979
register(FullName, 'full-name');
7080
```
7181

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.
87+
88+
```jsx
89+
function TextSection({ heading, content }) {
90+
return (
91+
<div>
92+
<h2>{heading}</h2>
93+
<p>{content}</p>
94+
</div>
95+
);
96+
}
97+
98+
register(TextSelection, 'text-selection', [], { shadow: true });
99+
```
100+
101+
```html
102+
<text-section>
103+
<span slot="heading">My Heading</span>
104+
<span slot="content">Some content goes here.</span>
105+
</text-section>
106+
```
107+
72108

73109
## Related
74110

0 commit comments

Comments
 (0)