-
Notifications
You must be signed in to change notification settings - Fork 5
android keyboard bug fix #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
android keyboard bug fix #1638
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,200 +1,93 @@ | ||
| "use client" | ||
|
|
||
| import { useEffect, useState, useCallback, useRef } from 'react'; | ||
| import { useEffect, useState, useCallback, type CSSProperties } from 'react'; | ||
| import { Capacitor } from '@capacitor/core'; | ||
| import { Keyboard } from '@capacitor/keyboard'; | ||
|
|
||
| // Debounce utility (simple implementation to avoid lodash dependency) | ||
| function debounce<T extends (...args: any[]) => any>( | ||
| func: T, | ||
| wait: number | ||
| ): (...args: Parameters<T>) => void { | ||
| let timeout: NodeJS.Timeout; | ||
| return (...args: Parameters<T>) => { | ||
| clearTimeout(timeout); | ||
| timeout = setTimeout(() => func(...args), wait); | ||
| }; | ||
| } | ||
|
|
||
| interface AndroidKeyboardHookReturn { | ||
| keyboardHeight: number; | ||
| isVisible: boolean; | ||
| isAndroid: boolean; | ||
| getContainerStyle: (baseStyle?: React.CSSProperties, adjustment?: number) => React.CSSProperties; | ||
| getContainerStyle: (baseStyle?: CSSProperties, adjustment?: number) => CSSProperties; | ||
| } | ||
|
|
||
| export function useAndroidKeyboard(debounceMs: number = 50): AndroidKeyboardHookReturn { | ||
| // All hooks must be called before any conditional logic | ||
| export function useAndroidKeyboard(): AndroidKeyboardHookReturn { | ||
| const [keyboardHeight, setKeyboardHeight] = useState(0); | ||
| const [isVisible, setIsVisible] = useState(false); | ||
| const initialHeightRef = useRef(typeof window !== 'undefined' ? window.innerHeight : 0); | ||
|
|
||
| // SSR safety check after hooks are declared | ||
| const isSSR = typeof window === 'undefined'; | ||
| const isAndroid = !isSSR && Capacitor.getPlatform() === 'android'; | ||
|
|
||
| // Recalculate initial height (for orientation changes) | ||
| const resetInitialHeight = useCallback(() => { | ||
| if (isSSR) return; | ||
| initialHeightRef.current = window.innerHeight; | ||
| }, [isSSR]); | ||
|
|
||
| const detectKeyboard = useCallback(() => { | ||
| if (isSSR) return; | ||
|
|
||
| const currentHeight = window.innerHeight; | ||
| const initialHeight = initialHeightRef.current; | ||
| let height = 0; | ||
|
|
||
| // Method 1: VisualViewport API (most reliable) | ||
| if (window.visualViewport) { | ||
| height = initialHeight - window.visualViewport.height; | ||
| } | ||
| // Method 2: Window resize fallback | ||
| else { | ||
| height = initialHeight - currentHeight; | ||
| } | ||
|
|
||
| // Only consider it a keyboard if height difference is significant | ||
| if (height > 50) { | ||
| setKeyboardHeight(height); | ||
| setIsVisible(true); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', `${height}px`); | ||
| } else { | ||
| setKeyboardHeight(0); | ||
| setIsVisible(false); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', '0px'); | ||
| } | ||
| }, [isSSR]); | ||
|
|
||
| // Debounced version to prevent flooding React with state updates | ||
| const debouncedDetectKeyboard = useRef(debounce(detectKeyboard, debounceMs)).current; | ||
|
|
||
| const handleOrientationChange = useCallback(() => { | ||
| if (isSSR) return; | ||
| resetInitialHeight(); | ||
| // Use immediate detection for orientation changes (not debounced) | ||
| detectKeyboard(); | ||
| }, [isSSR, resetInitialHeight, detectKeyboard]); | ||
|
|
||
| // Simplified focus handlers - remove double state setting | ||
| const handleFocusIn = useCallback((e: FocusEvent) => { | ||
| if (isSSR) return; | ||
|
|
||
| const target = e.target as HTMLElement; | ||
| if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.contentEditable === 'true')) { | ||
| // Only use fallback if plugin isn't available | ||
| if (!Capacitor.isPluginAvailable('Keyboard')) { | ||
| const fallbackHeight = Math.floor(window.innerHeight * 0.35); | ||
| setKeyboardHeight(fallbackHeight); | ||
| setIsVisible(true); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', `${fallbackHeight}px`); | ||
| } | ||
| } | ||
| }, [isSSR]); | ||
|
|
||
| const handleFocusOut = useCallback(() => { | ||
| if (isSSR) return; | ||
|
|
||
| // Immediately clear keyboard state when focus leaves input | ||
| setKeyboardHeight(0); | ||
| setIsVisible(false); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', '0px'); | ||
| }, [isSSR]); | ||
|
|
||
| const handleResize = useCallback(() => { | ||
| if (isSSR) return; | ||
| debouncedDetectKeyboard(); | ||
| }, [isSSR, debouncedDetectKeyboard]); | ||
|
|
||
| const handleVisualViewportResize = useCallback(() => { | ||
| if (isSSR) return; | ||
| debouncedDetectKeyboard(); | ||
| }, [isSSR, debouncedDetectKeyboard]); | ||
|
|
||
| useEffect(() => { | ||
| if (isSSR || !isAndroid) return; | ||
|
|
||
| let keyboardShowCleanup: (() => void) | undefined; | ||
| let keyboardHideCleanup: (() => void) | undefined; | ||
|
|
||
| // Setup Capacitor keyboard listeners (if available) | ||
| const setupCapacitorKeyboard = async () => { | ||
| if (Capacitor.isPluginAvailable('Keyboard')) { | ||
| try { | ||
| const keyboardWillShowListener = await Keyboard.addListener('keyboardWillShow', (info) => { | ||
| const height = info.keyboardHeight || 300; | ||
| setKeyboardHeight(height); | ||
| setIsVisible(true); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', `${height}px`); | ||
| }); | ||
|
|
||
| const keyboardWillHideListener = await Keyboard.addListener('keyboardWillHide', () => { | ||
| setKeyboardHeight(0); | ||
| setIsVisible(false); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', '0px'); | ||
| }); | ||
|
|
||
| keyboardShowCleanup = () => keyboardWillShowListener.remove(); | ||
| keyboardHideCleanup = () => keyboardWillHideListener.remove(); | ||
| } catch (error) { | ||
| console.error('[Android Keyboard] Error setting up Capacitor Keyboard:', error); | ||
| let mounted = true; | ||
| let showCleanup: (() => void) | undefined; | ||
| let hideCleanup: (() => void) | undefined; | ||
|
|
||
| const setupKeyboardListeners = async () => { | ||
| if (!Capacitor.isPluginAvailable('Keyboard')) return; | ||
|
|
||
| try { | ||
| const showListener = await Keyboard.addListener('keyboardWillShow', (info) => { | ||
| const height = info.keyboardHeight ?? 300; | ||
| setKeyboardHeight(height); | ||
| setIsVisible(true); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', `${height}px`); | ||
| }); | ||
|
|
||
| const hideListener = await Keyboard.addListener('keyboardWillHide', () => { | ||
| setKeyboardHeight(0); | ||
| setIsVisible(false); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', '0px'); | ||
| }); | ||
|
|
||
| // If unmounted during async setup, remove listeners immediately | ||
| if (!mounted) { | ||
| showListener.remove(); | ||
| hideListener.remove(); | ||
| return; | ||
| } | ||
|
|
||
| showCleanup = () => showListener.remove(); | ||
| hideCleanup = () => hideListener.remove(); | ||
| } catch (error) { | ||
| console.error('[Android Keyboard] Error setting up listeners:', error); | ||
| } | ||
| }; | ||
|
|
||
| // Add event listeners with proper cleanup references | ||
| if (window.visualViewport) { | ||
| window.visualViewport.addEventListener('resize', handleVisualViewportResize); | ||
| } | ||
|
|
||
| window.addEventListener('resize', handleResize); | ||
| window.addEventListener('orientationchange', handleOrientationChange); | ||
| document.addEventListener('focusin', handleFocusIn); | ||
| document.addEventListener('focusout', handleFocusOut); | ||
|
|
||
| // Setup Capacitor keyboard listeners | ||
| setupCapacitorKeyboard(); | ||
|
|
||
| // Initial measurement | ||
| resetInitialHeight(); | ||
| detectKeyboard(); | ||
| setupKeyboardListeners(); | ||
|
|
||
| return () => { | ||
| // Cleanup Capacitor listeners | ||
| keyboardShowCleanup?.(); | ||
| keyboardHideCleanup?.(); | ||
|
|
||
| // Cleanup DOM listeners (using same function references) | ||
| if (window.visualViewport) { | ||
| window.visualViewport.removeEventListener('resize', handleVisualViewportResize); | ||
| } | ||
| window.removeEventListener('resize', handleResize); | ||
| window.removeEventListener('orientationchange', handleOrientationChange); | ||
| document.removeEventListener('focusin', handleFocusIn); | ||
| document.removeEventListener('focusout', handleFocusOut); | ||
| mounted = false; | ||
| showCleanup?.(); | ||
| hideCleanup?.(); | ||
| document.documentElement.style.setProperty('--android-keyboard-height', '0px'); | ||
| }; | ||
| }, [isSSR, isAndroid, handleVisualViewportResize, handleResize, handleOrientationChange, handleFocusIn, handleFocusOut, resetInitialHeight, detectKeyboard]); | ||
| }, [isSSR, isAndroid]); | ||
|
|
||
| // Centralized container style for keyboard adjustments | ||
| const getContainerStyle = useCallback(( | ||
| baseStyle: React.CSSProperties = {}, | ||
| baseStyle: CSSProperties = {}, | ||
| adjustment: number = 40 | ||
| ): React.CSSProperties => { | ||
| ): CSSProperties => { | ||
| if (isSSR || !isAndroid || !isVisible || keyboardHeight <= 0) { | ||
| return { | ||
| ...baseStyle, | ||
| transition: 'transform 0.1s ease-out', | ||
| transition: baseStyle.transition ?? 'transform 0.1s ease-out', | ||
| }; | ||
| } | ||
|
|
||
| const adjustedTransform = Math.max(0, keyboardHeight - adjustment); | ||
| const baseTransform = baseStyle.transform ?? ''; | ||
| const translateY = adjustedTransform > 0 ? `translateY(-${adjustedTransform}px)` : ''; | ||
| const combinedTransform = `${baseTransform} ${translateY}`.trim(); | ||
|
|
||
| return { | ||
| ...baseStyle, | ||
| transform: `translateY(-${adjustedTransform}px)`, | ||
| transition: 'transform 0.1s ease-out', | ||
| transform: combinedTransform || undefined, | ||
| transition: baseStyle.transition ?? 'transform 0.1s ease-out', | ||
| }; | ||
| }, [isSSR, isAndroid, isVisible, keyboardHeight]); | ||
|
|
||
| return { keyboardHeight, isVisible, isAndroid, getContainerStyle }; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.