Skip to content

Commit 8341594

Browse files
Merge pull request #5860 from ejmurc:fix-nan-transforms-hidden-field
PiperOrigin-RevId: 825691442
2 parents 7619df0 + 590ae99 commit 8341594

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

field/internal/field.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ export class Field extends LitElement {
276276
return;
277277
}
278278

279+
const keyframes = this.getLabelKeyframes();
280+
if (!keyframes.length) {
281+
return;
282+
}
283+
279284
this.isAnimating = true;
280285
this.labelAnimation?.cancel();
281286

@@ -291,10 +296,10 @@ export class Field extends LitElement {
291296
// Re-calculating the animation each time will prevent any visual glitches
292297
// from appearing.
293298
// TODO(b/241113345): use animation tokens
294-
this.labelAnimation = this.floatingLabelEl?.animate(
295-
this.getLabelKeyframes(),
296-
{duration: 150, easing: EASING.STANDARD},
297-
);
299+
this.labelAnimation = this.floatingLabelEl?.animate(keyframes, {
300+
duration: 150,
301+
easing: EASING.STANDARD,
302+
});
298303

299304
this.labelAnimation?.addEventListener('finish', () => {
300305
// At the end of the animation, update the visible label.
@@ -320,6 +325,10 @@ export class Field extends LitElement {
320325
} = restingLabelEl.getBoundingClientRect();
321326
const floatingScrollWidth = floatingLabelEl.scrollWidth;
322327
const restingScrollWidth = restingLabelEl.scrollWidth;
328+
// If either label has no dimensions (e.g., display: none), skip animation
329+
if (floatingScrollWidth === 0 || restingScrollWidth === 0) {
330+
return [];
331+
}
323332
// Scale by width ratio instead of font size since letter-spacing will scale
324333
// incorrectly. Using the width we can better approximate the adjusted
325334
// scale and compensate for tracking and overflow.

field/internal/field_test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ describe('Field', () => {
341341
// Test case.
342342
const labelValue = 'Label';
343343
const {instance} = await setupTest({
344-
required: true, label: labelValue, noAsterisk: true
344+
required: true,
345+
label: labelValue,
346+
noAsterisk: true,
345347
});
346348
//Assertion
347349
expect(instance.labelText)
@@ -408,4 +410,27 @@ describe('Field', () => {
408410
.toBeTrue();
409411
});
410412
});
413+
414+
describe('label animation', () => {
415+
it('should not produce NaN transforms when populated while hidden', async () => {
416+
const {instance} = await setupTest({label: 'Hidden Label'});
417+
instance.style.display = 'none';
418+
await env.waitForStability();
419+
420+
const floatingLabel =
421+
instance.shadowRoot?.querySelector('.label.floating')!;
422+
expect(floatingLabel).withContext('floating label element').toBeDefined();
423+
const floatingLabelAnimateSpy = spyOn(
424+
floatingLabel,
425+
'animate',
426+
).and.callThrough();
427+
428+
instance.populated = true;
429+
await env.waitForStability();
430+
431+
expect(floatingLabelAnimateSpy)
432+
.withContext('floatingLabel.animate()')
433+
.not.toHaveBeenCalled();
434+
});
435+
});
411436
});

0 commit comments

Comments
 (0)