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
Copy file name to clipboardExpand all lines: docs/_guide/your-first-component.md
+11-7Lines changed: 11 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,13 @@ chapter: 2
8
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
9
10
10
```js
11
+
import {controller} from'@github/catalyst'
11
12
@controller
12
13
classHelloWorldElementextendsHTMLElement {
13
14
connectedCallback() {
14
15
this.innerHTML='Hello World!'
15
16
}
16
17
}
17
-
// This happens automatically within `@controller`
@@ -42,18 +41,23 @@ Remember! A class name _must_ include at least two CamelCased words (not includi
42
41
43
42
### What does `@controller` do?
44
43
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 be named with a `-` in the tag name, and the JS class must `extends 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:
46
45
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 your class, and the newly tag name.
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 opt to do this all manaully. For example here's the above `HelloWorldElement`, written without the `@controller` annotation:
0 commit comments