224 lines
6.6 KiB
TypeScript
224 lines
6.6 KiB
TypeScript
import { useEffect, useState, type KeyboardEvent } from 'react'
|
|
import {
|
|
AnimatePresence,
|
|
motion,
|
|
useIsPresent,
|
|
useReducedMotion,
|
|
} from 'motion/react'
|
|
import { useEditorStore } from '@/editor/state/editorStore'
|
|
import { usePlural } from '@/editor/plural'
|
|
import {
|
|
MORE_BUTTON_ID,
|
|
panelDomId,
|
|
tabDomId,
|
|
tabsFor,
|
|
type EditorTabId,
|
|
} from '@/editor/tabs'
|
|
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)]'
|
|
const ACTIVE =
|
|
'border-[var(--color-accent)] font-medium text-[var(--color-accent-strong)]'
|
|
const IDLE =
|
|
'border-transparent text-[var(--color-ink-muted)] hover:text-[var(--color-ink)]'
|
|
|
|
/** The blocking-error count on the Vérification tab, as text; absent at zero. */
|
|
function ErrorCount({ tab }: { tab: EditorTabId }) {
|
|
const plural = usePlural()
|
|
const count = useEditorStore(
|
|
(s) =>
|
|
s.previewState?.report.issues.filter((i) => i.severity === 'error')
|
|
.length ?? 0,
|
|
)
|
|
if (tab !== 'verification' || count === 0) return null
|
|
return (
|
|
<>
|
|
{' '}
|
|
<span className="ml-1 rounded-md bg-red-50 px-1.5 text-xs text-red-700">
|
|
{plural('editor.tab.errors', count)}
|
|
</span>
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** Answers false when the change was refused (a field of the open panel refused to apply). */
|
|
type SelectTab = (
|
|
tab: EditorTabId,
|
|
options?: { replace?: boolean },
|
|
) => boolean | void
|
|
|
|
interface TabListProps {
|
|
shown: readonly EditorTabId[]
|
|
active: EditorTabId
|
|
onSelect: SelectTab
|
|
}
|
|
|
|
/** The tablist proper: the shown tabs only, arrows move between them. */
|
|
function TabList({ shown, active, onSelect }: TabListProps) {
|
|
const t = useT()
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
const at = shown.indexOf(active)
|
|
const step =
|
|
event.key === 'ArrowRight' ? 1 : event.key === 'ArrowLeft' ? -1 : 0
|
|
if (step === 0 || at < 0) return
|
|
event.preventDefault()
|
|
const next = shown[(at + step + shown.length) % shown.length]
|
|
// An arrow-key walk along the row replaces its history entry instead of adding one.
|
|
if (onSelect(next, { replace: true }) === false) return
|
|
document.getElementById(tabDomId(next))?.focus()
|
|
}
|
|
return (
|
|
<div
|
|
role="tablist"
|
|
aria-label={t('editor.tab.list')}
|
|
onKeyDown={onKeyDown}
|
|
className="flex flex-wrap gap-1"
|
|
>
|
|
{shown.map((tab) => (
|
|
<button
|
|
key={tab}
|
|
id={tabDomId(tab)}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={tab === active}
|
|
aria-controls={panelDomId(tab)}
|
|
tabIndex={tab === active ? 0 : -1}
|
|
onClick={() => onSelect(tab)}
|
|
className={`-mb-px ${BASE} ${tab === active ? ACTIVE : IDLE}`}
|
|
>
|
|
{t(`editor.tab.${tab}`)}
|
|
<ErrorCount tab={tab} />
|
|
</button>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
interface MoreTabsProps {
|
|
more: readonly EditorTabId[]
|
|
active: EditorTabId
|
|
onSelect: SelectTab
|
|
}
|
|
|
|
interface MoreListProps {
|
|
more: readonly EditorTabId[]
|
|
active: EditorTabId
|
|
onPick: (tab: EditorTabId) => void
|
|
}
|
|
|
|
/** 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>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* The panel of a tab picked in « Plus » takes the focus once that tab is the shown one: the
|
|
* entry that held it is gone with the list, so the focus would fall to the page.
|
|
*/
|
|
function usePanelFocus(active: EditorTabId) {
|
|
const [wanted, setWanted] = useState<EditorTabId | null>(null)
|
|
useEffect(() => {
|
|
if (wanted === null || wanted !== active) return
|
|
document.getElementById(panelDomId(active))?.focus()
|
|
setWanted(null)
|
|
}, [wanted, active])
|
|
return setWanted
|
|
}
|
|
|
|
/** 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, close, toggle } = useMoreMenu(active)
|
|
const wantPanel = usePanelFocus(active)
|
|
const pick = (tab: EditorTabId) => {
|
|
if (onSelect(tab) !== false) wantPanel(tab)
|
|
close()
|
|
}
|
|
useMoreDismiss(open, close)
|
|
return (
|
|
<>
|
|
<button
|
|
id={MORE_BUTTON_ID}
|
|
type="button"
|
|
aria-expanded={open}
|
|
aria-controls={MORE_LIST_ID}
|
|
onClick={toggle}
|
|
className={`-mb-px ${BASE} ${more.includes(active) ? ACTIVE : IDLE}`}
|
|
>
|
|
{t('editor.tab.more')}
|
|
</button>
|
|
<AnimatePresence>
|
|
{open && <MoreList more={more} active={active} onPick={pick} />}
|
|
</AnimatePresence>
|
|
</>
|
|
)
|
|
}
|
|
|
|
interface EditorTabsProps {
|
|
active: EditorTabId
|
|
tier: TabTier
|
|
onSelect: SelectTab
|
|
}
|
|
|
|
/**
|
|
* 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 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({
|
|
active,
|
|
tier,
|
|
onSelect,
|
|
}: EditorTabsProps) {
|
|
const { shown, more } = tabsFor(tier)
|
|
return (
|
|
<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} />
|
|
)}
|
|
<HistoryButtons />
|
|
</div>
|
|
)
|
|
}
|