feat(editor): Plus dropdown overlay with back request, sticky sidebar
This commit is contained in:
1 parent
8a60128311
commit
ca5804a8b7
8 files changed
+536
-55
No files matched your search
@@ -1,6 +1,10 @@
|
||||
import { useEffect, useRef, useState, type KeyboardEvent } from 'react'
|
||||
import { inWorkspace } from '@/editor/annotations/deselect'
|
||||
import { isTextField } from '@/editor/focusRequest'
|
||||
import type { KeyboardEvent } from 'react'
|
||||
import {
|
||||
AnimatePresence,
|
||||
motion,
|
||||
useIsPresent,
|
||||
useReducedMotion,
|
||||
} from 'motion/react'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { usePlural } from '@/editor/plural'
|
||||
import {
|
||||
@@ -13,6 +17,7 @@ import {
|
||||
import type { TabTier } from '@/editor/useViewport'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import HistoryButtons from './HistoryButtons'
|
||||
import { MORE_LIST_ID, useMoreDismiss, useMoreMenu } from './useMoreMenu'
|
||||
|
||||
const BASE =
|
||||
'min-h-11 whitespace-nowrap border-b-2 px-3 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
|
||||
@@ -99,71 +104,73 @@ interface MoreTabsProps {
|
||||
onSelect: SelectTab
|
||||
}
|
||||
|
||||
/**
|
||||
* While « Plus » is open, Escape closes it and returns the focus to it. Captured on the
|
||||
* document so the key is marked handled before any body's deselection sees it; a modal
|
||||
* dialog keeps its own Escape.
|
||||
*/
|
||||
function useMoreEscape(open: boolean, close: () => void) {
|
||||
const closeRef = useRef(close)
|
||||
closeRef.current = close
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onKey = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key !== 'Escape' || event.isComposing) return
|
||||
if (document.querySelector('dialog[open]')) return
|
||||
if (isTextField(event.target) || !inWorkspace(event.target)) return
|
||||
event.preventDefault()
|
||||
closeRef.current()
|
||||
document.getElementById(MORE_BUTTON_ID)?.focus()
|
||||
}
|
||||
document.addEventListener('keydown', onKey, true)
|
||||
return () => document.removeEventListener('keydown', onKey, true)
|
||||
}, [open])
|
||||
interface MoreListProps {
|
||||
more: readonly EditorTabId[]
|
||||
active: EditorTabId
|
||||
onPick: (tab: EditorTabId) => void
|
||||
}
|
||||
|
||||
/** Below 640 px: « Plus » and its disclosure of the tabs that do not fit. */
|
||||
/** The overlay list: a closing list is inert, so nothing in it takes focus or a tap. */
|
||||
function MoreList({ more, active, onPick }: MoreListProps) {
|
||||
const t = useT()
|
||||
const reduced = useReducedMotion()
|
||||
const present = useIsPresent()
|
||||
const hidden = { opacity: 0, y: reduced ? 0 : -4 }
|
||||
return (
|
||||
<motion.ul
|
||||
id={MORE_LIST_ID}
|
||||
inert={!present}
|
||||
initial={hidden}
|
||||
animate={{
|
||||
opacity: 1,
|
||||
y: 0,
|
||||
transition: { duration: reduced ? 0 : 0.15 },
|
||||
}}
|
||||
exit={{ ...hidden, transition: { duration: reduced ? 0 : 0.12 } }}
|
||||
className="absolute inset-x-0 top-full z-20 flex flex-wrap gap-1 border border-[var(--color-border)] bg-[var(--color-surface)] p-1 shadow-md"
|
||||
>
|
||||
{more.map((tab) => (
|
||||
<li key={tab}>
|
||||
<button
|
||||
id={tabDomId(tab)}
|
||||
type="button"
|
||||
aria-pressed={tab === active}
|
||||
onClick={() => onPick(tab)}
|
||||
className={`rounded-md px-3 ${BASE} border-b-0 ${tab === active ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t(`editor.tab.${tab}`)}
|
||||
<ErrorCount tab={tab} />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</motion.ul>
|
||||
)
|
||||
}
|
||||
|
||||
/** Below 640 px: « Plus » and its dropdown of the tabs that do not fit. */
|
||||
function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
|
||||
const t = useT()
|
||||
const [open, setOpen] = useState(false)
|
||||
const { open, close, toggle } = useMoreMenu(active)
|
||||
const pick = (tab: EditorTabId) => {
|
||||
onSelect(tab)
|
||||
setOpen(false)
|
||||
close()
|
||||
}
|
||||
useMoreEscape(open, () => setOpen(false))
|
||||
useMoreDismiss(open, close)
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
id={MORE_BUTTON_ID}
|
||||
type="button"
|
||||
aria-expanded={open}
|
||||
aria-controls="editor-tabs-more"
|
||||
onClick={() => setOpen((value) => !value)}
|
||||
aria-controls={MORE_LIST_ID}
|
||||
onClick={toggle}
|
||||
className={`-mb-px ${BASE} ${more.includes(active) ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t('editor.tab.more')}
|
||||
</button>
|
||||
{open && (
|
||||
<ul
|
||||
id="editor-tabs-more"
|
||||
className="order-last flex w-full flex-wrap gap-1 border-t border-[var(--color-border)] py-1"
|
||||
>
|
||||
{more.map((tab) => (
|
||||
<li key={tab}>
|
||||
<button
|
||||
id={tabDomId(tab)}
|
||||
type="button"
|
||||
aria-pressed={tab === active}
|
||||
onClick={() => pick(tab)}
|
||||
className={`rounded-md px-3 ${BASE} border-b-0 ${tab === active ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t(`editor.tab.${tab}`)}
|
||||
<ErrorCount tab={tab} />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<AnimatePresence>
|
||||
{open && <MoreList more={more} active={active} onPick={pick} />}
|
||||
</AnimatePresence>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -177,7 +184,7 @@ interface EditorTabsProps {
|
||||
/**
|
||||
* The draft's tab row (boards 04 and 05): the five tabs from 640 px (the row may wrap
|
||||
* between 640 and 1 023 px, never scroll sideways), Annotations and
|
||||
* Vérification below it with « Plus » opening a disclosure of the others (no measurement).
|
||||
* Vérification below it with « Plus » opening a dropdown overlay of the others (no measurement).
|
||||
* « Défaire » and « Refaire » sit at the right end of the row on every tab.
|
||||
*/
|
||||
export default function EditorTabs({
|
||||
@@ -187,7 +194,10 @@ export default function EditorTabs({
|
||||
}: EditorTabsProps) {
|
||||
const { shown, more } = tabsFor(tier)
|
||||
return (
|
||||
<div className="flex flex-wrap items-end gap-1 border-b border-[var(--color-border)]">
|
||||
<div
|
||||
data-editor-tabs
|
||||
className="relative flex flex-wrap items-end gap-1 border-b border-[var(--color-border)]"
|
||||
>
|
||||
<TabList shown={shown} active={active} onSelect={onSelect} />
|
||||
{more.length > 0 && (
|
||||
<MoreTabs more={more} active={active} onSelect={onSelect} />
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { inWorkspace } from '@/editor/annotations/deselect'
|
||||
import { isTextField } from '@/editor/focusRequest'
|
||||
import { MORE_BUTTON_ID, type EditorTabId } from '@/editor/tabs'
|
||||
|
||||
export const MORE_LIST_ID = 'editor-tabs-more'
|
||||
const ROW_MARK = '[data-editor-tabs]'
|
||||
const MENU_PARTS = `#${MORE_LIST_ID}, #${MORE_BUTTON_ID}`
|
||||
/** A closing tap's click follows within this delay or never comes (a drag). */
|
||||
const SWALLOW_MS = 600
|
||||
|
||||
/** The platform's back request (Android Back), where the browser offers it. */
|
||||
interface CloseWatcherLike {
|
||||
onclose: (() => void) | null
|
||||
destroy: () => void
|
||||
}
|
||||
type CloseWatcherCtor = new () => CloseWatcherLike
|
||||
|
||||
const focusMore = () => document.getElementById(MORE_BUTTON_ID)?.focus()
|
||||
|
||||
/**
|
||||
* Open state of « Plus », closed by any change of the active tab (render-time reset, no
|
||||
* effect).
|
||||
*/
|
||||
export function useMoreMenu(active: EditorTabId) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [seen, setSeen] = useState(active)
|
||||
if (seen !== active) {
|
||||
setSeen(active)
|
||||
setOpen(false)
|
||||
}
|
||||
const close = useCallback(() => setOpen(false), [])
|
||||
return { open, close, toggle: () => setOpen((value) => !value) }
|
||||
}
|
||||
|
||||
/**
|
||||
* While « Plus » is open, Escape closes it and returns the focus to it. Captured on the
|
||||
* document so the key is marked handled before any body's deselection sees it; a modal
|
||||
* dialog keeps its own Escape.
|
||||
*/
|
||||
function useMoreEscape(open: boolean, close: () => void) {
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key !== 'Escape' || event.isComposing) return
|
||||
if (document.querySelector('dialog[open]')) return
|
||||
if (isTextField(event.target) || !inWorkspace(event.target)) return
|
||||
event.preventDefault()
|
||||
close()
|
||||
focusMore()
|
||||
}
|
||||
document.addEventListener('keydown', onKey, true)
|
||||
return () => document.removeEventListener('keydown', onKey, true)
|
||||
}, [open, close])
|
||||
}
|
||||
|
||||
/** Swallows the click that completes a closing tap, once, so the page never sees it. */
|
||||
function swallowNextClick() {
|
||||
const stop = (event: Event) => {
|
||||
event.stopPropagation()
|
||||
event.preventDefault()
|
||||
release()
|
||||
}
|
||||
const timer = window.setTimeout(() => release(), SWALLOW_MS)
|
||||
const release = () => {
|
||||
window.clearTimeout(timer)
|
||||
document.removeEventListener('click', stop, true)
|
||||
}
|
||||
document.addEventListener('click', stop, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* A press outside « Plus » closes it. Taps outside the tab row never reach the page (no
|
||||
* deselection, no click); taps inside the row (a tab, Défaire) go on and act.
|
||||
*/
|
||||
function useOutsidePress(open: boolean, close: () => void) {
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onDown = (event: PointerEvent) => {
|
||||
const target = event.target instanceof Element ? event.target : null
|
||||
if (target?.closest(MENU_PARTS)) return
|
||||
close()
|
||||
if (target?.closest(ROW_MARK)) return
|
||||
event.stopPropagation()
|
||||
swallowNextClick()
|
||||
}
|
||||
document.addEventListener('pointerdown', onDown, true)
|
||||
return () => document.removeEventListener('pointerdown', onDown, true)
|
||||
}, [open, close])
|
||||
}
|
||||
|
||||
/** The platform's back request closes the open menu before history goes back. */
|
||||
function useBackRequest(open: boolean, close: () => void) {
|
||||
useEffect(() => {
|
||||
const Ctor = (window as { CloseWatcher?: CloseWatcherCtor }).CloseWatcher
|
||||
if (!open || !Ctor) return
|
||||
const watcher = new Ctor()
|
||||
watcher.onclose = () => {
|
||||
close()
|
||||
focusMore()
|
||||
}
|
||||
return () => watcher.destroy()
|
||||
}, [open, close])
|
||||
}
|
||||
|
||||
/** Every way « Plus » closes besides a selection and a tab change. */
|
||||
export function useMoreDismiss(open: boolean, close: () => void) {
|
||||
useMoreEscape(open, close)
|
||||
useOutsidePress(open, close)
|
||||
useBackRequest(open, close)
|
||||
}
|
||||
@@ -48,7 +48,7 @@ export function Sidebar({ shell }: { shell: ShellBreakpoint }) {
|
||||
const t = useT()
|
||||
return (
|
||||
<aside
|
||||
className={`${shell.sidebar} border-r border-[var(--color-border)] bg-[var(--color-surface)]`}
|
||||
className={`${shell.sidebar} sticky top-0 h-dvh self-start overflow-y-auto border-r border-[var(--color-border)] bg-[var(--color-surface)]`}
|
||||
>
|
||||
<div className="px-5 py-5">
|
||||
<Link to="/app" aria-label={t('shell.nav.dashboard')} className="inline-block">
|
||||
|
||||
Reference in new issue
Block a user