Skip to content

Commit fa18fe9

Browse files
committed
docs: rewrite Your-First-Component
Based on user feedback this section has been re-written for a few reasons: - It focusses more on the `@controller` decorator and less on Custom Elements - It repeats (multiple times, including a call-out box) that the class name must consist of two words, as this is something users miss.
1 parent 68968b2 commit fa18fe9

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

docs/_guide/conventions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class UserListElement extends HTMLElement {}
1616

1717
### The best class-names are two word descriptions
1818

19-
Custom elements are required to have a `-` inside the tag name. Catalyst's `@controller` will derive the tag name from the class name - and so as such the class name needs to have at least two capital letters, or to put it another way, it needs to consist of two words. The element name should describe what it does succinctly in two words. Some examples:
19+
Custom elements are required to have a `-` inside the tag name. Catalyst's `@controller` will derive the tag name from the class name - and so as such the class name needs to have at least two capital letters, or to put it another way, it needs to consist of at least two CamelCased words. The element name should describe what it does succinctly in two words. Some examples:
2020

2121
- `theme-picker` (`class ThemePickerElement`)
2222
- `markdown-toolbar` (`class MarkdownToolbarElement`)

docs/_guide/your-first-component.md

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,64 @@ subtitle: Building an HTMLElement
33
chapter: 2
44
---
55

6-
Custom Elements allow you to create reusable components that you can declare in HTML, and [progressively enhance](https://en.wikipedia.org/wiki/Progressive_enhancement) within JavaScript. Custom Elements must named with a `-` in the HTML name, and the JS class must `extend HTMLElement`. When the browser connects each element class instance to the DOM node, `connectedCallback` is fired - this is where you can change parts of the element. Here's a basic example:
6+
### Catalyst's `@controller` decorator
77

8-
```html
9-
<hello-world></hello-world>
10-
<script>
8+
Catalyst's `@controller` decorator lets you create Custom Elements with virtually no boilerplate, by automatically calling `customElements.register`, and by adding ["Actions"](/guide/actions) and ["Targets"](/guide/targets) features described later. Using TypeScript (with `decorators` support enabled), simply add `@controller` to the top of your class:
9+
10+
```js
11+
@controller
1112
class HelloWorldElement extends HTMLElement {
1213
connectedCallback() {
1314
this.innerHTML = 'Hello World!'
1415
}
1516
}
16-
window.customElements.register('hello-world', HelloWorldElement)
17-
</script>
17+
// This happens automatically within `@controller`
18+
// window.customElements.register('hello-world', HelloWorldElement)
1819
```
1920
<br>
2021

22+
Catalyst will automatically convert the classes name; removing the trailing `Element` suffix and lowercasing all capital letters, separating them with a dash.
2123

22-
Here are the three key elements to remember:
24+
By convention, Catalyst controllers end in `Element`, and Catalyst will strip this for the tag name, but the `Element` suffix is not required - just convention. All examples in this guide use `Element` suffixed names.
2325

24-
- Custom Elements must have a dash in the name.
25-
- The JS class must `extend HTMLElement`
26-
- `connectedCallback` can be used as a life-cycle hook for when the element and class are connected.
26+
<div class="d-flex border rounded-1 my-3 box-shadow-medium">
27+
<span class="d-flex bg-blue text-white rounded-left-1 p-3">
28+
<svg width="24" viewBox="0 0 14 16" class="octicon octicon-info" aria-hidden="true">
29+
<path
30+
fill-rule="evenodd"
31+
d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"
32+
/>
33+
</svg>
34+
</span>
35+
<div class="p-3">
36+
37+
Remember! A class name _must_ include at least two CamelCased words (not including the `Element` suffix). One-word elements will raise exceptions. Example of good names: `UserListElement`, `SubTaskElement`, `PagerContainerElement`
2738

28-
### Catalyst
39+
</div>
40+
</div>
2941

30-
Catalyst saves you writing some of this boilerplate, by automatically calling the `customElements.register` code, and by adding ["Actions"](/guide/actions) and ["Targets"](/guide/targets) features described later. If you're using TypeScript with `decorators` support, simply add `@controller` to the top of your class:
3142

32-
```js
33-
@controller
43+
### What does `@controller` do?
44+
45+
Catalyst components are really just "Custom Elements", they're doing all the heavy lifting. Custom Elements allow you to create reusable components that you can declare in HTML, and [progressively enhance](https://en.wikipedia.org/wiki/Progressive_enhancement) within JavaScript. Custom Elements must named with a `-` in the HTML name, and the JS class must `extend HTMLElement`. When the browser connects each element class instance to the DOM node, `connectedCallback` is fired - this is where you can change parts of the element. Here's a basic example:
46+
47+
```html
48+
<hello-world></hello-world>
49+
<script>
3450
class HelloWorldElement extends HTMLElement {
3551
connectedCallback() {
3652
this.innerHTML = 'Hello World!'
3753
}
3854
}
39-
// No longer need this:
40-
// window.customElements.register('hello-world', HelloWorldElement)
55+
window.customElements.register('hello-world', HelloWorldElement)
56+
</script>
4157
```
42-
<br>
43-
44-
Catalyst will automatically convert the classes name; removing the trailing `Element` suffix and lowercasing all capital letters, separating them with a dash.
4558

46-
By convention, Catalyst controllers end in `Element`, and Catalyst will strip this for the tag name, but suffixing `Element` is not required. All examples in this guide use `Element` suffixed names.
59+
The Catalyst version isn't all that different, it's just that we have the `@controller` decorator to save on some of the boilerplate.
4760

48-
#### What about without Decorators?
61+
### What about without TypeScript Decorators?
4962

50-
If you don't want to use decorators, you can simply wrap the class in a call to `controller`:
63+
If you don't want to use TypeScript decorators, you can use `controller` as a regular function, and just pass it your class:
5164

5265
```js
5366
controller(

0 commit comments

Comments
 (0)