fix(ui): keep picker results above mobile keyboard
This commit is contained in:
1 parent
40df469fd6
commit
4422e4c387
3 files changed
+97
-8
No files matched your search
@@ -60,11 +60,11 @@ export function TextArea({ className = '', ...rest }) {
|
||||
return <textarea className={'field area ' + className} {...rest} />
|
||||
}
|
||||
|
||||
export function SearchField({ value, onChange, onClear, ...rest }) {
|
||||
export const SearchField = forwardRef(function SearchField({ value, onChange, onClear, ...rest }, ref) {
|
||||
return (
|
||||
<div className="searchf">
|
||||
<Icon name="magnifier" className="lead" />
|
||||
<input className="field" value={value} onChange={onChange} {...rest} />
|
||||
<input ref={ref} className="field" value={value} onChange={onChange} {...rest} />
|
||||
{!!value && (
|
||||
<button className="clear" onClick={onClear} aria-label="Clear">
|
||||
<Icon name="xmark" />
|
||||
@@ -72,7 +72,7 @@ export function SearchField({ value, onChange, onClear, ...rest }) {
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
/* ============================ 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 (
|
||||
<>
|
||||
<h3>{title}</h3>
|
||||
{search && <div style={{ marginBottom: 10 }}>
|
||||
{search && <div className="picker-search">
|
||||
<SearchField value={query} onChange={e => 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} />
|
||||
</div>}
|
||||
<div className="sect-b">
|
||||
{visible.map(o => (
|
||||
<button key={o.value} className="lrow tap" onClick={() => { close(); onChange(o.value) }}>
|
||||
{visible.map((o, index) => (
|
||||
<button key={o.value} ref={index === 0 ? firstVisibleRef : undefined} className="lrow tap" onClick={() => { close(); onChange(o.value) }}>
|
||||
<span className="lrow-m"><span className="lrow-t">{o.label}</span>
|
||||
{o.subtitle && <span className="lrow-s">{o.subtitle}</span>}</span>
|
||||
{o.value === value && <Icon name="check" className="lrow-k" />}
|
||||
|
||||
+11
-1
@@ -698,14 +698,24 @@ h4.sec{
|
||||
position:absolute;left:0;right:0;bottom:0;background:var(--bg-el);
|
||||
border-radius:var(--r-xl) var(--r-xl) 0 0;
|
||||
padding:8px 18px calc(20px + var(--sab));
|
||||
max-height:90vh;overflow-y:auto;-webkit-overflow-scrolling:touch;
|
||||
max-height:90vh;max-height:min(90vh,calc(100dvh - 8px));
|
||||
overflow-y:auto;-webkit-overflow-scrolling:touch;scroll-padding-bottom:calc(20px + var(--sab));
|
||||
animation:sheetup .34s var(--ease);
|
||||
overscroll-behavior:contain;touch-action:pan-y;will-change:transform;
|
||||
}
|
||||
.sheet:has(.picker-search){
|
||||
bottom:var(--picker-keyboard-bottom,0px);
|
||||
max-height:min(90vh,calc(100dvh - 8px),calc(var(--picker-visual-height,100vh) - 8px));
|
||||
}
|
||||
:root[data-theme="light"] .sheet{background:var(--bg)}
|
||||
@keyframes sheetup{from{transform:translateY(100%)}to{transform:none}}
|
||||
.sheet .grab{width:36px;height:5px;border-radius:99px;background:var(--label-4);margin:6px auto 14px}
|
||||
.sheet h3{font-size:20px;font-weight:600;letter-spacing:-.021em;margin-bottom:14px}
|
||||
.picker-search{
|
||||
position:sticky;top:0;z-index:2;margin-bottom:0;padding-bottom:10px;
|
||||
background:var(--bg-el);
|
||||
}
|
||||
:root[data-theme="light"] .picker-search{background:var(--bg)}
|
||||
.center{
|
||||
position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);
|
||||
width:min(84vw,300px);background:var(--bg-el);border-radius:var(--r-lg);padding:20px;
|
||||
|
||||
@@ -320,6 +320,7 @@ describe('Stats exercise progress picker', () => {
|
||||
expect(input).toBeTruthy()
|
||||
expect(input.getAttribute('aria-label')).toBe('Search…')
|
||||
expect(input.getAttribute('placeholder')).toBe('Search…')
|
||||
expect(modal.querySelector('.picker-search')).toBeTruthy()
|
||||
const optionButtons = () => [...modal.querySelectorAll('.sect-b button')]
|
||||
const initialOptionCount = optionButtons().length
|
||||
expect(initialOptionCount).toBe(3)
|
||||
@@ -333,6 +334,24 @@ describe('Stats exercise progress picker', () => {
|
||||
input.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
})
|
||||
|
||||
// A focused search can leave a newly narrowed result below the visual viewport. The
|
||||
// correction must advance the sheet's own scroll position, not the page behind it.
|
||||
const sheet = modal.querySelector('.sheet')
|
||||
const firstOption = optionButtons()[0]
|
||||
Object.defineProperties(sheet, {
|
||||
clientHeight: { configurable: true, value: 400 },
|
||||
scrollHeight: { configurable: true, value: 1000 },
|
||||
})
|
||||
vi.spyOn(sheet, 'getBoundingClientRect').mockReturnValue({ bottom: 600 })
|
||||
vi.spyOn(firstOption, 'getBoundingClientRect').mockReturnValue({ bottom: 500 })
|
||||
Object.defineProperty(window, 'visualViewport', { configurable: true, value: { height: 300 } })
|
||||
input.focus()
|
||||
expect(sheet.style.getPropertyValue('--picker-visual-height')).toBe('300px')
|
||||
expect(sheet.style.getPropertyValue('--picker-keyboard-bottom')).toBe(`${Math.max(0, window.innerHeight - 300)}px`)
|
||||
await setSearch('bench')
|
||||
await tick(100)
|
||||
expect(sheet.scrollTop).toBeGreaterThan(0)
|
||||
|
||||
await setSearch('no such historical exercise')
|
||||
expect(modal.textContent).toContain('No match')
|
||||
expect(optionButtons()).toHaveLength(0)
|
||||
|
||||
Reference in new issue
Block a user