Skip to content

Commit 32f05d9

Browse files
wagnermacielmmalerba
authored andcommitted
docs: add missing standards for building components (#22412)
* docs: add missing standards for building components * Naming conventions for the selectors of components & directives * The tests & public APIs for MDC components should mirror the non-MDC version * Public properties should use coercion * fixup! docs: add missing standards for building components * fixup! docs: add missing standards for building components * fixup! docs: add missing standards for building components (cherry picked from commit 60946cc)
1 parent 179ce89 commit 32f05d9

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

CODING_STANDARDS.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,86 @@ activateRipple() {
244244
}
245245
```
246246

247+
##### Selectors
248+
* Component selectors should be lowercase and delimited by hyphens. Components should use element
249+
selectors except when the component API uses a native HTML element.
250+
* Directive selectors should be camel cased. Exceptions may be made for directives that act like a
251+
component but would have an empty template, or when the directive is intended to match some
252+
existing attribute.
253+
247254
#### Inheritance
248255

249256
Avoid using inheritance to apply reusable behaviors to multiple components. This limits how many
250257
behaviors can be composed. Instead, [TypeScript mixins][ts-mixins] can be used to compose multiple
251258
common behaviors into a single component.
252259

260+
#### MDC checks
261+
To ensure backwards compatability, we check that tests written for MDC Components include all of
262+
the same tests that the non-MDC version had. Similarly, we check that the public API of MDC
263+
Components match that of the non-MDC version.
264+
265+
In the case where old tests no longer make sense and should be omitted or the public API should be
266+
changed, you can do so in [scripts/check-mdc-tests-config.ts](https://github.com/angular/components/blob/master/scripts/check-mdc-tests-config.ts) and
267+
[scripts/check-mdc-exports-config.ts](https://github.com/angular/components/blob/master/scripts/check-mdc-exports-config.ts).
268+
Remember to leave a comment explaining why the change was necessary.
269+
270+
#### Coercion
271+
Component and directive inputs for boolean and number values must use a setter to coerce values to
272+
the expected type using cdk/coercion.
273+
For example:
274+
```ts
275+
@Input() disabled: boolean;
276+
get disabled(): boolean { return this._disabled; }
277+
set disabled(v: boolean) { this._disabled = coerceBooleanProperty(v); }
278+
private _disabled = false;
279+
280+
...
281+
282+
static ngAcceptInputType_value: BooleanInput;
283+
```
284+
The above code allows users to set `disabled` similar to how it can be set on native inputs:
285+
```html
286+
<component disabled></component>
287+
```
288+
Even though an empty string is technically what is being provided as the value of `disabled`,
289+
`ngAcceptInputType` allows the mismatched type to be provided and `coerceBooleanProperty`
290+
interprets the given value (an empty string) to the correct type & value, which in this case would
291+
be `true`.
292+
293+
#### Expose native inputs
294+
Native inputs used in components should be exposed to developers through `ng-content`. This allows
295+
developers to interact directly with the input and allows us to avoid providing custom
296+
implementations for all of the input's native behaviors.
297+
298+
For example:
299+
300+
**Do:**
301+
302+
Implementation
303+
```html
304+
<ng-content></ng-content>
305+
```
306+
307+
Usage
308+
```html
309+
<your-component>
310+
<input>
311+
</your-component>
312+
```
313+
314+
**Don't:**
315+
316+
Implementation
317+
```html
318+
<input>
319+
```
320+
321+
Usage
322+
```html
323+
<component></component>
324+
```
325+
326+
253327
### Angular
254328

255329
#### Host bindings
@@ -354,3 +428,21 @@ When it is not super obvious, include a brief description of what a class repres
354428
```
355429

356430
[ts-mixins]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-mix-in-classes
431+
432+
#### Prefer CSS classes to tag names and attributes for styling
433+
Targeting tag names can cause conflicts with the MDC version of the component. For this reason, use
434+
CSS class names defined by us instead of tag names. We also prefer classes over attributes for
435+
consistency.
436+
```scss
437+
/** Do: */
438+
.mat-mdc-slider { ... }
439+
440+
/** Don't: */
441+
mdc-slider { ... }
442+
443+
/** Do: */
444+
.mat-mdc-slider-input { ... }
445+
446+
/** Don't: */
447+
input[type="button"] { ... }
448+
```

0 commit comments

Comments
 (0)