Skip to content

Commit 8a17904

Browse files
authored
Merge pull request #25 from preactjs/update-readme
Update readme and add section about observedAttributes/propTypes
2 parents e6eda8d + 048d904 commit 8a17904

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemen
88
Import `CustomElement` and call with your component a tag name __\*__, and a list of attribute names you want to observe:
99

1010
```javascript
11-
import registerCustomElement from "preact-custom-element";
11+
import register from 'preact-custom-element';
1212

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

17-
registerCustomElement(Greeting, "x-greeting", ["name"]);
17+
register(Greeting, 'x-greeting', ['name']);
1818
```
1919

2020
> _**\* Note:** as per the [Custom Elements specification](http://w3c.github.io/webcomponents/spec/custom/#prod-potentialcustomelementname), the tag name must contain a hyphen._
@@ -31,19 +31,45 @@ Output:
3131
<p>Hello, Billy Jo!</p>
3232
```
3333

34-
### Why the prop names parameter?
34+
### Prop Names and Automatic Prop Names
3535

36-
The Custom Elements V1 spec requires you to explictly state the attribute names you want to observe. From your Preact component perspective, `props` could be an object with any keys at runtime. This unfortunate combination of factors leaves us needing to explicitly state them.
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.
3737

38-
It's possible that a compile step could introspect your usages of props and generate the glue code here. Please send me a link if you do this!
38+
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:
3939

40-
## Related
40+
```js
41+
import register from 'preact-custom-element';
4142

42-
[preact-shadow-dom](https://github.com/bspaulding/preact-shadow-dom)
43+
// <x-greeting name="Bo"></x-greeting>
44+
class Greeting extends Component {
45+
// Register as <x-greeting>:
46+
static tagName = 'x-greeting';
4347

44-
## Thanks
48+
// Track these attributes:
49+
static observedAttributes = ['name'];
4550

46-
Big thanks to BrowserStack for providing service for CI on this project! BrowserStack allows us to test this project on all real browsers that support Custom Elements, including mobile browsers.
51+
render({ name }) {
52+
return <p>Hello, {name}!</p>;
53+
}
54+
}
55+
register(Greeting);
56+
```
57+
58+
If no `observedAttributes` are specified, they will be inferred from the keys of `propTypes` if present on the Component:
59+
60+
```js
61+
// Other option: use PropTypes:
62+
function FullName(props) {
63+
return <span>{props.first} {props.last}</span>
64+
}
65+
FullName.propTypes = {
66+
first: Object, // you can use PropTypes, or this
67+
last: Object // trick to define untyped props.
68+
};
69+
register(FullName, 'full-name');
70+
```
4771

48-
<a href="https://www.browserstack.com" target="_blank" rel="noopener noreferrer"><img src="https://p14.zdusercontent.com/attachment/1015988/9muQl92dJ9ShKIGmIt7iaICUb?token=eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..W4aqOGR0iTl_Rh1nskJGRQ.gLdLdkMD8vfZdJ7eqZpU6lmB-yGQv2hCYRJeBQ91WtaJzpYMQQUEWNE0oK3xLjBKYPKWA9D1UlA-beeUwlczRKVF8ZoG8OMDg6K3vVIIFKH3an8QfcH0iFQXhH4m6cmXqoPAcqDXvrpv3DUXIQaxD8bXykKFpBR5gEk6m3VsH8geK4UxzQ3ORYCOv4XD8EPm-Ap0lZwVZaGMHAncCP9dlOVhZjVVDKwBI5cwFOa_jSwtsCbgW3EX901k-nu1w6IlgFvWh8mxMaM4DMtVtCGfnuNspN7qYXJRTgMEVPVIk8o.bKvlbSGn8PntRSHO7sgBSA" height="150"/>
49-
</a>
72+
73+
## Related
74+
75+
[preact-shadow-dom](https://github.com/bspaulding/preact-shadow-dom)

0 commit comments

Comments
 (0)