Skip to content

Commit 3efb060

Browse files
crisbetoalxhub
authored andcommitted
fix(ivy): unable to bind style zero (#32994)
Fixes not being able to bind zero as a value in style bindings. Fixes #32984. PR Close #32994
1 parent c61e4d7 commit 3efb060

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

packages/core/src/render3/styling/bindings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ export function setStylingMapsSyncFn(fn: SyncStylingMapsFn) {
726726
export const setStyle: ApplyStylingFn =
727727
(renderer: Renderer3 | null, native: RElement, prop: string, value: string | null) => {
728728
if (renderer !== null) {
729-
if (value) {
729+
// Use `isStylingValueDefined` to account for falsy values that should be bound like 0.
730+
if (isStylingValueDefined(value)) {
730731
// opacity, z-index and flexbox all have number values
731732
// and these need to be converted into strings so that
732733
// they can be assigned properly.

packages/core/src/render3/util/styling_utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export function hasValueChanged(
180180
/**
181181
* Determines whether the provided styling value is truthy or falsy.
182182
*/
183-
export function isStylingValueDefined(value: any) {
183+
export function isStylingValueDefined<T extends string|number|{}|null>(value: T):
184+
value is NonNullable<T> {
184185
// the reason why null is compared against is because
185186
// a CSS class value that is set to `false` must be
186187
// respected (otherwise it would be treated as falsy).

packages/core/test/acceptance/styling_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ describe('styling', () => {
165165
expect(fixture.nativeElement.innerHTML).toBe('<div></div>');
166166
});
167167

168+
it('should be able to bind zero', () => {
169+
@Component({template: '<div #div [style.opacity]="opacity"></div>'})
170+
class App {
171+
@ViewChild('div') div !: ElementRef<HTMLElement>;
172+
opacity = 0;
173+
}
174+
175+
TestBed.configureTestingModule({declarations: [App]});
176+
const fixture = TestBed.createComponent(App);
177+
fixture.detectChanges();
178+
179+
expect(fixture.componentInstance.div.nativeElement.style.opacity).toBe('0');
180+
});
181+
168182
it('should be able to bind a SafeValue to backgroundImage', () => {
169183
@Component({template: '<div [style.backgroundImage]="image"></div>'})
170184
class Cmp {

0 commit comments

Comments
 (0)