1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | import { Directive, ElementRef, Input, KeyValueDiffers, Renderer2 } from '@angular/core';
|
---|
9 | /**
|
---|
10 | * @ngModule CommonModule
|
---|
11 | *
|
---|
12 | * @usageNotes
|
---|
13 | *
|
---|
14 | * Set the font of the containing element to the result of an expression.
|
---|
15 | *
|
---|
16 | * ```
|
---|
17 | * <some-element [ngStyle]="{'font-style': styleExp}">...</some-element>
|
---|
18 | * ```
|
---|
19 | *
|
---|
20 | * Set the width of the containing element to a pixel value returned by an expression.
|
---|
21 | *
|
---|
22 | * ```
|
---|
23 | * <some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>
|
---|
24 | * ```
|
---|
25 | *
|
---|
26 | * Set a collection of style values using an expression that returns key-value pairs.
|
---|
27 | *
|
---|
28 | * ```
|
---|
29 | * <some-element [ngStyle]="objExp">...</some-element>
|
---|
30 | * ```
|
---|
31 | *
|
---|
32 | * @description
|
---|
33 | *
|
---|
34 | * An attribute directive that updates styles for the containing HTML element.
|
---|
35 | * Sets one or more style properties, specified as colon-separated key-value pairs.
|
---|
36 | * The key is a style name, with an optional `.<unit>` suffix
|
---|
37 | * (such as 'top.px', 'font-style.em').
|
---|
38 | * The value is an expression to be evaluated.
|
---|
39 | * The resulting non-null value, expressed in the given unit,
|
---|
40 | * is assigned to the given style property.
|
---|
41 | * If the result of evaluation is null, the corresponding style is removed.
|
---|
42 | *
|
---|
43 | * @publicApi
|
---|
44 | */
|
---|
45 | export class NgStyle {
|
---|
46 | constructor(_ngEl, _differs, _renderer) {
|
---|
47 | this._ngEl = _ngEl;
|
---|
48 | this._differs = _differs;
|
---|
49 | this._renderer = _renderer;
|
---|
50 | this._ngStyle = null;
|
---|
51 | this._differ = null;
|
---|
52 | }
|
---|
53 | set ngStyle(values) {
|
---|
54 | this._ngStyle = values;
|
---|
55 | if (!this._differ && values) {
|
---|
56 | this._differ = this._differs.find(values).create();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | ngDoCheck() {
|
---|
60 | if (this._differ) {
|
---|
61 | const changes = this._differ.diff(this._ngStyle);
|
---|
62 | if (changes) {
|
---|
63 | this._applyChanges(changes);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | _setStyle(nameAndUnit, value) {
|
---|
68 | const [name, unit] = nameAndUnit.split('.');
|
---|
69 | value = value != null && unit ? `${value}${unit}` : value;
|
---|
70 | if (value != null) {
|
---|
71 | this._renderer.setStyle(this._ngEl.nativeElement, name, value);
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | this._renderer.removeStyle(this._ngEl.nativeElement, name);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | _applyChanges(changes) {
|
---|
78 | changes.forEachRemovedItem((record) => this._setStyle(record.key, null));
|
---|
79 | changes.forEachAddedItem((record) => this._setStyle(record.key, record.currentValue));
|
---|
80 | changes.forEachChangedItem((record) => this._setStyle(record.key, record.currentValue));
|
---|
81 | }
|
---|
82 | }
|
---|
83 | NgStyle.decorators = [
|
---|
84 | { type: Directive, args: [{ selector: '[ngStyle]' },] }
|
---|
85 | ];
|
---|
86 | NgStyle.ctorParameters = () => [
|
---|
87 | { type: ElementRef },
|
---|
88 | { type: KeyValueDiffers },
|
---|
89 | { type: Renderer2 }
|
---|
90 | ];
|
---|
91 | NgStyle.propDecorators = {
|
---|
92 | ngStyle: [{ type: Input, args: ['ngStyle',] }]
|
---|
93 | };
|
---|
94 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmdfc3R5bGUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9jb21tb24vc3JjL2RpcmVjdGl2ZXMvbmdfc3R5bGUudHMiXSwibmFtZXM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