fix(editor): view row reveal survives a reversed slide; double click ignored
This commit is contained in:
1 parent
295413c5f9
commit
bc82cd63b6
4 files changed
+286
-55
No files matched your search
@@ -4,7 +4,7 @@ import type { SheetView } from '@faunosteo/core-wasm/types'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useViewRow } from './viewRow'
|
||||
import { chipOf, useViewRow } from './viewRow'
|
||||
import type { HintState } from './viewRow'
|
||||
|
||||
const PILL =
|
||||
@@ -28,7 +28,10 @@ const DOT =
|
||||
'absolute -right-0.5 -top-0.5 h-2.5 w-2.5 rounded-full bg-[var(--color-ink)] ' +
|
||||
'ring-2 ring-white'
|
||||
|
||||
/** `:focus-visible` is unsupported in some environments (happy-dom): then not keyboard. */
|
||||
/**
|
||||
* Whether the chip took a keyboard focus. happy-dom treats `:focus-visible` as `:focus`; the
|
||||
* try/catch is for engines that throw on the selector (then: not keyboard).
|
||||
*/
|
||||
function isKeyboardFocus(element: HTMLElement): boolean {
|
||||
try {
|
||||
return element.matches(':focus-visible')
|
||||
@@ -120,7 +123,10 @@ export default function ViewSwitcher({
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {
|
||||
if (event.altKey || event.ctrlKey || event.metaKey) return
|
||||
const at = views.findIndex((view) => view.id === tabStop)
|
||||
const target = event.target as HTMLElement
|
||||
const from =
|
||||
target.closest<HTMLElement>('[data-view-chip]')?.dataset.viewChip
|
||||
const at = views.findIndex((view) => view.id === (from ?? tabStop))
|
||||
const last = views.length - 1
|
||||
const to = {
|
||||
ArrowRight: Math.min(at + 1, last),
|
||||
@@ -131,11 +137,8 @@ export default function ViewSwitcher({
|
||||
if (to === undefined || views.length === 0) return
|
||||
event.preventDefault()
|
||||
const id = views[to].id
|
||||
const chips =
|
||||
rowRef.current?.querySelectorAll<HTMLElement>('[data-view-chip]')
|
||||
Array.from(chips ?? [])
|
||||
.find((chip) => chip.dataset.viewChip === id)
|
||||
?.focus({ preventScroll: true })
|
||||
const row = rowRef.current
|
||||
if (row) chipOf(row, id)?.focus({ preventScroll: true })
|
||||
reveal(id)
|
||||
}
|
||||
const press = (side: 'start' | 'end') =>
|
||||
@@ -163,7 +166,11 @@ export default function ViewSwitcher({
|
||||
tabIndex={tabStop === view.id ? 0 : -1}
|
||||
data-view-chip={view.id}
|
||||
onClick={(event) =>
|
||||
view.id !== current && onSelect(view.id, event.currentTarget)
|
||||
// A repeated click (detail > 1) is dropped: the row may have moved
|
||||
// under the pointer after the first, and the second would hit a neighbour.
|
||||
event.detail <= 1 &&
|
||||
view.id !== current &&
|
||||
onSelect(view.id, event.currentTarget)
|
||||
}
|
||||
className="group h-11 shrink-0 px-1 outline-none"
|
||||
>
|
||||
|
||||
@@ -68,7 +68,7 @@ export function hintState(
|
||||
return { start, end, dot }
|
||||
}
|
||||
|
||||
function chipOf(row: HTMLElement, id: string): HTMLElement | null {
|
||||
export function chipOf(row: HTMLElement, id: string): HTMLElement | null {
|
||||
const chips = row.querySelectorAll<HTMLElement>('[data-view-chip]')
|
||||
return Array.from(chips).find((chip) => chip.dataset.viewChip === id) ?? null
|
||||
}
|
||||
@@ -124,27 +124,51 @@ export function useViewRow(
|
||||
const currentRef = useRef(current)
|
||||
currentRef.current = current
|
||||
|
||||
/**
|
||||
* Where a smooth slide in flight will land, null when none: a reveal is judged against
|
||||
* it, not against the instantaneous `scrollLeft` (a chip clear at the moment may not be
|
||||
* once the slide ends).
|
||||
*/
|
||||
const pending = useRef<number | null>(null)
|
||||
|
||||
const slideTo = useCallback(
|
||||
(row: HTMLElement, left: number, instant: boolean) => {
|
||||
const slides = !instant && Math.abs(left - row.scrollLeft) >= 1
|
||||
pending.current = slides ? left : null
|
||||
row.scrollTo({ left, behavior: instant ? 'auto' : 'smooth' })
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const reveal = useCallback(
|
||||
(id: string) => {
|
||||
(id: string, now = false) => {
|
||||
const row = rowRef.current
|
||||
const chip = row && chipOf(row, id)
|
||||
if (!row || !chip) return
|
||||
const left = revealLeft(row, spanOf(row, chip))
|
||||
const left = revealLeft(
|
||||
{
|
||||
scrollLeft: pending.current ?? row.scrollLeft,
|
||||
clientWidth: row.clientWidth,
|
||||
scrollWidth: row.scrollWidth,
|
||||
},
|
||||
spanOf(row, chip),
|
||||
)
|
||||
if (left === null) return
|
||||
const instant = opening.current || reduced
|
||||
row.scrollTo({ left, behavior: instant ? 'auto' : 'smooth' })
|
||||
slideTo(row, left, now || opening.current || !!reduced)
|
||||
},
|
||||
[rowRef, reduced],
|
||||
[rowRef, reduced, slideTo],
|
||||
)
|
||||
|
||||
const page = useCallback(
|
||||
(direction: 1 | -1) => {
|
||||
const row = rowRef.current
|
||||
if (!row) return
|
||||
const left = direction * PAGE_SHARE * row.clientWidth
|
||||
row.scrollBy({ left, behavior: reduced ? 'auto' : 'smooth' })
|
||||
const from = pending.current ?? row.scrollLeft
|
||||
const max = row.scrollWidth - row.clientWidth
|
||||
const left = from + direction * PAGE_SHARE * row.clientWidth
|
||||
slideTo(row, Math.min(Math.max(left, 0), max), !!reduced)
|
||||
},
|
||||
[rowRef, reduced],
|
||||
[rowRef, reduced, slideTo],
|
||||
)
|
||||
|
||||
const revealRef = useRef(reveal)
|
||||
@@ -163,26 +187,53 @@ export function useViewRow(
|
||||
refresh()
|
||||
}, [refresh, namesKey])
|
||||
|
||||
// New names change the chip widths: the active chip is put back at once, like the opening.
|
||||
const names = useRef(namesKey)
|
||||
useLayoutEffect(() => {
|
||||
if (names.current === namesKey) return
|
||||
names.current = namesKey
|
||||
revealRef.current(currentRef.current, true)
|
||||
}, [namesKey])
|
||||
|
||||
useEffect(() => {
|
||||
const row = rowRef.current
|
||||
if (!row) return
|
||||
const onScroll = () => refreshRef.current()
|
||||
const settled = () => {
|
||||
pending.current = null
|
||||
}
|
||||
const onScroll = () => {
|
||||
const target = pending.current
|
||||
if (target !== null && Math.abs(row.scrollLeft - target) < 1) settled()
|
||||
refreshRef.current()
|
||||
}
|
||||
// The user takes the row over, or the slide ends where `scrollend` exists.
|
||||
// shortcut: an engine without `scrollend` whose slide is cut short by something other
|
||||
// than these events keeps a stale target until the row next reaches it.
|
||||
const takeovers = ['wheel', 'touchstart', 'pointerdown', 'scrollend']
|
||||
row.addEventListener('scroll', onScroll, { passive: true })
|
||||
if (typeof ResizeObserver === 'undefined')
|
||||
return () => row.removeEventListener('scroll', onScroll)
|
||||
for (const type of takeovers) row.addEventListener(type, settled)
|
||||
const detach = () => {
|
||||
row.removeEventListener('scroll', onScroll)
|
||||
for (const type of takeovers) row.removeEventListener(type, settled)
|
||||
}
|
||||
if (typeof ResizeObserver === 'undefined') return detach
|
||||
let width = row.clientWidth
|
||||
const observer = new ResizeObserver(() => {
|
||||
if (width === 0 && row.clientWidth > 0) {
|
||||
openWindow()
|
||||
// A view that changed while the row was hidden had nothing to reveal.
|
||||
revealRef.current(currentRef.current)
|
||||
}
|
||||
const shown = width === 0 && row.clientWidth > 0
|
||||
if (shown) openWindow()
|
||||
// A view that changed while the row was hidden had nothing to reveal; a new width
|
||||
// moves the chip edges the same way.
|
||||
if (
|
||||
shown ||
|
||||
(width > 0 && row.clientWidth > 0 && row.clientWidth !== width)
|
||||
)
|
||||
revealRef.current(currentRef.current, true)
|
||||
width = row.clientWidth
|
||||
refreshRef.current()
|
||||
})
|
||||
observer.observe(row)
|
||||
return () => {
|
||||
row.removeEventListener('scroll', onScroll)
|
||||
detach()
|
||||
observer.disconnect()
|
||||
}
|
||||
}, [rowRef, openWindow])
|
||||
|
||||
@@ -45,8 +45,9 @@ body,
|
||||
while another section shows, and the strip is sticky from 1 024 px only. Where the
|
||||
drawing column is pinned (block below), its controls take the bar's margin instead.
|
||||
The chip row is scrolled sideways, not by these margins: its own `scroll-padding-inline:
|
||||
72px` (the fade width) keeps a chip the browser focus-scrolls into view, a Tab entry for
|
||||
one, clear of the fades in this Chromium; other browsers do it best effort. */
|
||||
72px` (the fade width) helps a chip that is fully hidden when the browser focus-scrolls it
|
||||
into view; a chip only partly under a fade is not scrolled by the browser, so the
|
||||
`:focus-visible` reveal in `viewRow.ts` covers it. Other browsers do it best effort. */
|
||||
@media (min-width: 1024px) {
|
||||
[data-place='annotations.drawing']:not([hidden])
|
||||
:is(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** U2 criterion 32: the view chips in one scrolling row (roving tab stop, hints, reveal). */
|
||||
import '../helpers/motion'
|
||||
import { setReducedMotion } from '../helpers/motion'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
act,
|
||||
@@ -62,13 +62,20 @@ function renderRow(onSelect = vi.fn(), views = viewsOf(), current = 'v3') {
|
||||
}
|
||||
}
|
||||
|
||||
const originalScrollTo = Element.prototype.scrollTo
|
||||
|
||||
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
resetStore()
|
||||
resetWork()
|
||||
setReducedMotion(false)
|
||||
vi.useRealTimers()
|
||||
delete (Element.prototype as unknown as Record<string, unknown>).scrollTo
|
||||
Object.defineProperty(Element.prototype, 'scrollTo', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: originalScrollTo,
|
||||
})
|
||||
})
|
||||
|
||||
describe('the roving tab stop and the keys', () => {
|
||||
@@ -123,6 +130,22 @@ describe('the roving tab stop and the keys', () => {
|
||||
expect(onSelect).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('drops a repeated click (detail above 1): the row may have moved under it', () => {
|
||||
const { onSelect } = renderRow()
|
||||
fireEvent.click(chip('Vue 2'), { detail: 2 })
|
||||
expect(onSelect).not.toHaveBeenCalled()
|
||||
fireEvent.click(chip('Vue 2'), { detail: 0 })
|
||||
fireEvent.click(chip('Vue 4'), { detail: 1 })
|
||||
expect(onSelect).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('starts an arrow from the chip that has the focus, not from the tab stop', () => {
|
||||
renderRow()
|
||||
// The tab stop is the active chip (Vue 3); the key comes from another chip.
|
||||
fireEvent.keyDown(chip('Vue 5'), { key: 'ArrowLeft' })
|
||||
expect(chip('Vue 4')).toHaveFocus()
|
||||
})
|
||||
|
||||
it('puts the tab stop back on the active chip when the focus leaves', async () => {
|
||||
renderRow()
|
||||
act(() => chip('Vue 3').focus())
|
||||
@@ -171,8 +194,15 @@ describe('the chips and their hints', () => {
|
||||
})
|
||||
})
|
||||
|
||||
/** Row geometry: 300 px wide showing 900 of content, each chip 100 px. */
|
||||
function stubGeometry(scrollWidth = 900, clientWidth = 300) {
|
||||
/**
|
||||
* Row geometry: 300 px wide showing 900 of content, each chip 100 px. `scrolled` reads the
|
||||
* row's instantaneous `scrollLeft`, which the chip boxes follow like in a browser.
|
||||
*/
|
||||
function stubGeometry(
|
||||
scrollWidth = 900,
|
||||
clientWidth = 300,
|
||||
scrolled = () => 0,
|
||||
) {
|
||||
const row = toolbar()
|
||||
Object.defineProperty(row, 'clientWidth', {
|
||||
configurable: true,
|
||||
@@ -182,13 +212,42 @@ function stubGeometry(scrollWidth = 900, clientWidth = 300) {
|
||||
configurable: true,
|
||||
value: scrollWidth,
|
||||
})
|
||||
Object.defineProperty(row, 'scrollLeft', {
|
||||
configurable: true,
|
||||
get: scrolled,
|
||||
})
|
||||
row.getBoundingClientRect = () => ({ left: 0, right: clientWidth }) as DOMRect
|
||||
chips().forEach((c, index) => {
|
||||
c.getBoundingClientRect = () =>
|
||||
({ left: index * 100, right: index * 100 + 100 }) as DOMRect
|
||||
({
|
||||
left: index * 100 - scrolled(),
|
||||
right: index * 100 + 100 - scrolled(),
|
||||
}) as DOMRect
|
||||
})
|
||||
}
|
||||
|
||||
/** The ResizeObserver callbacks of the mounted rows; `restore` puts the original back. */
|
||||
function stubResizeObserver() {
|
||||
const original = globalThis.ResizeObserver
|
||||
const callbacks: Array<() => void> = []
|
||||
vi.stubGlobal(
|
||||
'ResizeObserver',
|
||||
class {
|
||||
constructor(callback: () => void) {
|
||||
callbacks.push(callback)
|
||||
}
|
||||
observe() {}
|
||||
disconnect() {}
|
||||
},
|
||||
)
|
||||
return {
|
||||
resize: () => act(() => callbacks.forEach((callback) => callback())),
|
||||
restore: () => {
|
||||
globalThis.ResizeObserver = original
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe('the reveal of the active chip', () => {
|
||||
let scrollTo: ReturnType<typeof vi.fn>
|
||||
beforeEach(() => {
|
||||
@@ -219,31 +278,65 @@ describe('the reveal of the active chip', () => {
|
||||
})
|
||||
|
||||
it('reveals the active chip when the row is shown again', () => {
|
||||
const original = globalThis.ResizeObserver
|
||||
const observers: Array<() => void> = []
|
||||
vi.stubGlobal(
|
||||
'ResizeObserver',
|
||||
class {
|
||||
constructor(callback: () => void) {
|
||||
observers.push(callback)
|
||||
}
|
||||
observe() {}
|
||||
disconnect() {}
|
||||
},
|
||||
)
|
||||
const observer = stubResizeObserver()
|
||||
try {
|
||||
vi.useFakeTimers()
|
||||
const { again } = renderRow(vi.fn(), viewsOf(), 'v1')
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
// Hidden: width 0, the change reveals nothing.
|
||||
stubGeometry(0, 0)
|
||||
again({ current: 'v5' })
|
||||
expect(scrollTo).not.toHaveBeenCalled()
|
||||
// Shown again: the chip (400 to 500) is past the end hint of a 300 px row.
|
||||
stubGeometry()
|
||||
observer.resize()
|
||||
expect(scrollTo).toHaveBeenCalledTimes(1)
|
||||
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
|
||||
} finally {
|
||||
observer.restore()
|
||||
}
|
||||
})
|
||||
|
||||
it('puts the active chip back at once when the row width changes', () => {
|
||||
const observer = stubResizeObserver()
|
||||
try {
|
||||
vi.useFakeTimers()
|
||||
renderRow(vi.fn(), viewsOf(), 'v5')
|
||||
stubGeometry()
|
||||
observer.resize()
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
scrollTo.mockClear()
|
||||
// 250 px wide: the chip (400 to 500) lands 72 px from the end of the new width.
|
||||
stubGeometry(900, 250)
|
||||
observer.resize()
|
||||
expect(scrollTo).toHaveBeenCalledTimes(1)
|
||||
expect(scrollTo).toHaveBeenCalledWith({ left: 322, behavior: 'auto' })
|
||||
} finally {
|
||||
observer.restore()
|
||||
}
|
||||
})
|
||||
|
||||
it('puts the active chip back at once when a language switch changes the names', () => {
|
||||
vi.useFakeTimers()
|
||||
renderRow(vi.fn(), viewsOf(), 'v5')
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
stubGeometry()
|
||||
scrollTo.mockClear()
|
||||
act(() => useLocaleStore.setState({ locale: 'en' }))
|
||||
expect(scrollTo).toHaveBeenCalledTimes(1)
|
||||
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
|
||||
})
|
||||
|
||||
it('slides with `auto` under reduced motion, for a reveal and for a page', () => {
|
||||
setReducedMotion(true)
|
||||
vi.useFakeTimers()
|
||||
const { again } = renderRow(vi.fn(), viewsOf(), 'v1')
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
// Hidden: width 0, the change reveals nothing.
|
||||
stubGeometry(0, 0)
|
||||
again({ current: 'v5' })
|
||||
expect(scrollTo).not.toHaveBeenCalled()
|
||||
// Shown again: the chip (400 to 500) is past the end hint of a 300 px row.
|
||||
stubGeometry()
|
||||
act(() => observers.forEach((callback) => callback()))
|
||||
expect(scrollTo).toHaveBeenCalledTimes(1)
|
||||
expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' })
|
||||
globalThis.ResizeObserver = original
|
||||
fireEvent.click(screen.getByTitle('Vues suivantes'))
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 240, behavior: 'auto' })
|
||||
again({ current: 'v5' })
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 272, behavior: 'auto' })
|
||||
})
|
||||
|
||||
it('shows the end hint again when a name change widens the content', () => {
|
||||
@@ -259,6 +352,85 @@ describe('the reveal of the active chip', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('a reveal while a slide is in flight', () => {
|
||||
let scrollTo: ReturnType<typeof vi.fn>
|
||||
let scrolled: number
|
||||
beforeEach(() => {
|
||||
scrollTo = vi.fn()
|
||||
scrolled = 0
|
||||
Object.defineProperty(Element.prototype, 'scrollTo', {
|
||||
configurable: true,
|
||||
value: scrollTo,
|
||||
})
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
/** Focus on Vue 1, geometry stubbed past the opening window, then four arrows right. */
|
||||
function arrowsToTheFourth() {
|
||||
renderRow(vi.fn(), viewsOf(), 'v1')
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
stubGeometry(900, 300, () => scrolled)
|
||||
act(() => chip('Vue 1').focus())
|
||||
for (let n = 0; n < 4; n++)
|
||||
fireEvent.keyDown(document.activeElement!, { key: 'ArrowRight' })
|
||||
expect(chip('Vue 5')).toHaveFocus()
|
||||
// Each chip past the end hint retargets the slide: the last lands on 272.
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 272, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
it('judges the chip against where the slide lands: a reversed arrow stops it short', () => {
|
||||
arrowsToTheFourth()
|
||||
// Mid-flight at 200 the fourth chip (300 to 400) is clear, but at 272 it is not.
|
||||
scrolled = 200
|
||||
fireEvent.keyDown(chip('Vue 5'), { key: 'ArrowLeft' })
|
||||
expect(chip('Vue 4')).toHaveFocus()
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 228, behavior: 'smooth' })
|
||||
})
|
||||
|
||||
it('leaves a slide that already brings the chip clear alone', () => {
|
||||
arrowsToTheFourth()
|
||||
scrollTo.mockClear()
|
||||
// The fifth chip (400 to 500) is the one the slide ends on.
|
||||
fireEvent.keyDown(chip('Vue 5'), { key: 'ArrowRight' })
|
||||
expect(scrollTo).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it.each([
|
||||
[
|
||||
'the slide ends (scrollend)',
|
||||
() => fireEvent(toolbar(), new Event('scrollend')),
|
||||
],
|
||||
['the user takes the row (wheel)', () => fireEvent.wheel(toolbar())],
|
||||
[
|
||||
'the row reaches its target',
|
||||
() => {
|
||||
scrolled = 272
|
||||
fireEvent.scroll(toolbar())
|
||||
scrolled = 200
|
||||
},
|
||||
],
|
||||
])('judges against the instantaneous position once %s', (_name, end) => {
|
||||
arrowsToTheFourth()
|
||||
scrolled = 200
|
||||
end()
|
||||
scrollTo.mockClear()
|
||||
// At rest on 200 the fourth chip (300 to 400) is clear: nothing to do.
|
||||
fireEvent.keyDown(chip('Vue 5'), { key: 'ArrowLeft' })
|
||||
expect(scrollTo).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('judges an arrow after a chevron page against the page target', () => {
|
||||
renderRow(vi.fn(), viewsOf(), 'v1')
|
||||
act(() => void vi.advanceTimersByTime(OPENING_MS + 1))
|
||||
stubGeometry(900, 300, () => scrolled)
|
||||
act(() => chip('Vue 1').focus())
|
||||
fireEvent.click(screen.getByTitle('Vues suivantes'))
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 240, behavior: 'smooth' })
|
||||
// Instantaneously at 0 the second chip is clear; at the page target 240 it is not.
|
||||
fireEvent.keyDown(chip('Vue 1'), { key: 'ArrowRight' })
|
||||
expect(scrollTo).toHaveBeenLastCalledWith({ left: 28, behavior: 'smooth' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('the reveal of a chip focused from outside the row', () => {
|
||||
let scrollTo: ReturnType<typeof vi.fn>
|
||||
beforeEach(() => {
|
||||
|
||||
Reference in new issue
Block a user