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
description: Wrap your Preact component up as a custom element
4
+
---
5
+
6
+
# preact-custom-element
7
+
8
+
Preact's tiny size and standards-first approach make it a great choice for building web components.
9
+
10
+
Preact is designed to render both full applications and individual parts of a page, making it a natural fit for building Web Components. Many companies use this approach to build component or design systems that are then wrapped up into a set of Web Components, enabling re-use across multiple projects and within other frameworks whilst continuing to offer the familiar Preact APIs.
11
+
12
+
---
13
+
14
+
<toc></toc>
15
+
16
+
---
17
+
18
+
## Creating a Web Component
19
+
20
+
Any Preact component can be turned into a web component with [preact-custom-element](https://github.com/preactjs/preact-custom-element), a very thin wrapper that adheres to the Custom Elements v1 spec.
21
+
22
+
```jsx
23
+
importregisterfrom'preact-custom-element';
24
+
25
+
constGreeting= ({ name ='World' }) =><p>Hello, {name}!</p>;
> Note: As per the [Custom Element Specification](http://w3c.github.io/webcomponents/spec/custom/#prod-potentialcustomelementname), the tag name must contain a hyphen (`-`).
34
+
35
+
Use the new tag name in HTML, attribute keys and values will be passed in as props:
36
+
37
+
```html
38
+
<x-greetingname="Billy Jo"></x-greeting>
39
+
```
40
+
41
+
Output:
42
+
43
+
```html
44
+
<p>Hello, Billy Jo!</p>
45
+
```
46
+
47
+
### Observed Attributes
48
+
49
+
Web Components require 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:
50
+
51
+
```jsx
52
+
// Listen to changes to the `name` attribute
53
+
register(Greeting, 'x-greeting', ['name']);
54
+
```
55
+
56
+
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:
57
+
58
+
```jsx
59
+
importregisterfrom'preact-custom-element';
60
+
61
+
// <x-greeting name="Bo"></x-greeting>
62
+
classGreetingextendsComponent {
63
+
// Register as <x-greeting>:
64
+
static tagName ='x-greeting';
65
+
66
+
// Track these attributes:
67
+
static observedAttributes = ['name'];
68
+
69
+
render({ name }) {
70
+
return<p>Hello, {name}!</p>;
71
+
}
72
+
}
73
+
register(Greeting);
74
+
```
75
+
76
+
If no `observedAttributes` are specified, they will be inferred from the keys of `propTypes` if present on the Component:
77
+
78
+
```jsx
79
+
// Other option: use PropTypes:
80
+
functionFullName({ first, last }) {
81
+
return (
82
+
<span>
83
+
{first} {last}
84
+
</span>
85
+
);
86
+
}
87
+
88
+
FullName.propTypes= {
89
+
first:Object, // you can use PropTypes, or this
90
+
last:Object// trick to define un-typed props.
91
+
};
92
+
93
+
register(FullName, 'full-name');
94
+
```
95
+
96
+
### Passing slots as props
97
+
98
+
The `register()` function has a fourth parameter to pass options; currently, only the `shadow` option is supported, which attaches a shadow DOM tree to the specified element. When enabled, this allows the use of named `<slot>` elements to forward the Custom Element's children to specific places in the shadow tree.
Copy file name to clipboardExpand all lines: content/en/guide/v10/web-components.md
+3-110Lines changed: 3 additions & 110 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,12 +5,9 @@ description: How to use web components with Preact
5
5
6
6
# Web Components
7
7
8
-
Preact's tiny size and standards-first approach make it a great choice for building web components.
8
+
Web Components are a set of different technologies that allow you to create reusable, encapsulated custom HTML elements that are entirely framework-agnostic. Examples of Web Components include elements like `<material-card>` or `<tab-bar>`.
9
9
10
-
Web Components are a set of standards that make it possible to build new HTML element types - Custom Elements like `<material-card>` or `<tab-bar>`.
11
-
Preact [fully supports these standards](https://custom-elements-everywhere.com/#preact), allowing seamless use of Custom Element lifecycles, properties and events.
12
-
13
-
Preact is designed to render both full applications and individual parts of a page, making it a natural fit for building Web Components. Many companies use it to build component or design systems that are then wrapped up into a set of Web Components, enabling re-use across multiple projects and within other frameworks.
10
+
As a platform primitive, Preact [fully supports Web Components](https://custom-elements-everywhere.com/#preact), allowing seamless use of Custom Element lifecycles, properties, and events in your Preact apps.
14
11
15
12
Preact and Web Components are complementary technologies: Web Components provide a set of low-level primitives for extending the browser, and Preact provides a high-level component model that can sit atop those primitives.
16
13
@@ -56,7 +53,7 @@ function Foo() {
56
53
}
57
54
```
58
55
59
-
> **Note:** Preact makes no assumptions over naming schemes and will not attempt to coerce names, in JSX or otherwise, to DOM properties. If a custom element has a property name `someProperty`, it will need to be set using `someProperty=...` rather than`some-property=...`.
56
+
> **Note:** Preact makes no assumptions over naming schemes and will not attempt to coerce names, in JSX or otherwise, to DOM properties. If a custom element has a property name `someProperty`, then it will need to be set using that exact same capitalization and spelling (`someProperty=...`). `someproperty=...` or`some-property=...` will not work.
60
57
61
58
When rendering static HTML using `preact-render-to-string` ("SSR"), complex property values like the object above are not automatically serialized. They are applied once the static HTML is hydrated on the client.
62
59
@@ -92,107 +89,3 @@ Preact normalizes the casing of standard built-in DOM Events, which are normally
92
89
// Corrected: listens for "tabchange" event (lower-case)
Any Preact component can be turned into a web component with [preact-custom-element](https://github.com/preactjs/preact-custom-element), a very thin wrapper that adheres to the Custom Elements v1 spec.
99
-
100
-
```jsx
101
-
importregisterfrom'preact-custom-element';
102
-
103
-
constGreeting= ({ name ='World' }) =><p>Hello, {name}!</p>;
> Note: As per the [Custom Element Specification](http://w3c.github.io/webcomponents/spec/custom/#prod-potentialcustomelementname), the tag name must contain a hyphen (`-`).
112
-
113
-
Use the new tag name in HTML, attribute keys and values will be passed in as props:
114
-
115
-
```html
116
-
<x-greetingname="Billy Jo"></x-greeting>
117
-
```
118
-
119
-
Output:
120
-
121
-
```html
122
-
<p>Hello, Billy Jo!</p>
123
-
```
124
-
125
-
### Observed Attributes
126
-
127
-
Web Components require 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:
128
-
129
-
```jsx
130
-
// Listen to changes to the `name` attribute
131
-
register(Greeting, 'x-greeting', ['name']);
132
-
```
133
-
134
-
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:
135
-
136
-
```jsx
137
-
importregisterfrom'preact-custom-element';
138
-
139
-
// <x-greeting name="Bo"></x-greeting>
140
-
classGreetingextendsComponent {
141
-
// Register as <x-greeting>:
142
-
static tagName ='x-greeting';
143
-
144
-
// Track these attributes:
145
-
static observedAttributes = ['name'];
146
-
147
-
render({ name }) {
148
-
return<p>Hello, {name}!</p>;
149
-
}
150
-
}
151
-
register(Greeting);
152
-
```
153
-
154
-
If no `observedAttributes` are specified, they will be inferred from the keys of `propTypes` if present on the Component:
155
-
156
-
```jsx
157
-
// Other option: use PropTypes:
158
-
functionFullName({ first, last }) {
159
-
return (
160
-
<span>
161
-
{first} {last}
162
-
</span>
163
-
);
164
-
}
165
-
166
-
FullName.propTypes= {
167
-
first:Object, // you can use PropTypes, or this
168
-
last:Object// trick to define un-typed props.
169
-
};
170
-
171
-
register(FullName, 'full-name');
172
-
```
173
-
174
-
### Passing slots as props
175
-
176
-
The `register()` function has a fourth parameter to pass options; currently, only the `shadow` option is supported, which attaches a shadow DOM tree to the specified element. When enabled, this allows the use of named `<slot>` elements to forward the Custom Element's children to specific places in the shadow tree.
0 commit comments