|
| 1 | +import { useEffect, type RefObject } from 'react'; |
| 2 | +import { isTextInput } from './isTextInput'; |
| 3 | + |
| 4 | +type UseSnapOnFocusOptions = { |
| 5 | + /** Ref to the container element to listen for focus events */ |
| 6 | + containerRef: RefObject<HTMLElement | null>; |
| 7 | + /** Whether the sheet is open */ |
| 8 | + isOpen: boolean; |
| 9 | + /** Current snap point index */ |
| 10 | + currentSnap: number | undefined; |
| 11 | + /** The last (full height) snap point index */ |
| 12 | + lastSnapPointIndex: number; |
| 13 | + /** Whether the hook is enabled */ |
| 14 | + isEnabled: boolean; |
| 15 | + /** Callback to snap to full height, returns a cleanup function to restore */ |
| 16 | + onSnapToFull: (() => VoidFunction) | (() => void); |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Snaps the sheet to full height when an input/textarea inside receives focus. |
| 21 | + * This happens before the keyboard opens, providing a smoother experience. |
| 22 | + */ |
| 23 | +export function useSnapOnFocus({ |
| 24 | + containerRef, |
| 25 | + isOpen, |
| 26 | + currentSnap, |
| 27 | + lastSnapPointIndex, |
| 28 | + isEnabled, |
| 29 | + onSnapToFull, |
| 30 | +}: UseSnapOnFocusOptions) { |
| 31 | + useEffect(() => { |
| 32 | + if (!isOpen) return; |
| 33 | + if (!isEnabled) return; |
| 34 | + |
| 35 | + const container = containerRef.current; |
| 36 | + if (!container) return; |
| 37 | + |
| 38 | + let cleanup: (() => void) | undefined; |
| 39 | + |
| 40 | + const handleFocusIn = (event: FocusEvent) => { |
| 41 | + const target = event.target as HTMLElement | null; |
| 42 | + if (!target) return; |
| 43 | + |
| 44 | + if (!isTextInput(target)) return; |
| 45 | + |
| 46 | + // Already at full height, nothing to do |
| 47 | + if (currentSnap === lastSnapPointIndex) return; |
| 48 | + |
| 49 | + cleanup = onSnapToFull() ?? undefined; |
| 50 | + }; |
| 51 | + |
| 52 | + const handleFocusOut = (event: FocusEvent) => { |
| 53 | + const relatedTarget = event.relatedTarget as HTMLElement | null; |
| 54 | + |
| 55 | + // If focus is moving to another input inside the container, don't restore |
| 56 | + if (relatedTarget && container.contains(relatedTarget)) { |
| 57 | + if (!isTextInput(relatedTarget)) return; |
| 58 | + } |
| 59 | + |
| 60 | + // Focus left the container or moved to a non-input element, restore snap |
| 61 | + cleanup?.(); |
| 62 | + cleanup = undefined; |
| 63 | + }; |
| 64 | + |
| 65 | + container.addEventListener('focusin', handleFocusIn); |
| 66 | + container.addEventListener('focusout', handleFocusOut); |
| 67 | + |
| 68 | + return () => { |
| 69 | + container.removeEventListener('focusin', handleFocusIn); |
| 70 | + container.removeEventListener('focusout', handleFocusOut); |
| 71 | + cleanup?.(); |
| 72 | + }; |
| 73 | + }, [ |
| 74 | + isOpen, |
| 75 | + isEnabled, |
| 76 | + containerRef, |
| 77 | + currentSnap, |
| 78 | + lastSnapPointIndex, |
| 79 | + onSnapToFull, |
| 80 | + ]); |
| 81 | +} |
0 commit comments