Skip to content

Commit 8975e8d

Browse files
committed
remove controller or component suffix from registered elements
1 parent 8c600ca commit 8975e8d

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

docs/_guide/conventions.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ subtitle: Conventions
55

66
Catalyst strives for convention over code. Here are a few conventions we recommend when writing Catalyst code:
77

8-
### Use `Element` to suffix your controller class
8+
### Use `Element` or `Component` to suffix your controller class
99

10-
Built in HTML elements all extend from the `HTMLElement` constructor, and are all suffixed with `Element` (for example `HTMLElement`, `SVGElement`, `HTMLInputElement` and so on). Catalyst components should be no different, they should behave as closely to the built-ins as possible.
10+
Built in HTML elements all extend from the `HTMLElement` constructor, and are all suffixed with `Element` (for example `HTMLElement`, `SVGElement`, `HTMLInputElement` and so on). Catalyst components can be suffixed with `Element`, `Component` or `Controller`. We think elements should behave as closely to the built-ins as possible, so we like to use `Element`, but the other suffixes might be useful for symettry with other server side comoponent frameworks such as [ViewComponent](https://viewcomponent.org/).
1111

1212
```typescript
1313
@controller
14-
class UserListElement extends HTMLElement {}
14+
class UserListElement extends HTMLElement {} // `<user-list />`
15+
```
16+
17+
```typescript
18+
@controller
19+
class UserListComponent extends HTMLElement {} // `<user-list />`
1520
```
1621

1722
### The best class-names are two word descriptions

docs/_guide/your-first-component.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class HelloWorldElement extends HTMLElement {
2727

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

30-
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.
30+
Catalyst controllers can end in `Element`, `Controller`, or `Component` and Catalyst will remove this suffix when generating a tag name. Adding one of these suffixes is _not_ required - just convention. All examples in this guide use `Element` suffixed names.
3131

3232
{% capture callout %}
33-
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`
33+
Remember! A class name _must_ include at least two CamelCased words (not including the `Element`, `Controller` or `Component` suffix). One-word elements will raise exceptions. Example of good names: `UserListElement`, `SubTaskController`, `PagerContainerComponent`
3434
{% endcapture %}{% include callout.md %}
3535

3636

src/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {dasherize} from './dasherize.js'
99
* Example: HelloController => hello-controller
1010
*/
1111
export function register(classObject: CustomElementClass): CustomElementClass {
12-
const name = dasherize(classObject.name).replace(/-element$/, '')
12+
const name = dasherize(classObject.name).replace(/-(element|controller|component)$/, '')
1313

1414
try {
1515
window.customElements.define(name, classObject)

test/register.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,16 @@ describe('register', () => {
7070
class FirstSuffixElement extends HTMLElement {}
7171
expect(window.customElements.get('first-suffix')).to.equal(FirstSuffixElement)
7272
})
73+
74+
it('automatically drops the `Controller` suffix', () => {
75+
@register
76+
class SecondSuffixController {}
77+
expect(window.customElements.get('second-suffix')).to.equal(SecondSuffixController)
78+
})
79+
80+
it('automatically drops the `Component` suffix', () => {
81+
@register
82+
class ThirdSuffixComponent {}
83+
expect(window.customElements.get('third-suffix')).to.equal(ThirdSuffixComponent)
84+
})
7385
})

0 commit comments

Comments
 (0)