Skip to content

Commit cd442ab

Browse files
authored
Merge pull request #47 from github/docs-rewrite-your-first-component
docs: rewrite Your-First-Component
2 parents f8343ad + d28b799 commit cd442ab

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

docs/_guide/your-first-component.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ chapter: 2
88
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:
99

1010
```js
11+
import {controller} from '@github/catalyst'
1112
@controller
1213
class HelloWorldElement extends HTMLElement {
1314
connectedCallback() {
1415
this.innerHTML = 'Hello World!'
1516
}
1617
}
17-
// This happens automatically within `@controller`
18-
// window.customElements.register('hello-world', HelloWorldElement)
1918
```
2019
<br>
2120

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

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.
23+
By convention Catalyst controllers end in `Element`; Catalyst will omit this when generating a tag name. The `Element` suffix is _not_ required - just convention. All examples in this guide use `Element` suffixed names.
2524

2625
<div class="d-flex border rounded-1 my-3 box-shadow-medium">
2726
<span class="d-flex bg-blue text-white rounded-left-1 p-3">
@@ -42,18 +41,23 @@ Remember! A class name _must_ include at least two CamelCased words (not includi
4241

4342
### What does `@controller` do?
4443

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:
44+
The `@controller` decorator doesn't do all that much. Catalyst components are just "Custom Elements" under the hood, and the `@controller` decorator saves you writing some boilerplate that you'd otherwise have to write by hand. Specifically the `@controller` decorator:
4645

47-
```html
48-
<hello-world></hello-world>
49-
<script>
46+
- Derives a tag name based on your class name, removing the trailing `Element` suffix and lowercasing all capital letters, separating them with a dash.
47+
- Calls `window.customElements.register` with the newly derived tag name and your class.
48+
- Injects a call to `bind(this)` inside of the `connectedCallback()` of your class; this ensures that as your element connects it picks up any `data-action` handlers. See [actions](/guide/actions) for more on this.
49+
50+
You can do all of this manually; for example here's the above `HelloWorldElement`, written without the `@controller` annotation:
51+
52+
```js
53+
import {bind} from '@github/catalyst'
5054
class HelloWorldElement extends HTMLElement {
5155
connectedCallback() {
56+
bind(this)
5257
this.innerHTML = 'Hello World!'
5358
}
5459
}
5560
window.customElements.register('hello-world', HelloWorldElement)
56-
</script>
5761
```
5862

5963
The Catalyst version isn't all that different, it's just that we have the `@controller` decorator to save on some of the boilerplate.

0 commit comments

Comments
 (0)