From 3f255ad185d4ed49d9f6d40022dfb87838ff9afc Mon Sep 17 00:00:00 2001 From: mortezaei Date: Thu, 20 Aug 2026 02:06:13 +0330 Subject: [PATCH] refactor: optimize keyboard height updates and improve viewport coordinate state transitions --- .../question-viewport-coordinator.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/Componentes/question-viewport-coordinator.ts b/src/components/Componentes/question-viewport-coordinator.ts index d363f6d..7f75f0b 100644 --- a/src/components/Componentes/question-viewport-coordinator.ts +++ b/src/components/Componentes/question-viewport-coordinator.ts @@ -3,8 +3,7 @@ import { useEffect } from "react"; import { viewPaddingsBridge } from "@/lib/view-paddings"; -const KEYBOARD_THRESHOLD = 40; -const DEFAULT_KEYBOARD_HEIGHT = 280; +const KEYBOARD_THRESHOLD = 20; const QUESTION_GAP = 16; const LIFT_VARIABLE = "--question-lift-y"; @@ -152,7 +151,10 @@ function scheduleLiftUpdate() { } export function updateQuestionKeyboardHeight(height: number) { - keyboardHeight = Math.max(0, height); + const nextHeight = Math.max(0, height); + if (Math.abs(nextHeight - keyboardHeight) < 1) return; + + keyboardHeight = nextHeight; keyboardVisible = keyboardHeight > KEYBOARD_THRESHOLD; if (keyboardVisible) { lastKeyboardHeight = keyboardHeight; @@ -194,14 +196,8 @@ export function useQuestionViewportCoordinator() { const handleFocusIn = (event: FocusEvent) => { if (!isActiveQuestionKeyboardInput(event.target)) return; activeQuestionInput = event.target; - - // Start with the last known keyboard geometry or standard default so lifting begins in the - // same frame as focus; Flutter/visualViewport then replaces it with the exact height. - if (!keyboardVisible) { - keyboardHeight = - lastKeyboardHeight > KEYBOARD_THRESHOLD - ? lastKeyboardHeight - : DEFAULT_KEYBOARD_HEIGHT; + if (lastKeyboardHeight > KEYBOARD_THRESHOLD) { + keyboardHeight = lastKeyboardHeight; keyboardVisible = true; } scheduleLiftUpdate(); @@ -214,8 +210,12 @@ export function useQuestionViewportCoordinator() { ) ? document.activeElement : null; - if (!activeQuestionInput) scheduleLiftUpdate(); - }, 0); + if (!activeQuestionInput) { + keyboardVisible = false; + keyboardHeight = 0; + scheduleLiftUpdate(); + } + }, 50); }; const unsubscribeConfig = viewPaddingsBridge.subscribeConfig((config) => { @@ -235,7 +235,7 @@ export function useQuestionViewportCoordinator() { lastKeyboardHeight = keyboardHeight; scheduleLiftUpdate(); } - } else if (keyboardHeight <= KEYBOARD_THRESHOLD) { + } else if (reduction <= KEYBOARD_THRESHOLD && keyboardHeight <= KEYBOARD_THRESHOLD) { keyboardVisible = false; keyboardHeight = 0; closedViewportHeight = viewportHeight;