Skip to content

Commit 92597ef

Browse files
committed
fix(hero-amount-input): keep row FLIP positions current after layout changes
Replay the row FLIP tracker when the prefix, its measured width, or the row width changes, and skip animation after a skeleton remount so the next keystroke does not slide from stale em positions.
1 parent cda0664 commit 92597ef

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

‎ui/components/ui/hero-amount-input/hero-amount-input.tsx‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useLayoutEffect, useState } from 'react';
1+
import React, { useLayoutEffect, useRef, useState } from 'react';
22
import {
33
FontFamily,
44
Input,
@@ -124,6 +124,9 @@ export const HeroAmountInput: React.FC<HeroAmountInputProps> = ({
124124
const [isComposing, setIsComposing] = useState(false);
125125
const [amount, setAmount] = useState<HTMLSpanElement | null>(null);
126126
const [rowTracker] = useState(createFlipTracker);
127+
// Set while the row is unmounted (e.g. loading skeleton) so the next play
128+
// records fresh positions instead of sliding from the pre-unmount map.
129+
const skipRowFlipRef = useRef(false);
127130
// The glyph layer draws the digits under the field, whose text is
128131
// transparent, so the field still draws the caret and selection. Reduced
129132
// motion and IME composition show the field's own text instead.
@@ -133,22 +136,37 @@ export const HeroAmountInput: React.FC<HeroAmountInputProps> = ({
133136

134137
// Slides the prefix and amount when a centered row re-centers, like the
135138
// Mobile container's LinearTransition. Positions are kept in em from the
136-
// row's anchor so a font-size step is not read as movement.
139+
// row's anchor so a font-size step is not read as movement. Prefix remesure,
140+
// currency switches, and row resizes must also call play so the map stays
141+
// current; otherwise the next keystroke animates that missed delta too.
137142
useLayoutEffect(() => {
138143
if (!row || !amount) {
144+
skipRowFlipRef.current = true;
139145
return;
140146
}
141147
const glyphs = amount.firstElementChild ?? amount;
148+
const animate = showGlyphs && !skipRowFlipRef.current;
149+
skipRowFlipRef.current = false;
142150
rowTracker.play(
143151
row,
144152
{
145153
origin: align === 'center' ? row.clientWidth / 2 : 0,
146154
scale: getElementFontSize(glyphs),
147155
},
148-
showGlyphs,
156+
animate,
149157
);
150158
syncGlyphScroll(amount, amount.querySelector('input'));
151-
}, [align, amount, row, rowTracker, showGlyphs, value]);
159+
}, [
160+
align,
161+
amount,
162+
prefix,
163+
prefixEm,
164+
row,
165+
rowTracker,
166+
rowWidth,
167+
showGlyphs,
168+
value,
169+
]);
152170

153171
if (isLoading) {
154172
return (

0 commit comments

Comments
 (0)