diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsx
index a65a30c..b9aa100 100644
--- a/frontend/src/components/ui.jsx
+++ b/frontend/src/components/ui.jsx
@@ -60,11 +60,11 @@ export function TextArea({ className = '', ...rest }) {
return
}
-export function SearchField({ value, onChange, onClear, ...rest }) {
+export const SearchField = forwardRef(function SearchField({ value, onChange, onClear, ...rest }, ref) {
return (
-
+
{!!value && (
)
-}
+})
/* ============================ switch ============================ */
@@ -264,18 +264,78 @@ export function SelectRow({ icon, iconTint, title, value, options, onChange, she
function SelectSheet({ title, value, options, onChange, search, close }) {
const [query, setQuery] = useState('')
+ const inputRef = useRef(null)
+ const firstVisibleRef = useRef(null)
+ const syncPickerViewport = useCallback(() => {
+ const input = inputRef.current
+ const sheet = input?.closest('.sheet')
+ const viewport = window.visualViewport
+ if (!sheet || !viewport) return
+ const visualHeight = Math.max(0, viewport.height || window.innerHeight)
+ const visualBottom = (viewport.offsetTop || 0) + visualHeight
+ const bottomInset = Math.max(0, window.innerHeight - visualBottom)
+ sheet.style.setProperty('--picker-keyboard-bottom', `${bottomInset}px`)
+ sheet.style.setProperty('--picker-visual-height', `${visualHeight}px`)
+ }, [])
const matcher = typeof search === 'function' ? search : search?.match
const visible = matcher ? options.filter(o => matcher(o, query)) : options
+
+ useEffect(() => {
+ if (!search) return
+ const viewport = window.visualViewport
+ if (!viewport) return
+ const sync = () => syncPickerViewport()
+ sync()
+ viewport.addEventListener('resize', sync)
+ viewport.addEventListener('scroll', sync)
+ return () => {
+ viewport.removeEventListener('resize', sync)
+ viewport.removeEventListener('scroll', sync)
+ const sheet = inputRef.current?.closest('.sheet')
+ sheet?.style.removeProperty('--picker-keyboard-bottom')
+ sheet?.style.removeProperty('--picker-visual-height')
+ }
+ }, [search, syncPickerViewport])
+
+ // Filtering can leave the first result underneath the mobile keyboard. Keep the
+ // correction local to the sheet instead of letting focus/scrollIntoView move the page.
+ useEffect(() => {
+ if (!query.trim() || !visible.length) return
+ const input = inputRef.current
+ if (!input || document.activeElement !== input) return
+ const schedule = callback => window.requestAnimationFrame
+ ? window.requestAnimationFrame(callback)
+ : window.setTimeout(callback, 0)
+ const cancel = frame => window.cancelAnimationFrame
+ ? window.cancelAnimationFrame(frame)
+ : window.clearTimeout(frame)
+ const frame = schedule(() => {
+ if (document.activeElement !== input) return
+ const option = firstVisibleRef.current
+ const sheet = option?.closest('.sheet')
+ if (!option || !sheet) return
+ const viewport = window.visualViewport
+ const visualBottom = viewport ? (viewport.offsetTop || 0) + viewport.height : window.innerHeight
+ const sheetBottom = sheet.getBoundingClientRect().bottom || visualBottom
+ const hiddenBy = option.getBoundingClientRect().bottom - Math.min(visualBottom, sheetBottom) + 8
+ if (hiddenBy <= 0) return
+ const maxScroll = Math.max(0, sheet.scrollHeight - sheet.clientHeight)
+ const nextScroll = Math.min(maxScroll, Math.max(0, sheet.scrollTop + hiddenBy))
+ if (nextScroll !== sheet.scrollTop) sheet.scrollTop = nextScroll
+ })
+ return () => cancel(frame)
+ }, [query, visible.length])
+
return (
<>
{title}
- {search &&
+ {search &&
setQuery(e.target.value)} onInput={e => setQuery(e.target.value)}
- onClear={() => setQuery('')} placeholder={search.placeholder} aria-label={search.label || search.placeholder} />
+ onClear={() => setQuery('')} onFocus={syncPickerViewport} ref={inputRef} placeholder={search.placeholder} aria-label={search.label || search.placeholder} />
}
- {visible.map(o => (
-