Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 63ad32a

Browse files
committed
Add docs
1 parent 2671b99 commit 63ad32a

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

README.md

+18-22
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ You can take the same ideas (and standards), apply them directly server side, to
2727
var components = require("server-components");
2828

2929
// Get the prototype for a new element
30-
var NewElement = components.newElement();
31-
32-
// When the element is created during DOM parsing, you can transform the HTML inside it.
33-
// This can be configurable too, either by setting attributes or adding HTML content
34-
// inside it or elsewhere in the page it can interact with. Elements can fire events
35-
// that other elements can receive to allow interactions, or even expose methods
36-
// or data that other elements in the page can access directly.
37-
NewElement.createdCallback = function () {
38-
this.innerHTML = "Hi there";
39-
};
30+
class NewElement extends components.HTMLElement {
31+
// When the element is created during DOM parsing, you can transform the HTML inside it.
32+
// This can be configurable too, either by setting attributes or adding HTML content
33+
// inside it or elsewhere in the page it can interact with. Elements can fire events
34+
// that other elements can receive to allow interactions, or even expose methods
35+
// or data that other elements in the page can access directly.
36+
connectedCallback() {
37+
this.innerHTML = "Hi there";
38+
}
39+
}
4040

4141
// Register the element with an element name
42-
components.registerElement("my-new-element", { prototype: NewElement });
42+
components.customElements.define("my-new-element", NewElement);
4343
```
4444

4545
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)
@@ -83,15 +83,15 @@ There aren't many published sharable components to drop in quite yet, as it's st
8383

8484
### Top-level API
8585

86-
#### `components.newElement()`
86+
#### `components.HTMLElement`
8787

8888
Creates a returns a new custom HTML element prototype, extending the HTMLElement prototype.
8989

9090
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).
9191

9292
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.
9393

94-
#### `components.registerElement(componentName, options)`
94+
#### `components.customElements.define(componentName, Constructor)`
9595

9696
Registers an element, so that it will be used when the given element name is found during parsing.
9797

@@ -131,9 +131,9 @@ These methods are methods you can implement on your component prototype (as retu
131131

132132
Any methods that are implemented, from this selection or otherwise, will be exposed on your element in the DOM during rendering. I.e. you can call `document.querySelector("my-element").setTitle("New Title")` and to call the `setTitle` method on your object, which can then potentially change how your component is rendered.
133133

134-
#### `yourComponent.createdCallback(document)`
134+
#### `yourComponentConstructor.prototype.createdCallback(document)`
135135

136-
Called when an element is created.
136+
Called when an element is attached to the faux DOM.
137137

138138
**This is where you put your magic!** Rewrite the elements contents to dynamically generate what your users will actually see client side. Read configuration from attributes or the initial child nodes to create flexible reconfigurable reusable elements. Register for events to create elements that interact with the rest of the application structure. Build your page.
139139

@@ -143,17 +143,13 @@ If this callback returns a promise, the rendering process will not resolve until
143143

144144
These callbacks are called in opening tag order, so a parent's createdCallback is called, then each of its children's, then its next sibling element.
145145

146-
#### `yourComponent.attachedCallback(document)`
147-
148-
Called when the element is attached to the DOM. This is different to when it's created when your component is being built programmatically, not through HTML parsing. *Not yet implemented*
149-
150-
#### `yourComponent.detachedCallback(document)`
146+
#### `yourComponentConstructor.prototype.disconnectedCallback(document)`
151147

152148
Called when the element is removed from the DOM. *Not yet implemented*
153149

154-
#### `yourComponent.attributeChangedCallback(document)`
150+
#### `yourComponentConstructor.prototype.attributeChangedCallback(document)`
155151

156-
Called when an attribute of the element is added, changed, or removed. *Not yet implemented*.
152+
Called when an attribute of the element is added, changed, or removed. *Partially implemented;* runs on component initialization.
157153

158154
**So far only the createdCallback is implemented here, as the others are less relevant initially for the key simpler cases. Each of those will be coming in time though! Watch this space.**
159155

component-examples.md

+20-21
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ With the web component below, rendering `<my-greeting></my-greeting>` will resul
1616
```javascript
1717
var components = require("server-components");
1818

19-
var StaticElement = components.newElement();
20-
StaticElement.createdCallback = function () {
21-
this.innerHTML = "Hi there";
22-
};
23-
24-
components.registerElement("my-greeting", { prototype: StaticElement });
19+
class StaticElement extends components.HTMLElement {
20+
connectedCallback() {
21+
this.innerHTML = "Hi there"
22+
}
23+
}
24+
components.customElements.define("my-greeting", StaticElement);
2525
```
2626

2727
This is very basic, and toy cases like this aren't immediately useful, but this can be helpful for standard
@@ -42,15 +42,15 @@ comeback!
4242
```javascript
4343
var components = require("server-components");
4444

45-
var CounterElement = components.newElement();
4645
var currentCount = 0;
4746

48-
CounterElement.createdCallback = function () {
49-
currentCount += 1;
50-
this.innerHTML = "There have been " + currentCount + " visitors.";
51-
};
52-
53-
components.registerElement("visitor-counter", { prototype: CounterElement });
47+
class CounterElement extends components.HTMLElement {
48+
connectedCallback() {
49+
currentCount += 1;
50+
this.innerHTML = "There have been " + currentCount + " visitors.";
51+
}
52+
}
53+
components.customElements.define("visitor-counter", CounterElement);
5454
```
5555

5656
After a few visitors, this will render `<visitor-counter></visitor-counter>` into something like
@@ -84,14 +84,13 @@ For example, you might want a component that wraps HTML, parses all the text wit
8484
var components = require("server-components");
8585
var linkify = require("linkifyjs/element");
8686

87-
var LinkifyElement = components.newElement();
88-
89-
LinkifyElement.createdCallback = function (document) {
90-
// Delegate the whole thing to a real normal front-end library!
91-
linkify(this, { target: () => null, linkClass: "autolinked" }, document);
92-
};
93-
94-
components.registerElement("linkify-urls", { prototype: LinkifyElement });
87+
class LinkifyElement extends components.HTMLElement {
88+
connectedCallback() {
89+
// Delegate the whole thing to a real front-end library!
90+
linkify(this, { target: () => null, linkClass: "autolinked" }, document);
91+
}
92+
}
93+
components.customElements.define("linkify-urls", LinkifyElement);
9594
```
9695

9796
With this, we can pass HTML into Server Components that looks like

0 commit comments

Comments
 (0)