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>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue