Skip to content

Commit ef1c688

Browse files
committed
fix: autoscroll fix for ios
1 parent c39472b commit ef1c688

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/components/home-view.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,20 @@ export function HomeView({ onSelectPlant }: HomeViewProps) {
160160

161161
useEffect(() => {
162162
if (isMobileSearchModeOpen) {
163-
// Small timeout allows the virtual keyboard to begin appearing and the browser
164-
// to do its initial scroll, which we then cleanly override by scrolling to the top.
165-
setTimeout(() => {
166-
window.scrollTo({ top: 0, behavior: 'smooth' });
167-
}, 50);
163+
const resetScroll = () => window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
164+
resetScroll();
165+
166+
// Aggressively counteract iOS Safari's native auto-scroll behavior when virtual keyboard opens
167+
const t1 = setTimeout(resetScroll, 50);
168+
const t2 = setTimeout(resetScroll, 150);
169+
const t3 = setTimeout(resetScroll, 300);
170+
171+
return () => {
172+
clearTimeout(t1);
173+
clearTimeout(t2);
174+
clearTimeout(t3);
175+
};
168176
}
169-
// We purposefully removed the document.body.style.overflow = 'hidden' logic here.
170-
// Dynamic overflow manipulation on iOS with virtual keyboards causes
171-
// erratic viewport scroll jumping. Keeping the body scrollable but overlaying
172-
// the fixed container natively avoids the layout crash bug.
173177
}, [isMobileSearchModeOpen]);
174178

175179
const handleSelectPlant = useCallback(
@@ -351,7 +355,7 @@ export function HomeView({ onSelectPlant }: HomeViewProps) {
351355
onFocus={() => {
352356
if (isPhoneViewport) {
353357
setIsOpen(true);
354-
setTimeout(() => window.scrollTo({ top: 0, behavior: 'smooth' }), 50);
358+
setTimeout(() => window.scrollTo({ top: 0, left: 0, behavior: 'instant' }), 10);
355359
} else if (query.trim().length > 0) {
356360
setIsOpen(true);
357361
}

0 commit comments

Comments
 (0)