Phone layout: chip strips no longer scroll the page, sheets stop short of the Dynamic Island, tab bar stays at the bottom after the keyboard
- useRevealActiveChip sets the strip's own scrollLeft instead of scrollIntoView, which walked every scrollable ancestor and jumped the page when the strip sat above the fold; .chips contain overscroll on both axes. - Every .sheet cap (and .center) carries env(safe-area-inset-top); the dead -webkit-overflow-scrolling is gone. - viewport-guard: with the keyboard down and the page displaced, a text field that kept focus (WebKit does not blur on button taps) is blurred and the correction runs; while Modals has the body pinned it unpins for one scroll and pins back where it was. Modals blurs a page field when a sheet opens; ticking a set blurs the row's input. - Android: shortEdges cutout mode so --sat is real, adjustResize for the keyboard.
This commit is contained in:
1 parent
413bce5bf8
commit
d58a98e768
9 files changed
+140
-17
No files matched your search
@@ -47,14 +47,33 @@ export function useSheetKeyboard(inputRef, enabled = true) {
|
||||
}
|
||||
|
||||
/* Chip strips scroll sideways, and a filter set from code ("★ Chosen", a reset) can land the
|
||||
active chip off-screen. Pull it into view along the strip only — block:'nearest' never moves
|
||||
the page when the strip is already visible, which is the case for every strip we have. */
|
||||
active chip off-screen. Pull it into view along the strip only. This used to be
|
||||
scrollIntoView({block:'nearest'}), which also walks every scrollable ancestor: with the
|
||||
strip scrolled above the fold (Library, halfway down the list) a tap on a chip made the
|
||||
whole page jump up to show the row — the "swiping the chips scrolls the page" bug. Setting
|
||||
the strip's own scrollLeft cannot move anything but the strip.
|
||||
|
||||
`pad` mirrors scroll-padding-inline in index.css so a revealed chip is not glued to the edge. */
|
||||
export function revealChip(strip, chip, pad = 16) {
|
||||
if (!strip || !chip || typeof strip.getBoundingClientRect !== 'function' || typeof chip.getBoundingClientRect !== 'function') return false
|
||||
const s = strip.getBoundingClientRect(), c = chip.getBoundingClientRect()
|
||||
const width = strip.clientWidth || s.width || 0
|
||||
if (!width) return false
|
||||
const left = c.left - s.left + (strip.scrollLeft || 0) // chip's offset inside the strip's content
|
||||
const right = left + c.width
|
||||
let next = strip.scrollLeft || 0
|
||||
if (left - pad < next) next = Math.max(0, left - pad)
|
||||
else if (right + pad > next + width) next = right + pad - width
|
||||
if (next === (strip.scrollLeft || 0)) return false
|
||||
strip.scrollLeft = next
|
||||
return true
|
||||
}
|
||||
|
||||
export function useRevealActiveChip(stripRef, active) {
|
||||
useEffect(() => {
|
||||
const strip = stripRef.current
|
||||
const chip = strip?.querySelector?.('.chip.on')
|
||||
if (!chip || typeof chip.scrollIntoView !== 'function') return
|
||||
chip.scrollIntoView({ inline: 'nearest', block: 'nearest' })
|
||||
if (chip) revealChip(strip, chip)
|
||||
}, [stripRef, active])
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { tappable } from './use-sheet-keyboard.js'
|
||||
import { tappable, revealChip } from './use-sheet-keyboard.js'
|
||||
|
||||
const keyEvent = (key, target) => {
|
||||
const ev = { key, target, currentTarget: target, preventDefault: vi.fn() }
|
||||
@@ -38,3 +38,38 @@ describe('tappable', () => {
|
||||
expect(tappable(undefined)).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
// A strip is 300px wide showing content from scrollLeft; chips are 80px wide, 8px apart.
|
||||
const strip = (scrollLeft = 0, width = 300) => ({
|
||||
scrollLeft, clientWidth: width,
|
||||
getBoundingClientRect: () => ({ left: 20, width })
|
||||
})
|
||||
const chipAt = (contentX, strip) => ({
|
||||
getBoundingClientRect: () => ({ left: 20 + contentX - strip.scrollLeft, width: 80 })
|
||||
})
|
||||
|
||||
describe('revealChip', () => {
|
||||
it('scrolls the strip, and only the strip, until the active chip is inside it', () => {
|
||||
const s = strip(0)
|
||||
expect(revealChip(s, chipAt(500, s))).toBe(true)
|
||||
expect(s.scrollLeft).toBe(500 + 80 + 16 - 300) // chip's right edge plus the padding at the right edge
|
||||
const back = strip(400)
|
||||
expect(revealChip(back, chipAt(100, back))).toBe(true)
|
||||
expect(back.scrollLeft).toBe(100 - 16) // padding at the left edge
|
||||
})
|
||||
|
||||
it('leaves a visible chip where it is', () => {
|
||||
const s = strip(100)
|
||||
expect(revealChip(s, chipAt(200, s))).toBe(false)
|
||||
expect(s.scrollLeft).toBe(100)
|
||||
})
|
||||
|
||||
it('never reaches for the page: no scrollIntoView, nothing without a measurable strip', () => {
|
||||
const s = strip(0)
|
||||
const chip = Object.assign(chipAt(900, s), { scrollIntoView: vi.fn() })
|
||||
revealChip(s, chip)
|
||||
expect(chip.scrollIntoView).not.toHaveBeenCalled()
|
||||
expect(revealChip(null, chip)).toBe(false)
|
||||
expect(revealChip({ scrollLeft: 0, clientWidth: 0, getBoundingClientRect: () => ({ left: 0, width: 0 }) }, chip)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -36,12 +36,35 @@ export function keyboardOpen(win = window) {
|
||||
/** Realign the viewports if iOS left them apart. Returns true when a correction was issued. */
|
||||
export function realign(win = window) {
|
||||
if (keyboardOpen(win)) return false
|
||||
if (isText(win.document.activeElement)) return false
|
||||
if (viewportDisplacement(win) <= 1) return false
|
||||
// The keyboard is gone but a text field may still hold focus: on WebKit tapping a <button>
|
||||
// (the set's tick, the RIR cell) does not blur the input, and iOS only puts the viewports
|
||||
// back once focus has left. Nothing is being typed with the keyboard down, so let it go.
|
||||
const active = win.document.activeElement
|
||||
if (isText(active)) active.blur?.()
|
||||
if (bodyPinned(win)) return realignPinned(win)
|
||||
win.scrollTo(win.scrollX || 0, win.scrollY || 0)
|
||||
return true
|
||||
}
|
||||
|
||||
const bodyPinned = win => win.document.body?.style?.position === 'fixed'
|
||||
|
||||
/** The same correction while Modals.jsx has the body pinned (`position:fixed; top:-y`): the
|
||||
* document has no scroll range then, so asking for the current position is a no-op and iOS
|
||||
* keeps the offset — the picker sheet left open after the keyboard closed. Let the page scroll
|
||||
* for one call, land it where the pin says it was, and pin it again. */
|
||||
export function realignPinned(win = window) {
|
||||
const b = win.document.body?.style
|
||||
if (!b || b.position !== 'fixed') return false
|
||||
const top = b.top
|
||||
const y = Math.max(0, -parseFloat(top || '0') || 0)
|
||||
b.position = ''
|
||||
win.scrollTo(win.scrollX || 0, y)
|
||||
b.top = top
|
||||
b.position = 'fixed'
|
||||
return true
|
||||
}
|
||||
|
||||
export function installViewportGuard(win = window) {
|
||||
const vv = win.visualViewport
|
||||
if (!vv) return () => {}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// @vitest-environment node
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { installViewportGuard, realign, viewportDisplacement, keyboardOpen } from './viewport-guard.js'
|
||||
import { installViewportGuard, realign, realignPinned, viewportDisplacement, keyboardOpen } from './viewport-guard.js'
|
||||
|
||||
function fakeWindow({ innerHeight = 800, vvHeight = 800, offsetTop = 0, pageTop = 0, scrollY = 0, active = null } = {}) {
|
||||
function fakeWindow({ innerHeight = 800, vvHeight = 800, offsetTop = 0, pageTop = 0, scrollY = 0, active = null, bodyStyle = {} } = {}) {
|
||||
const listeners = {}
|
||||
const on = (map, type, fn) => { (map[type] = map[type] || []).push(fn) }
|
||||
const vv = {
|
||||
@@ -10,7 +10,7 @@ function fakeWindow({ innerHeight = 800, vvHeight = 800, offsetTop = 0, pageTop
|
||||
addEventListener(t, fn) { on(this.listeners, t, fn) }, removeEventListener() {}
|
||||
}
|
||||
const doc = {
|
||||
activeElement: active, listeners: {},
|
||||
activeElement: active, listeners: {}, body: { style: bodyStyle },
|
||||
addEventListener(t, fn) { on(this.listeners, t, fn) }, removeEventListener() {}
|
||||
}
|
||||
const win = {
|
||||
@@ -41,7 +41,32 @@ describe('viewport guard', () => {
|
||||
|
||||
expect(realign(fakeWindow())).toBe(false) // aligned
|
||||
expect(realign(fakeWindow({ offsetTop: 190, vvHeight: 480 }))).toBe(false) // keyboard still up
|
||||
expect(realign(fakeWindow({ offsetTop: 190, active: { tagName: 'INPUT' } }))).toBe(false) // still typing
|
||||
})
|
||||
|
||||
it('lets go of a text field that kept focus after the keyboard closed, then realigns', () => {
|
||||
// WebKit: tapping the set's tick does not blur the weight field. With the keyboard down
|
||||
// and the page displaced, that focus is what keeps iOS from putting the viewports back.
|
||||
const active = { tagName: 'INPUT', blur: vi.fn() }
|
||||
const w = fakeWindow({ offsetTop: 190, active })
|
||||
expect(realign(w)).toBe(true)
|
||||
expect(active.blur).toHaveBeenCalledTimes(1)
|
||||
expect(w.scrollTo).toHaveBeenCalledWith(0, 0)
|
||||
// an aligned page is left alone, focus included (a desktop browser mid-typing)
|
||||
const typing = { tagName: 'INPUT', blur: vi.fn() }
|
||||
expect(realign(fakeWindow({ active: typing }))).toBe(false)
|
||||
expect(typing.blur).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('unpins the body for one scroll when a sheet has it fixed, and pins it back where it was', () => {
|
||||
const bodyStyle = { position: 'fixed', top: '-240px' }
|
||||
const w = fakeWindow({ offsetTop: 190, scrollY: 0, bodyStyle })
|
||||
const seen = []
|
||||
w.scrollTo = vi.fn(() => seen.push({ ...bodyStyle }))
|
||||
expect(realign(w)).toBe(true)
|
||||
expect(w.scrollTo).toHaveBeenCalledWith(0, 240)
|
||||
expect(seen[0].position).toBe('') // the page could actually scroll at that moment
|
||||
expect(bodyStyle).toEqual({ position: 'fixed', top: '-240px' }) // and is pinned again after
|
||||
expect(realignPinned(fakeWindow())).toBe(false) // nothing pinned, nothing to do
|
||||
})
|
||||
|
||||
it('fires when the keyboard has just closed, again after its animation, and on focusout', async () => {
|
||||
|
||||
Reference in new issue
Block a user