SPB Git

Repository Public

Splitter.tsx 3.4 KB 82 lines tsx

1// Séparateur de volets : glissement à la souris ou au doigt, flèches du clavier, double-clic pour revenir à la taille d'origine.2// Rôle ARIA « separator » focalisable (aria-valuenow/min/max) ; la taille est propre à l'appareil.3import { useRef } from "react"4import { t } from "../i18n"5import { S } from "../state/app"6import { PANE_LIMITS, type PaneKey } from "../state/device"7import { setPaneSize } from "../state/session"8import { useStore } from "../state/store"910const STEP = 1611const LABEL: Record<PaneKey, "splitter.sidebar" | "splitter.list" | "splitter.listHeight"> = { sidebar: "splitter.sidebar", list: "splitter.list", listHeight: "splitter.listHeight" }1213export function Splitter({ pane, controls }: { pane: PaneKey; /** id du volet redimensionné */ controls: string }) {14  const value = useStore(S, (s) => s.device.paneWidths[pane])15  const limits = PANE_LIMITS[pane]16  const horizontal = pane === "listHeight"17  const drag = useRef<{ start: number; from: number; last: number } | null>(null)18  const ref = useRef<HTMLDivElement>(null)19  const pos = (e: { clientX: number; clientY: number }) => (horizontal ? e.clientY : e.clientX)20  return (21    <div22      ref={ref}23      className={`splitter${horizontal ? " horizontal" : ""}`}24      data-pane={pane}25      role="separator"26      tabIndex={0}27      aria-orientation={horizontal ? "horizontal" : "vertical"}28      aria-controls={controls}29      aria-label={t(LABEL[pane])}30      aria-valuenow={value}31      aria-valuemin={limits.min}32      aria-valuemax={limits.max}33      title={t("splitter.hint")}34      onPointerDown={(e) => {35        if (e.button !== 0) return36        e.preventDefault()37        drag.current = { start: pos(e), from: value, last: value }38        e.currentTarget.setPointerCapture(e.pointerId)39        e.currentTarget.classList.add("dragging")40        document.documentElement.classList.add("resizing")41      }}42      onPointerMove={(e) => {43        const d = drag.current44        if (!d) return45        // pendant le glissement, seule la variable CSS bouge : aucun rendu React, aucune écriture de stockage46        d.last = setPaneSize(pane, d.from + pos(e) - d.start, false)47        ref.current?.setAttribute("aria-valuenow", String(d.last))48      }}49      onPointerUp={(e) => {50        const d = drag.current51        drag.current = null52        e.currentTarget.classList.remove("dragging")53        document.documentElement.classList.remove("resizing")54        if (d) setPaneSize(pane, d.last)55      }}56      onPointerCancel={(e) => {57        const d = drag.current58        drag.current = null59        e.currentTarget.classList.remove("dragging")60        document.documentElement.classList.remove("resizing")61        if (d) setPaneSize(pane, d.from)62      }}63      onDoubleClick={() => setPaneSize(pane, limits.initial)}64      onKeyDown={(e) => {65        const less = horizontal ? "ArrowUp" : "ArrowLeft"66        const more = horizontal ? "ArrowDown" : "ArrowRight"67        const step = e.shiftKey ? STEP * 3 : STEP68        let next: number | null = null69        if (e.key === less) next = value - step70        else if (e.key === more) next = value + step71        else if (e.key === "Home") next = limits.min72        else if (e.key === "End") next = limits.max73        else if (e.key === "Enter") next = limits.initial74        if (next === null) return75        e.preventDefault()76        e.stopPropagation()77        setPaneSize(pane, next)78      }}79    />80  )81}82
Files main
Menu

Theme