Skip to content

Commit 590ae99

Browse files
committed
fix(field): prevent NaN transforms when element is hidden
Fixes #5815 When a field's display is set to none, getBoundingClientRect() returns zeros, causing division by zero in getLabelKeyframes(). This resulted in NaN values in transform strings. Added check to skip animation when label dimensions are zero.
1 parent 7619df0 commit 590ae99

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

field/internal/field.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ export class Field extends LitElement {
320320
} = restingLabelEl.getBoundingClientRect();
321321
const floatingScrollWidth = floatingLabelEl.scrollWidth;
322322
const restingScrollWidth = restingLabelEl.scrollWidth;
323+
// If either label has no dimensions (e.g., display: none), skip animation
324+
if (floatingScrollWidth === 0 || restingScrollWidth === 0) {
325+
return [];
326+
}
323327
// Scale by width ratio instead of font size since letter-spacing will scale
324328
// incorrectly. Using the width we can better approximate the adjusted
325329
// scale and compensate for tracking and overflow.

field/internal/field_test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,28 @@ describe('Field', () => {
408408
.toBeTrue();
409409
});
410410
});
411+
412+
describe('label animation', () => {
413+
it('should not produce NaN transforms when populated while hidden', async () => {
414+
const {instance} = await setupTest({label: 'Hidden Label'});
415+
instance.style.display = 'none';
416+
await env.waitForStability();
417+
const animateCalls: unknown[] = [];
418+
spyOn(Element.prototype, 'animate').and.callFake((keyframes, options) => {
419+
animateCalls.push(keyframes);
420+
return new Animation();
421+
});
422+
const consoleErrorSpy = spyOn(console, 'error');
423+
instance.populated = true;
424+
await env.waitForStability();
425+
for (const keyframe of animateCalls) {
426+
const frames = Array.isArray(keyframe) ? keyframe : [keyframe];
427+
for (const frame of frames) {
428+
const transform = (frame as any)?.transform ?? '';
429+
expect(transform).not.toMatch(/NaN/);
430+
}
431+
}
432+
expect(consoleErrorSpy).not.toHaveBeenCalled();
433+
});
434+
});
411435
});

0 commit comments

Comments
 (0)