Standardize modal focus and keyboard behavior
This commit is contained in:
parent
e87edda5c7
commit
954240adf9
15 changed files with 331 additions and 139 deletions
|
|
@ -1,9 +1,8 @@
|
|||
// Rôle : copie ou crée en checklist les données visibles des pages de jeu.
|
||||
/* eslint-disable react-refresh/only-export-components -- expose aussi les formatters publics testés pour l'import checklist. */
|
||||
import { useEffect, useId, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { useId, useState } from "react";
|
||||
import { Icon } from "../../components/Icon.jsx";
|
||||
import { lockBodyScroll } from "../../utils/bodyScrollLock.js";
|
||||
import { Modal } from "../../components/Modal.jsx";
|
||||
|
||||
async function copyText(value) {
|
||||
try {
|
||||
|
|
@ -52,19 +51,6 @@ function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, o
|
|||
const [creating, setCreating] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
function handleKeyDown(event) {
|
||||
if (event.key === "Escape" && !creating) onClose();
|
||||
}
|
||||
|
||||
const unlock = lockBodyScroll();
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
unlock();
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [creating, onClose]);
|
||||
|
||||
async function copyItems() {
|
||||
setError("");
|
||||
if (!await copyText(text)) {
|
||||
|
|
@ -89,10 +75,8 @@ function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, o
|
|||
}
|
||||
}
|
||||
|
||||
return createPortal(
|
||||
<div className="confirm-modal-root results-checklist-modal-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={() => !creating && onClose()} />
|
||||
<section className="confirm-modal nds-modal results-checklist-modal" role="dialog" aria-modal="true" aria-labelledby={titleId} aria-describedby={descriptionId}>
|
||||
return (
|
||||
<Modal rootClassName="confirm-modal-root results-checklist-modal-root" dialogClassName="confirm-modal nds-modal results-checklist-modal" labelledBy={titleId} describedBy={descriptionId} onRequestClose={onClose} canClose={!creating}>
|
||||
<header className="results-checklist-modal-header">
|
||||
<h2 id={titleId}>{content.resultsModalTitle || "Utiliser les résultats visibles"}</h2>
|
||||
<button className="nds-icon-button" data-size="sm" type="button" disabled={creating} onClick={onClose} aria-label={content.resultsClose || "Fermer"} title={content.resultsClose || "Fermer"}>
|
||||
|
|
@ -119,7 +103,7 @@ function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, o
|
|||
)}
|
||||
{error && <p className="results-checklist-modal-error nds-inline-notice" data-variant="danger" role="alert"><Icon name="close" /><span>{error}</span></p>}
|
||||
<footer className="nds-modal-footer results-checklist-modal-actions">
|
||||
<button className="nds-button" data-size="sm" type="button" disabled={creating} onClick={() => void copyItems()} autoFocus>
|
||||
<button className="nds-button" data-size="sm" type="button" disabled={creating} onClick={() => void copyItems()} data-modal-initial-focus="">
|
||||
<Icon name="copy" />{content.resultsCopyLabel || "Copier le format texte"}
|
||||
</button>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={!linkedToolbox || creating} onClick={() => void createChecklist()}>
|
||||
|
|
@ -127,9 +111,7 @@ function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, o
|
|||
{creating ? content.resultsCreatingLabel || "Création…" : content.resultsCreateLabel || "Créer dans la toolbox"}
|
||||
</button>
|
||||
</footer>
|
||||
</section>
|
||||
</div>,
|
||||
document.body
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
/* eslint-disable react-refresh/only-export-components -- expose aussi les helpers purs de calcul/export du crop. */
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Icon } from "../../components/Icon.jsx";
|
||||
import { Modal } from "../../components/Modal.jsx";
|
||||
import { useKeyedState } from "../../hooks/useKeyedState.js";
|
||||
import { lockBodyScroll } from "../../utils/bodyScrollLock.js";
|
||||
|
||||
const OUTPUT_WIDTH = 620;
|
||||
const OUTPUT_HEIGHT = 248;
|
||||
|
|
@ -75,8 +75,6 @@ export function ToolboxCoverEditor({ file, existingCover = "", onClose, onSave }
|
|||
const [transform, setTransform] = useKeyedState(transformKey, () => ({ x: 0, y: 0, scale: baseScale }));
|
||||
const maxScale = baseScale * 4;
|
||||
|
||||
useEffect(() => lockBodyScroll(), []);
|
||||
|
||||
useEffect(() => {
|
||||
const nextSource = createPreviewSource(file, existingCover);
|
||||
if (!nextSource) return undefined;
|
||||
|
|
@ -160,9 +158,7 @@ export function ToolboxCoverEditor({ file, existingCover = "", onClose, onSave }
|
|||
}
|
||||
|
||||
return (
|
||||
<div className="confirm-modal-root toolbox-cover-editor-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={onClose} />
|
||||
<section className="confirm-modal nds-modal toolbox-cover-editor" role="dialog" aria-modal="true" aria-labelledby="toolbox-cover-editor-title">
|
||||
<Modal rootClassName="confirm-modal-root toolbox-cover-editor-root" dialogClassName="confirm-modal nds-modal toolbox-cover-editor" labelledBy="toolbox-cover-editor-title" onRequestClose={onClose}>
|
||||
<header>
|
||||
<h2 id="toolbox-cover-editor-title">Image de card</h2>
|
||||
<button className="nds-icon-button" data-size="xs" type="button" onClick={onClose} aria-label="Fermer" title="Fermer"><Icon name="close" /></button>
|
||||
|
|
@ -198,7 +194,6 @@ export function ToolboxCoverEditor({ file, existingCover = "", onClose, onSave }
|
|||
<button className="nds-button" data-size="sm" type="button" onClick={onClose}>Annuler</button>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={!imageSize.width || Boolean(error)} onClick={saveCover}>Valider</button>
|
||||
</footer>
|
||||
</section>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
// Rôle : affiche et gère les onglets d'espaces persistants d'une toolbox.
|
||||
import { useEffect, useState } from "react";
|
||||
import { Icon } from "../../components/Icon.jsx";
|
||||
import { Modal } from "../../components/Modal.jsx";
|
||||
import { useGroupedReorder } from "../../hooks/useGroupedReorder.js";
|
||||
import { lockBodyScroll } from "../../utils/bodyScrollLock.js";
|
||||
import { MAX_TOOLBOX_SPACES } from "./storage/toolboxStorage.js";
|
||||
import { createToolboxSpace, deleteToolboxSpace, reorderToolboxSpaces } from "./toolboxSpaceUtils.js";
|
||||
|
||||
|
|
@ -10,24 +10,20 @@ const SPACE_HOVER_DELAY_MS = 600;
|
|||
|
||||
function SpaceNameModal({ initialName = "", title, text, onClose }) {
|
||||
const [name, setName] = useState(initialName);
|
||||
useEffect(() => lockBodyScroll(), []);
|
||||
return (
|
||||
<div className="confirm-modal-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={() => onClose("")} />
|
||||
<section className="confirm-modal nds-modal toolbox-space-modal" role="dialog" aria-modal="true" aria-labelledby="toolbox-space-modal-title">
|
||||
<Modal dialogClassName="confirm-modal nds-modal toolbox-space-modal" labelledBy="toolbox-space-modal-title" onRequestClose={() => onClose("")}>
|
||||
<header><h2 id="toolbox-space-modal-title">{title}</h2></header>
|
||||
<form onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
if (name.trim()) onClose(name.trim());
|
||||
}}>
|
||||
<label className="field"><span>{text.nameLabel}</span><input className="nds-input" data-size="sm" value={name} onChange={(event) => setName(event.target.value)} autoComplete="off" autoFocus required /></label>
|
||||
<label className="field"><span>{text.nameLabel}</span><input className="nds-input" data-size="sm" value={name} onChange={(event) => setName(event.target.value)} autoComplete="off" data-modal-initial-focus="" required /></label>
|
||||
<footer className="nds-modal-footer">
|
||||
<button className="nds-button" data-size="sm" type="button" onClick={() => onClose("")}>{text.cancelLabel}</button>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="submit">{text.saveLabel}</button>
|
||||
</footer>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
// Rôle : rend les contrôles modal et quick-add d'ajout d'outils toolbox.
|
||||
import { useEffect, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { useState } from "react";
|
||||
import { CompactDropdown } from "../../../components/CompactDropdown.jsx";
|
||||
import { Icon } from "../../../components/Icon.jsx";
|
||||
import { Modal } from "../../../components/Modal.jsx";
|
||||
import { Tooltip } from "../../../components/Tooltip.jsx";
|
||||
import { lockBodyScroll } from "../../../utils/bodyScrollLock.js";
|
||||
import { LIBRARY_CATEGORY_DEFINITIONS, TOOLBOX_MODULES } from "../toolboxModuleRegistry.js";
|
||||
|
||||
export function AddToolControls({ categories, descriptions, onAdd }) {
|
||||
|
|
@ -15,11 +14,6 @@ export function AddToolControls({ categories, descriptions, onAdd }) {
|
|||
title: categories[category.key]
|
||||
}));
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return undefined;
|
||||
return lockBodyScroll();
|
||||
}, [open]);
|
||||
|
||||
function addTool(type) {
|
||||
onAdd(type);
|
||||
setOpen(false);
|
||||
|
|
@ -28,7 +22,7 @@ export function AddToolControls({ categories, descriptions, onAdd }) {
|
|||
function renderToolCard(type) {
|
||||
const module = TOOLBOX_MODULES[type];
|
||||
return (
|
||||
<button key={type} className="tool-add-card" type="button" onClick={() => addTool(type)}>
|
||||
<button key={type} className="tool-add-card" type="button" data-modal-initial-focus={type === moduleCategories[0]?.types[0] ? "" : undefined} onClick={() => addTool(type)}>
|
||||
<span className="module-icon" aria-hidden="true">
|
||||
<span className={`module-icon-svg module-icon-${module.icon}`} />
|
||||
</span>
|
||||
|
|
@ -103,10 +97,8 @@ export function AddToolControls({ categories, descriptions, onAdd }) {
|
|||
)}
|
||||
</CompactDropdown>
|
||||
</div>
|
||||
{open && createPortal(
|
||||
<div className="tool-add-modal-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={() => setOpen(false)} />
|
||||
<section className="confirm-modal nds-modal tool-add-modal" role="dialog" aria-modal="true" aria-labelledby="tool-add-modal-title">
|
||||
{open && (
|
||||
<Modal rootClassName="tool-add-modal-root" dialogClassName="confirm-modal nds-modal tool-add-modal" labelledBy="tool-add-modal-title" onRequestClose={() => setOpen(false)}>
|
||||
<header>
|
||||
<h2 id="tool-add-modal-title">Ajouter un outil</h2>
|
||||
<button className="drawer-close-button nds-icon-button" data-size="sm" type="button" onClick={() => setOpen(false)} aria-label="Fermer" title="Fermer">
|
||||
|
|
@ -121,9 +113,7 @@ export function AddToolControls({ categories, descriptions, onAdd }) {
|
|||
</section>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
</div>,
|
||||
document.body
|
||||
</Modal>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,30 +1,10 @@
|
|||
// Rôle : affiche la modale d'import/export texte partagée par les outils compatibles.
|
||||
import { useEffect } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { Icon } from "../../../components/Icon.jsx";
|
||||
import { lockBodyScroll } from "../../../utils/bodyScrollLock.js";
|
||||
import { Modal } from "../../../components/Modal.jsx";
|
||||
|
||||
export function TextImportModal({ title, value, placeholder, submitLabel, readOnly = false, error = "", info = "", onChange, onSubmit, onCopy, onClose }) {
|
||||
useEffect(() => {
|
||||
function handleKeyDown(event) {
|
||||
if (event.key === "Escape") {
|
||||
event.stopPropagation();
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
|
||||
const unlock = lockBodyScroll();
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
unlock();
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [onClose]);
|
||||
|
||||
return createPortal(
|
||||
<div className="confirm-modal-root text-import-modal-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={onClose} />
|
||||
<section className="confirm-modal nds-modal text-import-modal-dialog" role="dialog" aria-modal="true" aria-labelledby="text-import-title">
|
||||
return (
|
||||
<Modal rootClassName="confirm-modal-root text-import-modal-root" dialogClassName="confirm-modal nds-modal text-import-modal-dialog" labelledBy="text-import-title" onRequestClose={onClose}>
|
||||
<header>
|
||||
<h2 id="text-import-title">{title}</h2>
|
||||
<button className="drawer-close-button nds-icon-button" data-size="sm" type="button" onClick={onClose} aria-label="Fermer" title="Fermer">
|
||||
|
|
@ -33,7 +13,7 @@ export function TextImportModal({ title, value, placeholder, submitLabel, readOn
|
|||
</header>
|
||||
<form className="text-import-modal-form" onSubmit={onSubmit}>
|
||||
<textarea
|
||||
autoFocus
|
||||
data-modal-initial-focus=""
|
||||
className={`nds-textarea ${error ? "has-error" : ""}`.trim()}
|
||||
data-size="sm"
|
||||
readOnly={readOnly}
|
||||
|
|
@ -65,8 +45,6 @@ export function TextImportModal({ title, value, placeholder, submitLabel, readOn
|
|||
)}
|
||||
</footer>
|
||||
</form>
|
||||
</section>
|
||||
</div>,
|
||||
document.body
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue