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
{{ message }}
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
For examples of more complex component definitions, take a look at the [example components](https://github.com/pimterry/server-components/blob/master/component-examples.md)
46
46
47
47
#### Use your components
48
48
49
49
```javascript
50
-
varcustomElements=require("server-components");
50
+
varcomponents=require("server-components");
51
51
52
52
// Render the HTML, and receive a promise for the resulting HTML string.
53
53
// The result is a promise because elements can render asynchronously, by returning
54
54
// promises from their callbacks. This allows elements to render content from
55
55
// external web services, your database, or anything else you can imagine.
56
-
customElements.renderPage(`
56
+
components.renderPage(`
57
57
<html>
58
58
<head></head>
59
59
<body>
@@ -83,15 +83,15 @@ There aren't many published sharable components to drop in quite yet, as it's st
83
83
84
84
### Top-level API
85
85
86
-
#### `customElements.HTMLElement`
86
+
#### `components.HTMLElement`
87
87
88
88
Creates a returns a new custom HTML element prototype, extending the HTMLElement prototype.
89
89
90
90
Note that this does *not* register the element. To do that, call `components.registerElement` with an element name, and options (typically including the prototype returned here as your 'prototype' value).
91
91
92
92
This is broadly equivalent to `Object.create(HTMLElement.prototype)` in browser land, and exactly equivalent here to `Object.create(components.dom.HTMLElement.prototype)`. You can call that yourself instead if you like, but it's a bit of a mouthful.
Registers an element, so that it will be used when the given element name is found during parsing.
97
97
@@ -103,25 +103,25 @@ This returns the constructor for the new element, so you can construct and inser
103
103
104
104
This is broadly equivalent to `document.registerElement` in browser land.
105
105
106
-
#### `customElements.renderPage(html)`
106
+
#### `components.renderPage(html)`
107
107
108
108
Takes an HTML string for a full page, and returns a promise for the HTML string of the rendered result. Server Components parses the HTML, and for each registered element within calls its various callbacks (see the Component API) below as it does so.
109
109
110
110
Unrecognized elements are left unchanged. When calling custom element callbacks any returned promises are collected, and this call will not return until all these promises have completed. If any promises are rejected, this renderPage call will be rejected too.
111
111
112
112
To support the full DOM Document API, this method requires that you are rendering a full page (including `<html>`, `<head>` and `<body>` tags). If you don't pass in content wrapped in those tags then they'll be automatically added, ensuring your resulting HTML has a full valid page structure. If that's not what you want, take a look at `renderFragment` below.
113
113
114
-
#### `customElements.renderFragment(html)`
114
+
#### `components.renderFragment(html)`
115
115
116
116
Takes an HTML string for part of a page, and returns a promise for the HTML string of the rendered result. Server Components parses the HTML, and for each registered element within calls its various callbacks (see the Component API) below as it does so.
117
117
118
118
Unrecognized elements are left unchanged. When calling custom element callbacks any returned promises are collected, and this call will not return until all these promises have completed. If any promises are rejected, this renderFragment call will be rejected too.
119
119
120
120
This method renders the content as a [Document Fragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment), a sub-part of a full document. This means if you there are any `<html>`, `<head>` or `<body>` tags in your input, they'll be stripped, as they're not legal within a fragment of a document. Note that this means the provided `document` object in your components will actually be a `DocumentFragment`, not a true `Document` object (although in most cases you can merrily ignore this). If you want to render a full page, take a look at `renderPage` above.
121
121
122
-
#### `customElements.dom`
122
+
#### `components.dom`
123
123
124
-
The DOM object (customElements.dom) exposes traditional DOM objects (normally globally available in browsers) such as the CustomEvent and various HTMLElement classes, typically use inside your component implementations.
124
+
The DOM object (components.dom) exposes traditional DOM objects (normally globally available in browsers) such as the CustomEvent and various HTMLElement classes, typically use inside your component implementations.
125
125
126
126
This is (very) broadly equivalent to `window` in browser land.
0 commit comments