Skip to content

Commit 81db73b

Browse files
authored
Merge pull request #43 from github/drop-element-suffix
Feat: Automatically drop Element suffix from controller names
2 parents 4279ccf + 4729b9e commit 81db73b

7 files changed

Lines changed: 68 additions & 59 deletions

File tree

docs/_guide/actions.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ Remember! Actions are _automatically_ bound using the `@controller` decorator. T
2727
<div class="">
2828

2929
```html
30-
<hello-controller>
30+
<hello-world>
3131
<input
32-
data-target="hello-controller.name"
32+
data-target="hello-world.name"
3333
type="text"
3434
>
3535

3636
<button
37-
data-action="click:hello-controller#greet">
37+
data-action="click:hello-world#greet">
3838
Greet
3939
</button>
4040

4141
<span
42-
data-target="hello-controller.output">
42+
data-target="hello-world.output">
4343
</span>
44-
</div>
44+
</hello-world>
4545
```
4646

4747
</div>
@@ -51,7 +51,7 @@ Remember! Actions are _automatically_ bound using the `@controller` decorator. T
5151
import { controller, target } from "@github/catalyst"
5252

5353
@controller
54-
class HelloController extends HTMLElement {
54+
class HelloWorldElement extends HTMLElement {
5555
@target nameTarget: HTMLElement
5656
@target outputTarget: HTMLElement
5757

@@ -78,40 +78,40 @@ The actions syntax follows a pattern of `event:controller#method`.
7878
Multiple actions can be bound to multiple events, methods, and controllers. For example:
7979

8080
```html
81-
<analytics-controller>
82-
<hello-controller>
81+
<analytics-tracking>
82+
<hello-world>
8383
<input
84-
data-target="hello-controller.name"
84+
data-target="hello-world.name"
8585
data-action="
86-
input:hello-controller#validate
87-
blur:hello-controller#validate
88-
focus:analytics-controller#hover
86+
input:hello-world#validate
87+
blur:hello-world#validate
88+
focus:analytics-tracking#hover
8989
"
9090
type="text"
9191
>
9292

9393
<button
9494
data-action="
95-
click:hello-controller#greet
96-
click:analytics-controller#click
97-
hover:analytics-controller#hover
95+
click:hello-world#greet
96+
click:analytics-tracking#click
97+
hover:analytics-tracking#hover
9898
"
9999
>
100100
Greet
101101
</button>
102-
</hello-controller>
103-
</analytics-controller>
102+
</hello-world>
103+
</analytics-tracking>
104104
```
105105

106106
### Custom Events
107107

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-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-load` Controller may dispatch a `loaded` event, once its contents are loaded, and other controllers can listen to this event:
109109

110110
```html
111111
<hover-card disabled>
112-
<lazy-controller data-url="/user/1" data-action="loaded:hover-card#enable">
112+
<lazy-load data-url="/user/1" data-action="loaded:hover-card#enable">
113113
<loading-spinner>
114-
</lazy-controller>
114+
</lazy-load>
115115
</hover-card>
116116
```
117117

@@ -123,7 +123,7 @@ Actions can always be bound to any method that is available on the Controller's
123123
import {controller} from '@github/catalyst'
124124

125125
@controller
126-
class HelloController extends HTMLElement {
126+
class HelloWorldElement extends HTMLElement {
127127

128128
hidden = () => {
129129
console.log('data-action cannot call this hidden method, but other JavaScript can!')
@@ -145,7 +145,7 @@ If you're not using decorators, then you'll need to call `bind(this)` somewhere
145145
```
146146
import {bind} from '@github/catalyst'
147147
148-
class HelloController extends HTMLElement {
148+
class HelloWorldElement extends HTMLElement {
149149
150150
connectedCallback() {
151151
bind(this)

docs/_guide/decorators.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Catalyst comes with the `@controller` decorator. This gets put on top of the cla
1515

1616
```js
1717
@controller
18-
class MyController extends HTMLElement {}
18+
class HelloWorldElement extends HTMLElement {}
1919
```
2020

2121
### Class Field Decorators
2222

2323
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:
2424

2525
```js
26-
class MyController extends HTMLElement {
26+
class HelloWorldElement extends HTMLElement {
2727

2828
@target something
2929

@@ -43,7 +43,7 @@ Catalyst doesn't currently ship with any method decorators, but you might see th
4343

4444

4545
```js
46-
class MyController extends HTMLElement {
46+
class HelloWorldElement extends HTMLElement {
4747

4848
@log
4949
submit() {
@@ -64,7 +64,7 @@ class MyController extends HTMLElement {
6464
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:
6565

6666
```js
67-
class MyController extends HTMLElement {
67+
class HelloWorldElement extends HTMLElement {
6868

6969
@debounce(100)
7070
handleInput() {

docs/_guide/targets.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ To create a Target, use the `@target` decorator on a class field, and add the ma
1515
<div>
1616

1717
```html
18-
<hello-controller>
18+
<hello-world>
1919
<span
20-
data-target="hello-controller.output">
20+
data-target="hello-world.output">
2121
</span>
22-
</div>
22+
</hello-world>
2323
```
2424

2525
</div>
@@ -29,7 +29,7 @@ To create a Target, use the `@target` decorator on a class field, and add the ma
2929
import { controller, target } from "@github/catalyst"
3030

3131
@controller
32-
class HelloController extends HTMLElement {
32+
class HelloWorldElement extends HTMLElement {
3333
@target outputTarget!: HTMLElement
3434

3535
greet() {
@@ -71,18 +71,18 @@ The `@target` decorator will only ever return _one_ element, just like `querySel
7171
Elements can be referenced as multiple targets, and targets may be referenced multiple times within the HTML:
7272

7373
```html
74-
<teammembers-controller>
75-
<userlist-controller>
76-
<user-controller data-target="userlist-controller.user">
77-
<input type="checkbox" data-target="teammembers-controller.readCheckbox">
78-
<input type="checkbox" data-target="teammembers-controller.writeCheckbox">
79-
</user-controller>
80-
<user-controller data-target="userlist-controller.user">
81-
<input type="checkbox" data-target="teammembers-controller.readCheckbox">
82-
<input type="checkbox" data-target="teammembers-controller.writeCheckbox">
83-
</user-controller>
84-
</userlist-controller>
85-
</teammembers-controller>
74+
<team-members>
75+
<user-list>
76+
<user-settings data-target="user-list.user">
77+
<input type="checkbox" data-target="team-members.readCheckbox">
78+
<input type="checkbox" data-target="team-members.writeCheckbox">
79+
</user-settings>
80+
<user-settings data-target="user-list.user">
81+
<input type="checkbox" data-target="team-members.readCheckbox">
82+
<input type="checkbox" data-target="team-members.writeCheckbox">
83+
</user-settings>
84+
</user-list>
85+
</team-members>
8686
```
8787

8888
<br>
@@ -91,7 +91,7 @@ Elements can be referenced as multiple targets, and targets may be referenced mu
9191
import { controller, targets } from "@github/catalyst"
9292

9393
@controller
94-
class HelloController extends HTMLElement {
94+
class HelloWorldElement extends HTMLElement {
9595
@targets readCheckbox!: HTMLElement
9696
@targets writeCheckbox!: HTMLElement
9797

@@ -110,7 +110,7 @@ If you're not using decorators, then you'll need to call `findTarget(this, key)`
110110

111111
```js
112112
import {findTarget, findTargets} from '@github/catalyst'
113-
class MyController extends HTMLElement {
113+
class HelloWorldElement extends HTMLElement {
114114

115115
get outputTarget() {
116116
return findTarget(this, 'outputTarget')

docs/_guide/your-first-component.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ chapter: 2
66
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:
77

88
```html
9-
<my-controller></my-controller>
9+
<hello-world></hello-world>
1010
<script>
11-
class MyController extends HTMLElement {
11+
class HelloWorldElement extends HTMLElement {
1212
connectedCallback() {
1313
this.innerHTML = 'Hello World!'
1414
}
1515
}
16-
window.customElements.register('my-controller', MyController)
16+
window.customElements.register('hello-world', HelloWorldElement)
1717
</script>
1818
```
1919
<br>
@@ -31,27 +31,27 @@ Catalyst saves you writing some of this boilerplate, by automatically calling th
3131

3232
```js
3333
@controller
34-
class MyController extends HTMLElement {
34+
class HelloWorldElement extends HTMLElement {
3535
connectedCallback() {
3636
this.innerHTML = 'Hello World!'
3737
}
3838
}
3939
// No longer need this:
40-
// window.customElements.register('my-controller', MyController)
40+
// window.customElements.register('hello-world', HelloWorldElement)
4141
```
4242
<br>
4343

44-
Catalyst will automatically "dasherize" the class name. All capital letters get lowercased and dash separated.
44+
Catalyst will automatically convert the classes name; removing the trailing `Element` suffix and lowercasing all capital letters, separating them with a dash.
4545

46-
By convention, Catalyst controllers end in `Controller`, but it's not required.
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.
4747

4848
#### What about without Decorators?
4949

5050
If you don't want to use decorators, you can simply wrap the class in a call to `controller`:
5151

5252
```js
5353
controller(
54-
class MyController extends HTMLElement {
54+
class HelloWorldElement extends HTMLElement {
5555
//...
5656
}
5757
)

docs/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ <h1 class="h000-mktg mt-6 mr-6">Catalyse your Web Components</h1>
2828
<div class="d-flex f4">
2929
<div class="mt-4">
3030
{% highlight html %}
31-
<hello-controller>
32-
<input data-target="hello-controller.name" type="text">
31+
<hello-world>
32+
<input data-target="hello-world.name" type="text">
3333

34-
<button data-action="click:hello-controller#greet">
34+
<button data-action="click:hello-world#greet">
3535
Greet
3636
</button>
3737

38-
<span data-target="hello-controller.output">
38+
<span data-target="hello-world.output">
3939
</span>
40-
</hello-controller>
40+
</hello-world>
4141
{% endhighlight %}
4242
</div>
4343
<div class="">
4444
{% highlight js %}
4545
import { controller, target } from "@github/catalyst"
4646

4747
@controller
48-
class HelloController extends HTMLElement {
48+
class HelloWorldElement extends HTMLElement {
4949
@target name!: HTMLElement
5050
@target output!: HTMLElement
5151

src/register.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ interface CustomElement {
1010
* Example: HelloController => hello-controller
1111
*/
1212
export function register(classObject: CustomElement): void {
13-
const name = classObject.name.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase()
13+
const name = classObject.name
14+
.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-')
15+
.replace(/-Element$/, '')
16+
.toLowerCase()
1417
if (!window.customElements.get(name)) {
1518
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1619
// @ts-ignore

test/register.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ describe('register', () => {
2727
ThisIsAnExampleOfADasherisedClassName
2828
)
2929
})
30+
31+
it('automatically drops the `Element` suffix', () => {
32+
class ASuffixedElement {}
33+
register(ASuffixedElement)
34+
expect(window.customElements.get('a-suffixed')).to.equal(ASuffixedElement)
35+
})
3036
})

0 commit comments

Comments
 (0)