|
2 | 2 |
|
3 | 3 | <!-- end auto-generated rule header --> |
4 | 4 |
|
5 | | -Disallow using `...attributes` with `class` attribute. |
6 | | - |
7 | | -When using `...attributes` (splattributes), any classes passed from the parent component will be automatically merged with the component's own classes. Adding a `class` attribute alongside `...attributes` can lead to confusion about which classes take precedence. |
| 5 | +This rule enforces that when using `...attributes` (spread attributes), you should not also use a `class` attribute. The `...attributes` syntax is used to forward HTML attributes from a parent component to a child component, and it already handles class merging automatically. |
8 | 6 |
|
9 | 7 | ## Examples |
10 | 8 |
|
11 | 9 | This rule **forbids** the following: |
12 | 10 |
|
13 | | -```gjs |
14 | | -<template> |
15 | | - <div ...attributes class='foo'> |
16 | | - content |
17 | | - </div> |
18 | | -</template> |
| 11 | +```hbs |
| 12 | +<div ...attributes class='foo'> |
| 13 | + content |
| 14 | +</div> |
19 | 15 | ``` |
20 | 16 |
|
21 | | -```gjs |
22 | | -<template> |
23 | | - <div class='foo' ...attributes> |
24 | | - content |
25 | | - </div> |
26 | | -</template> |
| 17 | +```hbs |
| 18 | +<div class='foo' ...attributes> |
| 19 | + content |
| 20 | +</div> |
27 | 21 | ``` |
28 | 22 |
|
29 | 23 | This rule **allows** the following: |
30 | 24 |
|
31 | | -```gjs |
32 | | -<template> |
33 | | - <div ...attributes> |
34 | | - content |
35 | | - </div> |
36 | | -</template> |
| 25 | +```hbs |
| 26 | +<div ...attributes> |
| 27 | + content |
| 28 | +</div> |
37 | 29 | ``` |
38 | 30 |
|
39 | | -```gjs |
40 | | -<template> |
41 | | - <div class='foo'> |
42 | | - content |
43 | | - </div> |
44 | | -</template> |
| 31 | +```hbs |
| 32 | +<div class='foo'> |
| 33 | + content |
| 34 | +</div> |
45 | 35 | ``` |
46 | 36 |
|
47 | 37 | ## Why? |
48 | 38 |
|
49 | | -When using `...attributes`, classes are automatically merged from parent components. Using a `class` attribute alongside it creates confusion about which classes take precedence and can lead to unexpected styling behavior. |
| 39 | +When using `...attributes`, any classes passed from the parent component will be automatically merged with the component's own classes. Adding a `class` attribute alongside `...attributes` can lead to confusion about which classes take precedence and may result in unexpected styling behavior. |
| 40 | + |
| 41 | +For example: |
| 42 | + |
| 43 | +```hbs |
| 44 | +{{! Parent component }} |
| 45 | +<MyComponent class='parent-class' /> |
| 46 | +
|
| 47 | +{{! MyComponent template }} |
| 48 | +<div ...attributes class='child-class'> |
| 49 | + {{! This is confusing: which class takes precedence? }} |
| 50 | +</div> |
| 51 | +``` |
| 52 | + |
| 53 | +Instead, you should either: |
| 54 | + |
| 55 | +1. Use `...attributes` alone to allow class merging from the parent |
| 56 | +2. Use `class` alone if you want to enforce specific classes |
50 | 57 |
|
51 | 58 | ## References |
52 | 59 |
|
53 | | -- [Ember.js Guides - Splattributes](https://guides.emberjs.com/release/components/component-arguments-and-html-attributes/#toc_html-attributes) |
| 60 | +- [Splattributes](https://guides.emberjs.com/release/components/component-arguments-and-html-attributes/#toc_html-attributes) in the Ember.js guides |
0 commit comments