Repository Public
1// Briques d'interface partagées : icônes, dialogue modal accessible, menu, notifications, garde d'erreurs.2import { Component, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore, type CSSProperties, type ReactNode } from "react"3import { t } from "../i18n"4import { initials, type Addr } from "../mail/address"5import { addressHue } from "../mail/unified"6import { S, dismissToast, type Toast } from "../state/app"7import { useStore } from "../state/store"89// ---------------------------------------------------------------- icônes (SVG en ligne, trait 1.75)10const PATHS = {11 inbox: "M4 13.5 6.2 5.6A2 2 0 0 1 8.1 4h7.8a2 2 0 0 1 1.9 1.6L20 13.5M4 13.5V18a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-4.5M4 13.5h4.5l1 2.5h5l1-2.5H20",12 unread: "M4 7.5A2.5 2.5 0 0 1 6.5 5h11A2.5 2.5 0 0 1 20 7.5v9a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 4 16.5v-9Zm0 .5 8 5.5L20 8",13 star: "m12 3.8 2.5 5.2 5.7.8-4.1 4 1 5.7-5.1-2.7-5.1 2.7 1-5.7-4.1-4 5.7-.8L12 3.8Z",14 pin: "M9 4h6l-1 5 3.5 3.5v1.5h-11V12.5L10 9 9 4Zm3 10v6",15 draft: "M5 19h4L19.5 8.5a2.1 2.1 0 0 0-3-3L6 16l-1 3Zm9.5-11.5 3 3",16 sent: "m4 12 16-7-6 16-2.5-6.5L4 12Zm7.5 2.5L20 5",17 archive: "M4 5h16v4H4V5Zm1.5 4v8a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V9M10 13h4",18 junk: "M12 3.5 20.5 8v8L12 20.5 3.5 16V8L12 3.5Zm0 5v4.5m0 3v.01",19 trash: "M5 7h14M10 7V5h4v2m-7 0 .8 11a2 2 0 0 0 2 1.9h4.4a2 2 0 0 0 2-1.9L17 7m-7 4v5m4-5v5",20 folder: "M4 7a2 2 0 0 1 2-2h3.5l2 2H18a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V7Z",21 plus: "M12 5v14M5 12h14",22 search: "M11 18a7 7 0 1 1 0-14 7 7 0 0 1 0 14Zm9 2-4-4",23 close: "m6 6 12 12M18 6 6 18",24 menu: "M4 7h16M4 12h16M4 17h16",25 back: "M15 5l-7 7 7 7",26 reply: "M10 8V4l-7 7 7 7v-4c5 0 8 1.5 10 5-1-7-4-11-10-11Z",27 replyAll: "M8 8V5l-6 6 6 6v-3m5-6V4L6 11l7 7v-4c4 0 6.5 1.5 8 5-1-7-3-11-8-11Z",28 forward: "M14 8V4l7 7-7 7v-4C9 14 6 15.5 4 19c1-7 4-11 10-11Z",29 attach: "m20 11.5-8 8a5 5 0 0 1-7-7l8.5-8.5a3.3 3.3 0 0 1 4.7 4.7L9.8 17.1a1.7 1.7 0 0 1-2.4-2.4L15 7",30 send: "M4 12h13m0 0-5-5m5 5-5 5M20 5v14",31 settings: "M12 15.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7Zm7.5-3.5a7.6 7.6 0 0 0-.1-1.3l2-1.5-2-3.4-2.3.9a7.5 7.5 0 0 0-2.3-1.3L14.400 3h-4l-.4 2.400a7.5 7.5 0 0 0-2.300 1.300l-2.300-.900-2 3.400 2 1.500a7.600 7.600 0 0 0 0 2.600l-2 1.500 2 3.400 2.300-.900a7.500 7.500 0 0 0 2.300 1.300l.4 2.400h4l.4-2.400a7.500 7.500 0 0 0 2.300-1.300l2.300.900 2-3.400-2-1.500c.1-.4.1-.900.1-1.300Z",32 shield: "M12 3.500 19 6v5.500c0 4.300-2.900 7.600-7 9-4.100-1.400-7-4.700-7-9V6l7-2.500Z",33 shieldOk: "M12 3.500 19 6v5.500c0 4.300-2.900 7.600-7 9-4.100-1.400-7-4.700-7-9V6l7-2.500Zm-3 8.500 2.200 2.200L15 10.400",34 warn: "M12 4 21 19.500H3L12 4Zm0 6v4.500m0 2.500v.01",35 check: "m5 12.500 4.500 4.500L19 7.500",36 more: "M6 12h.01M12 12h.01M18 12h.01",37 download: "M12 4v11m0 0-4.500-4.500M12 15l4.500-4.500M5 19h14",38 image: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm0 10 4.500-4.500 4 4L15 13l5 5M9 9.500h.01",39 file: "M7 3.500h7l4 4V19a1.500 1.500 0 0 1-1.500 1.500h-9.500A1.500 1.500 0 0 1 5.500 19V5A1.500 1.500 0 0 1 7 3.500Zm7 0v4h4",40 signout: "M10 5H6.500A1.500 1.500 0 0 0 5 6.500v11A1.500 1.500 0 0 0 6.500 19H10m5-3.500 3.500-3.500L15 8.500M18.500 12H10",41 key: "M15 9.500a3.500 3.500 0 1 1-3.300 4.700L4 14.200V17h2.500v2H9l2.800-2.800",42 code: "m9 7-5 5 5 5m6-10 5 5-5 5",43 chevron: "m8 10 4 4 4-4",44 refresh: "M19 12a7 7 0 1 1-2.100-5m2.100-2v4h-4",45 offline: "M4 4l16 16M8.500 8.800A9 9 0 0 0 3.500 12m17 0a9 9 0 0 0-6-3.700M7 15.500a6 6 0 0 1 4-1.900m6 1.900a6 6 0 0 0-2-1.500M12 19h.01",46 help: "M12 20.500a8.500 8.500 0 1 0 0-17 8.500 8.500 0 0 0 0 17Zm-2.200-10.800a2.300 2.300 0 1 1 3.400 2c-.800.500-1.200 1-1.200 1.800m0 2.500v.01",47 mail: "M4 7.500A2.500 2.500 0 0 1 6.500 5h11A2.500 2.500 0 0 1 20 7.500v9a2.500 2.500 0 0 1-2.500 2.500h-11A2.500 2.500 0 0 1 4 16.500v-9Zm0 .500 8 5.500L20 8",48 edit: "M5 19h4L19.500 8.500a2.100 2.100 0 0 0-3-3L6 16l-1 3Z",49 clock: "M12 20.500a8.500 8.500 0 1 0 0-17 8.500 8.500 0 0 0 0 17Zm0-13V12l3 2",50 user: "M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm-7.500 8a7.500 7.500 0 0 1 15 0",51 filter: "M4 6h16l-6 7.500V19l-4-1.500v-4L4 6Z",52 move: "M4 7a2 2 0 0 1 2-2h3.500l2 2H18a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V7Zm6 6h5m0 0-2-2m2 2-2 2",53 server: "M5 5h14v6H5V5Zm0 8h14v6H5v-6Zm3-5h.01M8 16h.01",54 globe: "M12 20.500a8.500 8.500 0 1 0 0-17 8.500 8.500 0 0 0 0 17Zm-8.500-8.500h17M12 3.500c2.500 2.300 3.700 5.200 3.700 8.500s-1.200 6.200-3.700 8.500c-2.500-2.300-3.700-5.200-3.700-8.500s1.200-6.200 3.700-8.500Z",55 queue: "M4 6h16M4 12h10M4 18h7m7-3.500v5m0 0-2-2m2 2 2-2",56 list: "M8 6h12M8 12h12M8 18h12M4 6h.01M4 12h.01M4 18h.01",57 template: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm0 4h16M9 10v10",58 sparkle: "m11 4 1.7 4.6 4.6 1.7-4.6 1.700L11 16.600l-1.700-4.600-4.600-1.700 4.600-1.700L11 4Zm7 10.500.8 2.200 2.200.8-2.200.8-.8 2.200-.8-2.200-2.200-.8 2.200-.8.8-2.200Z",59 copy: "M9.500 9h9A1.500 1.500 0 0 1 20 10.500v9a1.500 1.500 0 0 1-1.500 1.500h-9A1.500 1.500 0 0 1 8 19.500v-9A1.500 1.500 0 0 1 9.500 9ZM16 6V4.500A1.500 1.500 0 0 0 14.500 3h-9A1.500 1.500 0 0 0 4 4.500v9A1.500 1.500 0 0 0 5.500 15H6",60 phone: "M8.500 3.500h7A1.500 1.500 0 0 1 17 5v14a1.500 1.500 0 0 1-1.500 1.500h-7A1.500 1.500 0 0 1 7 19V5a1.500 1.500 0 0 1 1.500-1.500Zm2.500 14h2",61 desktop: "M4 5.500h16v10.500H4V5.500Zm5 14.500h6m-3-4v4",62 sun: "M12 16a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm0-13v2m0 14v2M3 12h2m14 0h2M5.600 5.600 7 7m10 10 1.400 1.400M5.600 18.400 7 17M17 7l1.400-1.400",63 moon: "M19.500 14.500A8 8 0 0 1 9.500 4.500a8 8 0 1 0 10 10Z",64 fit: "M4 9V5h4m12 4V5h-4M4 15v4h4m12-4v4h-4",65 braces: "M8 4c-2 0-2.500 1-2.500 2.500v2c0 1.500-.500 2.500-2 3.500 1.500 1 2 2 2 3.500v2C5.500 19 6 20 8 20m8-16c2 0 2.500 1 2.500 2.500v2c0 1.500.500 2.500 2 3.500-1.500 1-2 2-2 3.500v2c0 1.500-.500 2.500-2.500 2.500",66 wrap: "M4 6h16M4 12h13a3 3 0 0 1 0 6h-4m0 0 2-2m-2 2 2 2M4 18h5",67 eye: "M2.500 12S6 5.500 12 5.500 21.500 12 21.500 12 18 18.500 12 18.500 2.500 12 2.500 12Zm9.500 3a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z",68 // ---- SP Mail Next69 panel: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm5-2v16",70 command: "M9 9V6.5A2.5 2.5 0 1 0 6.5 9H9Zm0 0h6M9 9v6m6-6V6.5A2.5 2.5 0 1 1 17.5 9H15Zm0 0v6m0 0H9m6 0v2.5a2.5 2.5 0 1 0 2.5-2.5H15Zm-6 0v2.5A2.5 2.5 0 1 1 6.5 15H9Z",71 snooze: "M12 20.5a8.5 8.5 0 1 0 0-17 8.5 8.5 0 0 0 0 17ZM9.5 9.5h5l-5 5h5",72 tag: "M4 5.5A1.5 1.5 0 0 1 5.5 4h6.2a1.5 1.5 0 0 1 1.1.4l6.8 6.8a1.5 1.5 0 0 1 0 2.1l-6.2 6.2a1.5 1.5 0 0 1-2.1 0L4.4 12.7a1.5 1.5 0 0 1-.4-1V5.5Zm4.5 3h.01",73 sync: "M20 12a8 8 0 0 1-14.3 4.9M4 12a8 8 0 0 1 14.3-4.9m.2-3.6v4h-4m-9 13v-4h4",74 chevronRight: "m10 8 4 4-4 4",75 layoutRight: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm8-2v16",76 layoutBelow: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm0 5h16",77 layoutNone: "M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6Zm4 3h8m-8 3h8m-8 3h5",78 contrast: "M12 20.5a8.5 8.5 0 1 0 0-17 8.5 8.5 0 0 0 0 17Zm0-17v17m0-11 5.6 5.6M12 14.5l3.6 3.6",79 bolt: "M13 3.5 5.5 13.5H11l-1 7 7.5-10H12l1-7Z",80 text: "M5 7V5h14v2M12 5v14m-3 0h6",81 dots: "M12 6h.01M12 12h.01M12 18h.01",82 // ---- SP Mail Next : productivité et intelligence83 users: "M9 11a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7Zm-6.500 9a6.500 6.500 0 0 1 13 0M16 4.500a3.500 3.500 0 0 1 0 6.500m2 3a6.500 6.500 0 0 1 3.500 6",84 userPlus: "M10 11a3.500 3.500 0 1 0 0-7 3.500 3.500 0 0 0 0 7Zm-6.500 9a6.500 6.500 0 0 1 13 0M19 8v6m-3-3h6",85 grid: "M4 4h7v7H4V4Zm9 0h7v7h-7V4ZM4 13h7v7H4v-7Zm9 0h7v7h-7v-7Z",86 bell: "M6 16.500V11a6 6 0 1 1 12 0v5.500l1.500 2h-15l1.500-2Zm4 4.500a2 2 0 0 0 4 0",87 bellOff: "M6 16.500V11a6 6 0 0 1 1.200-3.600M18 14.500V11a6 6 0 0 0-8.500-5.500M4.500 18.500H17M10 21a2 2 0 0 0 4 0M4 4l16 16",88 calendar: "M5 6a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V6Zm0 4h14M9 3v4m6-4v4",89 alarm: "M12 20a7 7 0 1 0 0-14 7 7 0 0 0 0 14Zm0-10v3.500l2 1.500M5 4 3 6m16-2 2 2",90 building: "M5 20V5a1 1 0 0 1 1-1h8a1 1 0 0 1 1 1v15m0-10h3a1 1 0 0 1 1 1v9M3 20h18M8.500 8h3m-3 4h3m-3 4h3",91 merge: "M6 4v4a4 4 0 0 0 4 4h4a4 4 0 0 1 4 4v4M18 4v4a4 4 0 0 1-4 4m-8 8v-4a4 4 0 0 1 4-4",92 upload: "M12 16V4m0 0L7.500 8.500M12 4l4.500 4.500M5 16v2a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-2",93 grip: "M9 6h.010M15 6h.010M9 12h.010M15 12h.010M9 18h.010M15 18h.010",94 rule: "M4 6h9m4 0h3M4 12h3m4 0h9M4 18h11m4 0h1M15 4v4M9 10v4m8 2v4",95 save: "M5 5a1 1 0 0 1 1-1h10l3 3v12a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5Zm3-1v5h7V4M8 20v-6h8v6",96 zip: "M6 4h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1Zm5 0v2m2 1v2m-2 1v2m2 1v2m-2 0h2v3h-2v-3Z",97 heart: "M12 20s-7.500-4.500-7.500-10A4.300 4.300 0 0 1 12 7.500 4.300 4.300 0 0 1 19.500 10c0 5.500-7.500 10-7.500 10Z",98 crown: "m4 8 4 4 4-7 4 7 4-4-1.500 10h-13L4 8Z",99 chart: "M4 20V4m0 16h16M8 16v-4m4 4V8m4 8v-6",100 music: "M9 18V6l10-2v12M9 18a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0Zm10-2a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0Z",101 film: "M5 4h14a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1Zm3 0v16m8-16v16M4 9h4m8 0h4M4 15h4m8 0h4",102 arrowUp: "M12 19V5m-6 6 6-6 6 6",103 arrowDown: "M12 5v14m-6-6 6 6 6-6",104} as const105export type IconName = keyof typeof PATHS106107export function Icon({ name, small, className }: { name: IconName; small?: boolean; className?: string }) {108 return (109 <svg className={`icon${small ? " s" : ""}${className ? ` ${className}` : ""}`} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" focusable="false">110 <path d={PATHS[name]} />111 </svg>112 )113}114115/** Pastille d'initiales, teintée de façon stable d'après l'adresse (purement décorative : le nom est toujours écrit à côté). */116export function Avatar({ addr, small, className }: { addr: Addr | undefined | null; small?: boolean; className?: string }) {117 return (118 <span className={`avatar${small ? " s" : ""}${className ? ` ${className}` : ""}`} style={{ "--h": addressHue(addr?.email ?? "") } as CSSProperties} aria-hidden="true">119 {initials(addr)}120 </span>121 )122}123124/** Suit une requête média (point de rupture, mouvement réduit, pointeur). */125export function useMedia(query: string): boolean {126 return useSyncExternalStore(127 (notify) => {128 const m = matchMedia(query)129 m.addEventListener("change", notify)130 return () => m.removeEventListener("change", notify)131 },132 () => matchMedia(query).matches,133 )134}135export const PHONE = "(max-width: 760px)"136export const NARROW = "(max-width: 1100px)"137export const REDUCED_MOTION = "(prefers-reduced-motion: reduce)"138139export function Spinner({ label }: { label?: string }) {140 return (141 <span className="row" role="status">142 <span className="spinner" aria-hidden="true" />143 <span className={label ? "muted small" : "sr-only"}>{label ?? t("common.loading")}</span>144 </span>145 )146}147148export function IconButton({ icon, label, onClick, pressed, disabled, className, shortcut }: { icon: IconName; label: string; onClick?: () => void; pressed?: boolean; disabled?: boolean; className?: string; shortcut?: string }) {149 return (150 <button type="button" className={`icon-btn${className ? ` ${className}` : ""}`} aria-label={label} title={shortcut ? `${label} (${shortcut})` : label} aria-pressed={pressed} disabled={disabled} onClick={onClick}>151 <Icon name={icon} />152 </button>153 )154}155156// ---------------------------------------------------------------- dialogue modal157const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"]),[contenteditable="true"]'158159/** Piège le focus dans un conteneur, le rend à l'élément d'origine à la fermeture, ferme sur Échap. */160export function useModal(onClose: () => void) {161 const ref = useRef<HTMLDivElement>(null)162 const closeRef = useRef(onClose)163 closeRef.current = onClose164 useLayoutEffect(() => {165 const previous = document.activeElement as HTMLElement | null166 const node = ref.current167 if (node) {168 const auto = node.querySelector<HTMLElement>("[data-autofocus]") ?? node.querySelector<HTMLElement>(FOCUSABLE) ?? node169 auto.focus()170 }171 const onKey = (e: KeyboardEvent) => {172 // dialogues empilés (invite de clé d'accès par-dessus un formulaire) : seul celui du dessus traite le clavier173 const modals = document.querySelectorAll('[aria-modal="true"]')174 if (node && modals.length > 1 && modals[modals.length - 1] !== node && !node.contains(modals[modals.length - 1])) return175 if (e.key === "Escape") {176 e.stopPropagation()177 closeRef.current()178 return179 }180 if (e.key !== "Tab" || !node) return181 const items = Array.from(node.querySelectorAll<HTMLElement>(FOCUSABLE)).filter((el) => el.offsetParent !== null || el === document.activeElement)182 if (!items.length) return e.preventDefault()183 const first = items[0]184 const last = items[items.length - 1]185 if (e.shiftKey && (document.activeElement === first || !node.contains(document.activeElement))) {186 e.preventDefault()187 last.focus()188 } else if (!e.shiftKey && document.activeElement === last) {189 e.preventDefault()190 first.focus()191 }192 }193 document.addEventListener("keydown", onKey, true)194 return () => {195 document.removeEventListener("keydown", onKey, true)196 if (previous && document.contains(previous)) previous.focus()197 }198 }, [])199 return ref200}201202export function Dialog({ title, onClose, children, footer, wide, alert }: { title: string; onClose: () => void; children: ReactNode; footer?: ReactNode; wide?: boolean; alert?: boolean }) {203 const ref = useModal(onClose)204 const id = useId()205 return (206 <div className="overlay" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>207 <div ref={ref} className={`dialog${wide ? " wide" : ""}`} role={alert ? "alertdialog" : "dialog"} aria-modal="true" aria-labelledby={id} tabIndex={-1}>208 <div className="dialog-head">209 <h2 id={id}>{title}</h2>210 <IconButton icon="close" label={t("common.close")} onClick={onClose} />211 </div>212 <div className="dialog-body">{children}</div>213 {footer ? <div className="dialog-foot">{footer}</div> : null}214 </div>215 </div>216 )217}218219export function ConfirmDialog({ title, body, confirmLabel, danger, onConfirm, onClose, requireText }: { title: string; body: ReactNode; confirmLabel: string; danger?: boolean; onConfirm: () => void | Promise<void>; onClose: () => void; /** exige de retaper ce texte (action irréversible) */ requireText?: string }) {220 const [typed, setTyped] = useState("")221 const [busy, setBusy] = useState(false)222 const ok = !requireText || typed.trim().toLowerCase() === requireText.toLowerCase()223 const inputId = useId()224 return (225 <Dialog226 title={title}227 onClose={onClose}228 alert229 footer={230 <>231 <button type="button" className="btn" onClick={onClose} data-autofocus={requireText ? undefined : true}>232 {t("common.cancel")}233 </button>234 <button235 type="button"236 className={`btn ${danger ? "danger solid" : "primary"}`}237 disabled={!ok || busy}238 onClick={async () => {239 setBusy(true)240 try {241 await onConfirm()242 } finally {243 setBusy(false)244 onClose()245 }246 }}247 >248 {confirmLabel}249 </button>250 </>251 }252 >253 <div className="stack">254 <div>{body}</div>255 {requireText ? (256 <div className="field">257 <label htmlFor={inputId}>{t("common.typeToConfirm", { text: requireText })}</label>258 <input id={inputId} className="input mono" value={typed} onChange={(e) => setTyped(e.target.value)} autoComplete="off" spellCheck={false} data-autofocus />259 </div>260 ) : null}261 </div>262 </Dialog>263 )264}265266export function PromptDialog({ title, label, initial = "", confirmLabel, onSubmit, onClose }: { title: string; label: string; initial?: string; confirmLabel: string; onSubmit: (value: string) => void | Promise<void>; onClose: () => void }) {267 const [value, setValue] = useState(initial)268 const [busy, setBusy] = useState(false)269 const id = useId()270 const submit = async () => {271 if (!value.trim() || busy) return272 setBusy(true)273 try {274 await onSubmit(value.trim())275 onClose()276 } finally {277 setBusy(false)278 }279 }280 return (281 <Dialog282 title={title}283 onClose={onClose}284 footer={285 <>286 <button type="button" className="btn" onClick={onClose}>287 {t("common.cancel")}288 </button>289 <button type="button" className="btn primary" disabled={!value.trim() || busy} onClick={() => void submit()}>290 {confirmLabel}291 </button>292 </>293 }294 >295 <form296 className="field"297 onSubmit={(e) => {298 e.preventDefault()299 void submit()300 }}301 >302 <label htmlFor={id}>{label}</label>303 <input id={id} className="input" value={value} onChange={(e) => setValue(e.target.value)} maxLength={120} data-autofocus />304 </form>305 </Dialog>306 )307}308309// ---------------------------------------------------------------- menu déroulant310export interface MenuItem {311 label: string312 icon?: IconName313 onSelect: () => void314 danger?: boolean315 disabled?: boolean316 /** coche à droite (choix courant d'un groupe) */317 checked?: boolean318}319export function MenuButton({ label, icon = "more", items, className, text, align = "right" }: { label: string; icon?: IconName; items: (MenuItem | "sep" | { heading: string })[]; className?: string; text?: string; align?: "left" | "right" }) {320 const [open, setOpen] = useState(false)321 const wrap = useRef<HTMLSpanElement>(null)322 const menu = useRef<HTMLDivElement>(null)323 useEffect(() => {324 if (!open) return325 menu.current?.querySelector<HTMLElement>("button:not([disabled])")?.focus()326 const away = (e: MouseEvent) => {327 if (!wrap.current?.contains(e.target as Node)) setOpen(false)328 }329 document.addEventListener("mousedown", away)330 return () => document.removeEventListener("mousedown", away)331 }, [open])332 const onKey = (e: React.KeyboardEvent) => {333 if (e.key === "Escape") {334 e.stopPropagation()335 setOpen(false)336 wrap.current?.querySelector<HTMLElement>("button")?.focus()337 return338 }339 if (e.key !== "ArrowDown" && e.key !== "ArrowUp" && e.key !== "Home" && e.key !== "End") return340 e.preventDefault()341 const buttons = Array.from(menu.current?.querySelectorAll<HTMLElement>("button:not([disabled])") ?? [])342 const i = buttons.indexOf(document.activeElement as HTMLElement)343 const next = e.key === "Home" ? 0 : e.key === "End" ? buttons.length - 1 : (i + (e.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length344 buttons[next]?.focus()345 }346 return (347 <span ref={wrap} style={{ position: "relative", display: "inline-flex" }} onKeyDown={onKey}>348 <button type="button" className={text ? `btn ${className ?? ""}` : `icon-btn ${className ?? ""}`} aria-haspopup="menu" aria-expanded={open} aria-label={text ? undefined : label} title={label} onClick={() => setOpen((o) => !o)}>349 <Icon name={icon} />350 {text ? <span>{text}</span> : null}351 </button>352 {open ? (353 <div ref={menu} className="menu" role="menu" aria-label={label} style={align === "right" ? { right: 0, top: "calc(100% + 4px)" } : { left: 0, top: "calc(100% + 4px)" }}>354 {items.map((it, i) =>355 it === "sep" ? (356 <div key={i} className="sep" role="separator" />357 ) : "heading" in it ? (358 <div key={i} className="menu-label">359 {it.heading}360 </div>361 ) : (362 <button363 key={i}364 type="button"365 role={it.checked === undefined ? "menuitem" : "menuitemradio"}366 aria-checked={it.checked}367 disabled={it.disabled}368 style={it.danger ? { color: "var(--danger)" } : undefined}369 onClick={() => {370 setOpen(false)371 it.onSelect()372 }}373 >374 {it.icon ? <Icon name={it.icon} small /> : null}375 <span className="truncate">{it.label}</span>376 {it.checked ? <Icon name="check" small className="menu-check" /> : null}377 </button>378 ),379 )}380 </div>381 ) : null}382 </span>383 )384}385386// ---------------------------------------------------------------- notifications387function ToastItem({ toast }: { toast: Toast }) {388 // le compte à rebours s'arrête tant que la notification est survolée ou a le focus389 const [held, setHeld] = useState(false)390 useEffect(() => {391 if (held) return392 const timer = setTimeout(() => dismissToast(toast.id), toast.timeout)393 return () => clearTimeout(timer)394 }, [toast.id, toast.timeout, held])395 return (396 <div className={`toast ${toast.kind}`} role={toast.kind === "error" ? "alert" : "status"} onMouseEnter={() => setHeld(true)} onMouseLeave={() => setHeld(false)} onFocus={() => setHeld(true)} onBlur={() => setHeld(false)}>397 <span className="toast-text">398 {toast.text}399 {toast.detail ? <span className="toast-detail">{toast.detail}</span> : null}400 </span>401 {toast.action && toast.actionLabel ? (402 <button403 type="button"404 className="toast-action"405 onClick={() => {406 toast.action?.()407 dismissToast(toast.id)408 }}409 >410 {toast.actionLabel}411 </button>412 ) : null}413 <button type="button" aria-label={t("common.dismiss")} onClick={() => dismissToast(toast.id)}>414 <Icon name="close" small />415 </button>416 </div>417 )418}419export function Toasts() {420 const toasts = useStore(S, (s) => s.toasts)421 return (422 <div className="toasts" aria-live="polite">423 {toasts.map((x) => (424 <ToastItem key={x.id} toast={x} />425 ))}426 </div>427 )428}429430// ---------------------------------------------------------------- garde d'erreurs431export class ErrorBoundary extends Component<{ children: ReactNode; compact?: boolean }, { error: Error | null }> {432 state: { error: Error | null } = { error: null }433 static getDerivedStateFromError(error: Error) {434 return { error }435 }436 render() {437 if (!this.state.error) return this.props.children438 return (439 <div className="empty" role="alert">440 <Icon name="warn" />441 <h2>{t("error.boundaryTitle")}</h2>442 <p>{t("error.boundaryBody")}</p>443 <div className="row">444 <button type="button" className="btn" onClick={() => this.setState({ error: null })}>445 {t("common.retry")}446 </button>447 {this.props.compact ? null : (448 <button type="button" className="btn primary" onClick={() => location.reload()}>449 {t("common.reload")}450 </button>451 )}452 </div>453 <p className="small muted mono">{this.state.error.message.slice(0, 200)}</p>454 </div>455 )456 }457}458459/** Champ de formulaire étiqueté. */460export function Field({ label, hint, children, id }: { label: string; hint?: string; id: string; children: ReactNode }) {461 return (462 <div className="field">463 <label htmlFor={id}>{label}</label>464 {children}465 {hint ? <span className="hint">{hint}</span> : null}466 </div>467 )468}469470// ---------------------------------------------------------------- confirmation impérative (raccourcis clavier, actions de liste)471interface Ask {472 title: string473 body: ReactNode474 confirmLabel: string475 danger?: boolean476 resolve: (ok: boolean) => void477}478let setAsk: ((a: Ask | null) => void) | null = null479/** Demande une confirmation ; résout à `true` si la personne confirme. */480export function confirmAsync(q: Omit<Ask, "resolve">): Promise<boolean> {481 return new Promise((resolve) => {482 if (!setAsk) return resolve(false)483 setAsk({ ...q, resolve })484 })485}486export function ConfirmHost() {487 const [ask, set] = useState<Ask | null>(null)488 useEffect(() => {489 setAsk = set490 return () => {491 setAsk = null492 }493 }, [])494 if (!ask) return null495 let answered = false496 return (497 <ConfirmDialog498 title={ask.title}499 body={ask.body}500 confirmLabel={ask.confirmLabel}501 danger={ask.danger}502 onConfirm={() => {503 answered = true504 ask.resolve(true)505 }}506 onClose={() => {507 if (!answered) ask.resolve(false)508 set(null)509 }}510 />511 )512}513