Skip to content

Commit 69b3c05

Browse files
JeanMechemmalerba
authored andcommitted
docs: add mentions host style bindings (angular#64257)
Also adds a section about css custom properties fixes angular#64256 PR Close angular#64257
1 parent dd7bb90 commit 69b3c05

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

adev/shared-docs/pipeline/shared/linking.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// In some case we know that we don't want to link a symbol
1010
// Example when there is a conflict between API entries and compiler features.
1111
// eg: "animate" is both an Animation API entry and an template instruction "animation.enter"
12-
const LINK_EXEMPT = new Set(['animate', 'animate.enter', 'animate.leave']);
12+
// or "style" is a generic term but also an Animation API entry.
13+
const LINK_EXEMPT = new Set(['animate', 'animate.enter', 'animate.leave', 'style']);
1314

1415
export function shouldLinkSymbol(symbol: string): boolean {
1516
return !LINK_EXEMPT.has(symbol);

adev/src/content/guide/components/host-elements.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ In the above example, `<profile-photo>` is the host element of the `ProfilePhoto
3737

3838
## Binding to the host element
3939

40-
A component can bind properties, attributes, and events to its host element. This behaves
40+
A component can bind properties, attributes, styles and events to its host element. This behaves
4141
identically to bindings on elements inside the component's template, but instead defined with
4242
the `host` property in the `@Component` decorator:
4343

@@ -48,6 +48,7 @@ the `host` property in the `@Component` decorator:
4848
'role': 'slider',
4949
'[attr.aria-valuenow]': 'value',
5050
'[class.active]': 'isActive()',
51+
'[style.background] : `hasError() ? 'red' : 'green'`,
5152
'[tabIndex]': 'disabled ? -1 : 0',
5253
'(keydown)': 'updateValue($event)',
5354
},
@@ -56,6 +57,7 @@ export class CustomSlider {
5657
value: number = 0;
5758
disabled: boolean = false;
5859
isActive = signal(false);
60+
hasError = signal(false);
5961
updateValue(event: KeyboardEvent) { /* ... */ }
6062
6163
/* ... */
@@ -98,8 +100,10 @@ export class CustomSlider {
98100
}
99101
```
100102

101-
**Always prefer using the `host` property over `@HostBinding` and `@HostListener`.** These
103+
<docs-callout critical title="Prefer using the `host` property over the decorators">
104+
**Always prefer using the `host` property over `@HostBinding` and `@HostListener`.** These
102105
decorators exist exclusively for backwards compatibility.
106+
</docs-callout>
103107

104108
## Binding collisions
105109

@@ -126,3 +130,36 @@ In cases like this, the following rules determine which value wins:
126130
- If both values are static, the instance binding wins.
127131
- If one value is static and the other dynamic, the dynamic value wins.
128132
- If both values are dynamic, the component's host binding wins.
133+
134+
## Styling with CSS custom properties
135+
136+
Developers often rely on [CSS Custom Properties](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_cascading_variables/Using_CSS_custom_properties) to enable a flexible configuration of their component's styles.
137+
You can set such custom properties on a host element with a [style binding][style binding](guide/templates/binding#css-style-properties).
138+
139+
```angular-ts
140+
@Component({
141+
/* ... */
142+
host: {
143+
'[style.--my-background]': 'color()',
144+
}
145+
})
146+
export class MyComponent {
147+
color = signal('lightgreen');
148+
}
149+
```
150+
151+
In this example, the `--my-background` CSS custom property is bound to the `color` signal. The value of the custom property will automatically update whenever the `color` signal changes. This will affect the current component and all its children that rely on this custom property.
152+
153+
### Setting custom properties on children compoents
154+
155+
Alternatively, it is also possible to set css custom properties on the host element of children components with a [style binding](guide/templates/binding#css-style-properties).
156+
157+
```angular-ts
158+
@Component({
159+
selector: 'my-component',
160+
template: `<my-child [style.--my-background]="color()">`,
161+
})
162+
export class MyComponent {
163+
color = signal('lightgreen');
164+
}
165+
```

packages/core/src/metadata/directives.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,9 @@ export interface HostBindingDecorator {
925925
*
926926
* @usageNotes
927927
*
928+
* NOTE: **Always** prefer using the `host` property over `@HostBinding`.
929+
* This decorator exist exclusively for backwards compatibility.
930+
*
928931
* The following example creates a directive that sets the `valid` and `invalid`
929932
* class, a style color, and an id on the DOM element that has an `ngModel` directive on it.
930933
*

0 commit comments

Comments
 (0)