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/actions.md
+15-13Lines changed: 15 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,7 +85,7 @@ Multiple actions can be bound to multiple events, methods, and controllers. For
85
85
data-action="
86
86
input:hello-world#validate
87
87
blur:hello-world#validate
88
-
focus:analytics-tracking#hover
88
+
focus:analytics-tracking#focus
89
89
"
90
90
type="text"
91
91
>
@@ -105,32 +105,34 @@ Multiple actions can be bound to multiple events, methods, and controllers. For
105
105
106
106
### Custom Events
107
107
108
-
A Controller may emit custom events, which may be listened to by other Controllers using the same Actions Syntax. There is no extra syntax needed for this. For example a `lazy-load` Controller may dispatch a `loaded` event, once its contents are loaded, and other controllers can listen to this event:
108
+
A Controller may emit custom events, which may be listened to by other Controllers using the same Actions Syntax. There is no extra syntax needed for this. For example a `lazy-loader` Controller might dispatch a `loaded` event, once its contents are loaded, and other controllers can listen to this event:
Actions can always be bound to any method that is available on the Controller's prototype. If you need a method on a class that _must not_ be invoked within Actions, then you can instead use a [_class field_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields) or a [_private class field_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_fields).
121
-
122
118
```js
123
119
import {controller} from'@github/catalyst'
124
120
125
121
@controller
126
-
classHelloWorldElementextendsHTMLElement {
122
+
classLazyLoaderextendsHTMLElement {
127
123
128
-
hidden= () => {
129
-
console.log('data-action cannot call this hidden method, but other JavaScript can!')
Copy file name to clipboardExpand all lines: docs/_guide/decorators.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ class HelloWorldElement extends HTMLElement {}
20
20
21
21
### Class Field Decorators
22
22
23
-
Catalyst comes with the `@target` and `@targets` decorators for more [read about Targets](/guide/targets). These get added on top or to the left of the field name, like so:
23
+
Catalyst comes with the `@target` and `@targets` decorators (for more on these [read the Targets guide section](/guide/targets)). These get added on top or to the left of the field name, like so:
24
24
25
25
```js
26
26
classHelloWorldElementextendsHTMLElement {
@@ -59,9 +59,11 @@ class HelloWorldElement extends HTMLElement {
59
59
}
60
60
```
61
61
62
-
### Function Call Decorators
62
+
### Function Calling Decorators
63
63
64
-
Some decorators are customisable - they get called with additional arguments, just like a function call. An example of this is the `@debounce` decorator in the [`@github/mini-throttle`](https://github.com/github/mini-throttle) package:
64
+
You might see some decorators that look like function calls, and that's because they are! Some decorators allow for customisation; calling with additional arguments. Decorators that expect to be called are generally not interchangeable with the non-call variant, a decorators documentation should tell you how to use it.
65
+
66
+
Catalyst doesn't ship with any decorators that can be called like a function; but an example of one can be found in the `@debounce` decorator in the [`@github/mini-throttle`](https://github.com/github/mini-throttle) package:
An aim of Catalyst is to be as light weight as possible, and so we often avoid including helper functions for otherwise fine code. We also want to keep Catalyst focussed, and so where some helper functions might be reasonable, we recommend judicious use of other small libraries.
7
+
8
+
Here are a few common patterns which we've avoided introducing into the Catalyst code base, and instead encourage you to take the example code and run with that:
9
+
10
+
11
+
### Debouncing or Throttling events
12
+
13
+
Often times you'll want to do something computationally intensive (or network intensive) based on a user event. It's worth throttling the amount of times a function can be called for these events, to prevent saturation of the CPU or network. For this we can use the "debounce" or "throttle" patterns. We recommend using the [`@github/mini-throttle`](https://github.com/github/mini-throttle) library for this, which provides convenient decorators to put on methods which will add throttling to them:
// Adding `@debounce(100)` here means this method will only be called once in a 100ms period.
23
+
@debounce(100)
24
+
search(event:Event) {
25
+
const value =event.currentTarget.value
26
+
// This function is very computationally intensive, so we should run it as little as possible
27
+
this.filterAllItemsWithValue(value)
28
+
}
29
+
30
+
}
31
+
```
32
+
33
+
### Aborting Network Requests
34
+
35
+
When making network requests using `fetch`, based on user input, you can cancel old requests as new ones come in, this is useful for performance as well as UI responsiveness, as old requests that aren't cancelled might complete later than newer ones causing the UI to jump around. Aborting network requests requires you to use [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) (a web platform feature).
36
+
37
+
```typescript
38
+
@controller
39
+
classRemoveSearchElementextendsHTMLElement {
40
+
41
+
#remoteSearchController:AbortController|null
42
+
43
+
async search(event:Event) {
44
+
// Abort the old Request
45
+
this.#remoteSearchController?.abort()
46
+
47
+
// To start making a new request, construct an AbortController
Copy file name to clipboardExpand all lines: docs/_guide/targets.md
+24-14Lines changed: 24 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@ chapter: 5
3
3
subtitle: Querying Descendants
4
4
---
5
5
6
-
One of the three [core patterns](/guide/introduction#three-core-concepts-observe-listen-query) is Querying. In Catalyst, Targets are the preferred way to query. Target use `querySelectorAll` under the hood, but make it a lot simpler to work with.
6
+
One of the three [core patterns](/guide/introduction#three-core-concepts-observe-listen-query) is Querying. In Catalyst, Targets are the preferred way to query. Targets use `querySelectorAll` under the hood, but in a way that makes it a lot simpler to work with.
7
7
8
-
Catalyst Components are really just Web Components, so you could simply use `querySelector` or `querySelectorAll` to select descendants of the element. Targets avoid some of the problems of `querySelector`; they provide a more consistent interfaceand handle nesting intuitively. Targets are also a little more ergonomic to reuse in a class. We'd recommend using Targets over `querySelector` wherever you can.
8
+
Catalyst Components are really just Web Components, so you could simply use `querySelector` or `querySelectorAll` to select descendants of the element. Targets avoid some of the problems of `querySelector`; they provide a more consistent interface, avoid coupling CSS classes or HTML tag names to JS, and they handle subtle issues like nested components. Targets are also a little more ergonomic to reuse in a class. We'd recommend using Targets over `querySelector` wherever you can.
9
9
10
10
To create a Target, use the `@target` decorator on a class field, and add the matching `data-target` attribute to your HTML, like so:
11
11
@@ -66,20 +66,20 @@ Remember! There are two decorators available, `@target` which fetches only one e
66
66
</div>
67
67
</div>
68
68
69
-
The `@target` decorator will only ever return _one_ element, just like `querySelector`. If you want to get multiple Targets, you need the `@targets` decorator which works almost identically, but it'll return an _array_ of _N_elements.
69
+
The `@target` decorator will only ever return _one_ element, just like `querySelector`. If you want to get multiple Targets, you need the `@targets` decorator which works almost identically, but it'll return an _array_ of elements. To put this into types: `@target` returns `Element|undefined` while `@targets` returns `Array<Element>`
70
70
71
71
Elements can be referenced as multiple targets, and targets may be referenced multiple times within the HTML:
@@ -106,7 +116,7 @@ class HelloWorldElement extends HTMLElement {
106
116
107
117
If you're using decorators, then the `@target` and `@targets` decorators will turn the decorated properties into getters.
108
118
109
-
If you're not using decorators, then you'll need to call `findTarget(this, key)` or `findTargets(this, key)` in the getter, for example:
119
+
If you're not using decorators, then you'll need to make a `getter`, and call `findTarget(this, key)` or `findTargets(this, key)` in the getter, for example:
0 commit comments