wulf.js 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -7
- package/changelog.md +21 -0
- package/dist/focus-trap.cjs.js +430 -0
- package/dist/focus-trap.cjs.js.map +7 -0
- package/dist/focus-trap.esm.js +407 -0
- package/dist/focus-trap.esm.js.map +7 -0
- package/dist/focus-trap.global.js +433 -0
- package/dist/focus-trap.global.js.map +7 -0
- package/dist/focus-trap.global.min.js +6 -0
- package/dist/focus-trap.global.min.js.map +7 -0
- package/dist/index.cjs.js +590 -8
- package/dist/index.cjs.js.map +3 -3
- package/dist/index.esm.js +590 -8
- package/dist/index.esm.js.map +3 -3
- package/dist/index.global.js +590 -8
- package/dist/index.global.js.map +3 -3
- package/dist/index.global.min.js +4 -1
- package/dist/index.global.min.js.map +4 -4
- package/dist/mask.cjs.js +106 -2
- package/dist/mask.cjs.js.map +3 -3
- package/dist/mask.esm.js +106 -2
- package/dist/mask.esm.js.map +3 -3
- package/dist/mask.global.js +106 -2
- package/dist/mask.global.js.map +3 -3
- package/dist/mask.global.min.js +4 -1
- package/dist/mask.global.min.js.map +4 -4
- package/dist/masonry.cjs.js +78 -3
- package/dist/masonry.cjs.js.map +3 -3
- package/dist/masonry.esm.js +78 -3
- package/dist/masonry.esm.js.map +3 -3
- package/dist/masonry.global.js +78 -3
- package/dist/masonry.global.js.map +3 -3
- package/dist/masonry.global.min.js +4 -1
- package/dist/masonry.global.min.js.map +4 -4
- package/dist/scroll.cjs.js +78 -3
- package/dist/scroll.cjs.js.map +3 -3
- package/dist/scroll.esm.js +78 -3
- package/dist/scroll.esm.js.map +3 -3
- package/dist/scroll.global.js +78 -3
- package/dist/scroll.global.js.map +3 -3
- package/dist/scroll.global.min.js +4 -1
- package/dist/scroll.global.min.js.map +4 -4
- package/dist/toast.cjs.js +91 -2
- package/dist/toast.cjs.js.map +3 -3
- package/dist/toast.esm.js +91 -2
- package/dist/toast.esm.js.map +3 -3
- package/dist/toast.global.js +91 -2
- package/dist/toast.global.js.map +3 -3
- package/dist/toast.global.min.js +4 -1
- package/dist/toast.global.min.js.map +4 -4
- package/dist/types/common/AuditableComponent.d.ts +45 -0
- package/dist/types/common/LocalizedComponent.d.ts +3 -2
- package/dist/types/focus-trap/FocusTrap.d.ts +64 -0
- package/dist/types/focus-trap/index.d.ts +3 -0
- package/dist/types/focus-trap/types.d.ts +45 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/mask/BaseInputMask.d.ts +14 -1
- package/dist/types/mask/InputMask.d.ts +1 -0
- package/dist/types/mask/types.d.ts +5 -2
- package/dist/types/masonry/Masonry.d.ts +7 -0
- package/dist/types/masonry/types.d.ts +7 -2
- package/dist/types/scroll/ScrollBlock.d.ts +7 -0
- package/dist/types/scroll/types.d.ts +3 -2
- package/dist/types/toast/Toast.d.ts +3 -0
- package/dist/types/toast/ToastInstance.d.ts +9 -1
- package/dist/types/toast/types.d.ts +3 -2
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -36,11 +36,16 @@
|
|
|
36
36
|
- [Options de Configuration](#options-de-configuration-toast)
|
|
37
37
|
- [Événements & Callbacks](#événements--callbacks-toast)
|
|
38
38
|
- [Personnalisation CSS & Couleurs](#personnalisation-css--couleurs-toast)
|
|
39
|
-
5. [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
5. [🎯 Section 5 : Focus Trap Accessible (FocusTrap)](#-section-5--focus-trap-accessible-focustrap)
|
|
40
|
+
- [Vision & Axiome du Flux Naturel](#vision--axiome-du-flux-naturel-focustrap)
|
|
41
|
+
- [Utilisation TypeScript / Vanilla](#utilisation-typescript--vanilla-focustrap)
|
|
42
|
+
- [Options de Configuration](#options-de-configuration-focustrap)
|
|
43
|
+
- [Gestion de Pile LIFO & Sentinelles](#gestion-de-pile-lifo--sentinelles)
|
|
44
|
+
6. [📦 Installation & Imports Modulaires](#-installation--imports-modulaires)
|
|
45
|
+
7. [🧪 Tests Unitaires & Intégration](#-tests-unitaires--intégration)
|
|
46
|
+
8. [🛠️ Compilation & Build](#-compilation--build)
|
|
47
|
+
9. [🚀 Versioning & Releases GitLab](#-versioning--releases-gitlab)
|
|
48
|
+
10. [📄 Licence](#-licence)
|
|
44
49
|
|
|
45
50
|
---
|
|
46
51
|
|
|
@@ -749,6 +754,59 @@ Toutes les notifications sont encapsulées dans des conteneurs fixes à l'écran
|
|
|
749
754
|
|
|
750
755
|
---
|
|
751
756
|
|
|
757
|
+
# 🎯 Section 5 : Focus Trap Accessible (FocusTrap)
|
|
758
|
+
|
|
759
|
+
Gestionnaire de confinement de focus ultra-léger (< 2 ko gzippé), sans dépendance externe, assurant une conformité WAI-ARIA stricte pour modales, tiroirs (drawers), lightboxes et popups.
|
|
760
|
+
|
|
761
|
+
> 📚 **Documentation approfondie :** [Spécifications Fonctionnelles](docs/focus-trap%20functional.md) • [Architecture Technique](docs/focus-trap%20technical.md) • [Accessibilité WAI-ARIA & APG](docs/focus-trap%20accessibility.md)
|
|
762
|
+
|
|
763
|
+
## Vision & Axiome du Flux Naturel (FocusTrap)
|
|
764
|
+
> **Le confort de saisie et la réactivité native sont prioritaires : le flux entre le premier et le dernier élément appartient au navigateur.**
|
|
765
|
+
- **Zéro friction intermédiaire** : Aucune interception sur les `Tab` intermédiaires, le navigateur déroule sa tabulation native à pleine vitesse.
|
|
766
|
+
- **Bornes dynamiques à la volée** : Le premier et le dernier élément focusable ne sont pas figés en mémoire ; ils s'adaptent instantanément aux apparitions dynamiques de champs (formulaires multi-étapes, accordéons, sections conditionnelles).
|
|
767
|
+
- **Sentinelles invisibles anti-fuite** : Des sentinelles `tabindex="0"` invisibles interceptent les sauts intempestifs hors du conteneur (lecteurs d'écran, tabulation rapide).
|
|
768
|
+
- **Orchestration LIFO (Pile de Traps)** : Support automatique des modales et popups imbriquées sans conflit.
|
|
769
|
+
|
|
770
|
+
## Utilisation TypeScript / Vanilla (FocusTrap)
|
|
771
|
+
|
|
772
|
+
```typescript
|
|
773
|
+
import { FocusTrap } from 'wulf.js/focus-trap';
|
|
774
|
+
|
|
775
|
+
// Sélection du conteneur modal
|
|
776
|
+
const modalEl = document.getElementById('my-modal');
|
|
777
|
+
|
|
778
|
+
// Initialisation du trap
|
|
779
|
+
const trap = new FocusTrap(modalEl, {
|
|
780
|
+
returnFocus: true, // Restaure le focus sur le bouton d'ouverture
|
|
781
|
+
escapeDeactivates: true, // Ferme sur touche Escape
|
|
782
|
+
setInert: true // Isole le reste de la page via l'attribut HTML5 'inert'
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
// À l'ouverture de la modale
|
|
786
|
+
trap.activate();
|
|
787
|
+
|
|
788
|
+
// À la fermeture
|
|
789
|
+
trap.deactivate();
|
|
790
|
+
```
|
|
791
|
+
|
|
792
|
+
## Options de Configuration (FocusTrap)
|
|
793
|
+
|
|
794
|
+
| Option | Type | Valeur par défaut | Description |
|
|
795
|
+
| :--- | :--- | :--- | :--- |
|
|
796
|
+
| `initialFocus` | `HTMLElement \| string \| false` | `'[data-autofocus], [autofocus]'` | Élément ou sélecteur à cibler à l'activation (`false` pour désactiver). |
|
|
797
|
+
| `returnFocus` | `boolean` | `true` | Restaure le focus sur l'élément actif lors de l'activation. |
|
|
798
|
+
| `fallbackReturnFocus` | `HTMLElement \| string` | `''` | Cible de secours si le déclencheur n'est plus dans le DOM. |
|
|
799
|
+
| `escapeDeactivates` | `boolean` | `true` | Désactive le trap à l'appui sur `Escape`. |
|
|
800
|
+
| `allowOutsideClick` | `boolean` | `false` | Bloque (`false`) ou autorise (`true`) les clics hors zone. |
|
|
801
|
+
| `setInert` | `boolean` | `false` | Applique automatiquement `inert` sur les frères du conteneur. |
|
|
802
|
+
| `onActivate` | `() => void` | `() => {}` | Callback déclenché à l'activation. |
|
|
803
|
+
| `onDeactivate` | `() => void` | `() => {}` | Callback déclenché à la désactivation. |
|
|
804
|
+
|
|
805
|
+
## Gestion de Pile LIFO & Sentinelles
|
|
806
|
+
Lorsqu'une modale fille s'ouvre depuis une modale mère ou un drawer, le trap parent est automatiquement suspendu (`pause()`). À la fermeture de la sous-modale, le trap parent reprend automatiquement la main sans conflit.
|
|
807
|
+
|
|
808
|
+
---
|
|
809
|
+
|
|
752
810
|
# 📦 Installation & Imports Modulaires
|
|
753
811
|
|
|
754
812
|
Installez le package via npm :
|
|
@@ -760,7 +818,7 @@ npm install wulf.js
|
|
|
760
818
|
### Utilisation Bundlers (Vite, Webpack, Rollup, Next.js)
|
|
761
819
|
```typescript
|
|
762
820
|
// 0. Importer l'ensemble de la suite
|
|
763
|
-
import { Mask, Scroll, Masonry, Toast } from 'wulf.js';
|
|
821
|
+
import { Mask, Scroll, Masonry, Toast, FocusTrap } from 'wulf.js';
|
|
764
822
|
|
|
765
823
|
// 1. Importer la librairie de Masque
|
|
766
824
|
import { InputMask, InputAmountMask, InputEmailMask } from 'wulf.js/mask';
|
|
@@ -777,6 +835,9 @@ import 'wulf.js/masonry/css';
|
|
|
777
835
|
// 4. Importer la librairie Toast
|
|
778
836
|
import { Toast } from 'wulf.js/toast';
|
|
779
837
|
import 'wulf.js/toast/css';
|
|
838
|
+
|
|
839
|
+
// 5. Importer la librairie FocusTrap
|
|
840
|
+
import { FocusTrap } from 'wulf.js/focus-trap';
|
|
780
841
|
```
|
|
781
842
|
|
|
782
843
|
### Utilisation Node.js / CommonJS (SSR)
|
|
@@ -785,6 +846,7 @@ const { InputMask } = require('wulf.js/mask');
|
|
|
785
846
|
const { ScrollBlock } = require('wulf.js/scroll');
|
|
786
847
|
const { Masonry } = require('wulf.js/masonry');
|
|
787
848
|
const { Toast } = require('wulf.js/toast');
|
|
849
|
+
const { FocusTrap } = require('wulf.js/focus-trap');
|
|
788
850
|
```
|
|
789
851
|
|
|
790
852
|
### Utilisation Navigateur Directe (Script Tags & IIFE)
|
|
@@ -805,6 +867,10 @@ const { Toast } = require('wulf.js/toast');
|
|
|
805
867
|
<link rel="stylesheet" href="dist/toast.css">
|
|
806
868
|
<script src="dist/toast.global.js"></script>
|
|
807
869
|
<!-- Expose window.Wulf.Toast -->
|
|
870
|
+
|
|
871
|
+
<!-- Focus Trap Accessible -->
|
|
872
|
+
<script src="dist/focus-trap.global.js"></script>
|
|
873
|
+
<!-- Expose window.Wulf.FocusTrap -->
|
|
808
874
|
```
|
|
809
875
|
|
|
810
876
|
---
|
|
@@ -814,7 +880,7 @@ const { Toast } = require('wulf.js/toast');
|
|
|
814
880
|
Le projet dispose d'une suite exhaustive de tests automatisés couvrant les cas nominaux, l'accessibilité WAI-ARIA, les comportements multi-navigateurs et la non-régression :
|
|
815
881
|
|
|
816
882
|
```bash
|
|
817
|
-
# Lancer tous les tests (Mask + Scroll + Masonry + Toast) dans le navigateur
|
|
883
|
+
# Lancer tous les tests (Mask + Scroll + Masonry + Toast + FocusTrap) dans le navigateur
|
|
818
884
|
npm test
|
|
819
885
|
|
|
820
886
|
# Lancer spécifiquement les tests d'une librairie
|
|
@@ -822,6 +888,7 @@ npm run test:mask
|
|
|
822
888
|
npm run test:scroll
|
|
823
889
|
npm run test:masonry
|
|
824
890
|
npm run test:toast
|
|
891
|
+
npm run test:focus-trap
|
|
825
892
|
|
|
826
893
|
# Démonstrations interactives
|
|
827
894
|
npm run demo:mask
|
|
@@ -853,6 +920,7 @@ npm run build
|
|
|
853
920
|
- **Barre de défilement** : `dist/scroll.esm.js`, `dist/scroll.cjs.js`, `dist/scroll.global.js`, `dist/scroll.global.min.js`, `dist/scroll.css`, `dist/types/scroll/`.
|
|
854
921
|
- **Grille Masonry** : `dist/masonry.esm.js`, `dist/masonry.cjs.js`, `dist/masonry.global.js`, `dist/masonry.global.min.js`, `dist/masonry.css`, `dist/types/masonry/`.
|
|
855
922
|
- **Notifications Toast** : `dist/toast.esm.js`, `dist/toast.cjs.js`, `dist/toast.global.js`, `dist/toast.global.min.js`, `dist/toast.css`, `dist/types/toast/`.
|
|
923
|
+
- **Focus Trap** : `dist/focus-trap.esm.js`, `dist/focus-trap.cjs.js`, `dist/focus-trap.global.js`, `dist/focus-trap.global.min.js`, `dist/types/focus-trap/`.
|
|
856
924
|
|
|
857
925
|
---
|
|
858
926
|
|
package/changelog.md
CHANGED
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
## [Non publié]
|
|
4
4
|
|
|
5
|
+
## [1.9.1] - 2026-10-30
|
|
6
|
+
- **Audit Automatique d'Accessibilité WAI-ARIA (`audit()`) sur l'ensemble des composants** :
|
|
7
|
+
- Création de la classe abstraite commune `AuditableComponent` (ainsi que des contrats `IAuditable` et `AuditableOptions`) dans `src/common/AuditableComponent.ts`, factorisant l'exécution de l'audit, le formatage du `console.warn` (`runAuditWarning()`) et la validation des références d'IDs ARIA (`checkAriaIdReferences()`).
|
|
8
|
+
- Ajout de la méthode publique pure `audit(): string[]` et de l'option de configuration `audit?: boolean` (défaut: `true`) sur `FocusTrap`, `BaseInputMask` (`InputMask`, `InputAmountMask`, `InputEmailMask` + attribut HTML `data-mask-audit`), `ScrollBlock`, `Masonry` et `ToastInstance`.
|
|
9
|
+
- Exécution automatique de l'audit à la fin de l'initialisation de chaque composant (ainsi qu'à l'appel de `activate()` pour `FocusTrap`), affichant un avertissement détaillé via `console.warn` si des contraintes d'accessibilité à la charge de l'intégrateur manquent dans le DOM.
|
|
10
|
+
- Contrôles effectués par composant :
|
|
11
|
+
- `Wulf.FocusTrap` : présence d'un `role` approprié (`dialog`, `alertdialog`, `menu`, `listbox` ou `<dialog>`), présence de `aria-modal="true"`, et présence d'un nom accessible (`aria-labelledby` valide ou `aria-label`).
|
|
12
|
+
- `Wulf.Mask` : présence d'un étiquetage accessible (`<label for>`, `<label>` parent, `aria-label` ou `aria-labelledby` valide), validité des IDs `aria-describedby`, et indication du format attendu sur `InputMask`.
|
|
13
|
+
- `Wulf.Scroll` : présence d'un libellé contextuel explicite (`ariaLabel`, `aria-label` ou `aria-labelledby`) et taille minimale de cible (`minThumbSize >= 24px`).
|
|
14
|
+
- `Wulf.Masonry` : présence d'un libellé contextuel explicite (`ariaLabel`, `aria-label` ou `aria-labelledby`) et présence de l'attribut `alt` sur les images `<img>` de la grille.
|
|
15
|
+
- `Wulf.Toast` : présence d'un contenu textuel (`title` ou `message`), durée d'affichage suffisante (`duration >= 5000ms` ou `duration: false`) et possibilité de fermeture sur les notifications persistantes.
|
|
16
|
+
- Documentation mise à jour dans `docs/* accessibility.md` et ajout des suites de tests dédiées dans `test-focus-trap.html`, `test-mask.html`, `test-scroll.html`, `test-masonry.html` et `test-toast.html`.
|
|
17
|
+
- **Nouveau module `Wulf.FocusTrap` (`src/focus-trap/`)** :
|
|
18
|
+
- Création de la micro-librairie autonome de Focus Trap zéro dépendance, conforme WAI-ARIA, pour modales, drawers, popups et lightboxes.
|
|
19
|
+
- Détection dynamique et rigoureuse des éléments focusables avec exclusion des éléments désactivés (`disabled`, `<fieldset disabled>`), cachés (`display: none`, `visibility: hidden`, `hidden`, `inert`, dimensions réelles nulles), et sélection chirurgicale du bouton coché dans les groupes de boutons radio (`<input type="radio">`).
|
|
20
|
+
- Bouclage clavier naturel : respect absolu du flux de lecture DOM natif entre le premier et le dernier focusable sans interférence sur les touches intermédiaires ; interception de `Tab` sur le dernier élément pour reboucler sur le premier, et de `Shift+Tab` sur le premier pour reboucler sur le dernier.
|
|
21
|
+
- Sentinelles invisibles anti-fuite (`wulf-sentinel-start` / `wulf-sentinel-end`) pour intercepter les pertes de focus lors de l'usage de lecteurs d'écran ou de focus externes.
|
|
22
|
+
- Gestion de pile globale LIFO (`trapStack`) : mise en pause automatique d'un trap parent lors de l'ouverture d'un trap fils (modales imbriquées) et réveil automatique à la fermeture.
|
|
23
|
+
- Support de la touche `Escape`, inertisation automatique des éléments frères (`setInert: true`), et mémorisation/restauration infaillible du focus sur l'élément déclencheur d'origine.
|
|
24
|
+
- Intégration dans le build (`build.mjs`, `package.json`, `index.ts`), suite de tests dédiée (`test-focus-trap.html`) et raccordement à l'orchestrateur global `test.html`.
|
|
25
|
+
|
|
5
26
|
## [1.9.0] - 2026-10-28
|
|
6
27
|
- **Assistance Prédictive d'Extensions de Domaine (TLD) sur `InputEmailMask` (`src/mask/`)** :
|
|
7
28
|
- `EmailEngine.ts` & `InputEmailMask.ts` : Ajout des options `defaultTld` (défaut: `'__'`, ou TLD réel comme `'fr'`, `'com'`, ou `null` pour désactiver) et `tlds` (liste d'extensions ordonnée maîtrisée par l'utilisateur) ainsi que des attributs HTML `data-mask-default-tld` et `data-mask-tlds`.
|
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/focus-trap/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
FocusTrap: () => FocusTrap,
|
|
24
|
+
default: () => FocusTrap
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/common/AuditableComponent.ts
|
|
29
|
+
var AuditableComponent = class {
|
|
30
|
+
/**
|
|
31
|
+
* Indication affichée en pied de warning pour expliquer comment désactiver l'audit.
|
|
32
|
+
*/
|
|
33
|
+
getAuditDisableHint() {
|
|
34
|
+
return "Passez { audit: false } dans les options pour d\xE9sactiver cet avertissement";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Exécute l'audit d'accessibilité et émet un avertissement standardisé dans la console
|
|
38
|
+
* si l'audit est activé et que des contraintes manquent.
|
|
39
|
+
*/
|
|
40
|
+
runAuditWarning(enabled = true) {
|
|
41
|
+
if (!enabled) return [];
|
|
42
|
+
const warnings = this.audit();
|
|
43
|
+
if (warnings.length > 0 && typeof console !== "undefined" && typeof console.warn === "function") {
|
|
44
|
+
console.warn(
|
|
45
|
+
`[${this.getAuditPrefix()}] Audit d'accessibilit\xE9 \u2014 ${warnings.length} contrainte(s) manquante(s) sur ${this.getAuditTargetLabel()} :
|
|
46
|
+
` + warnings.map((w) => ` - ${w}`).join("\n") + `
|
|
47
|
+
(${this.getAuditDisableHint()})`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return warnings;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Vérifie si un attribut de référence d'IDs ARIA (ex: aria-labelledby, aria-describedby)
|
|
54
|
+
* pointe vers des éléments réellement présents dans le document.
|
|
55
|
+
*/
|
|
56
|
+
checkAriaIdReferences(element, attrName, warnings) {
|
|
57
|
+
const rawValue = element.getAttribute(attrName)?.trim();
|
|
58
|
+
if (!rawValue) return false;
|
|
59
|
+
const doc = element.ownerDocument || document;
|
|
60
|
+
const ids = rawValue.split(/\s+/).filter(Boolean);
|
|
61
|
+
const isValid = ids.length > 0 && ids.every((id) => Boolean(doc.getElementById(id)));
|
|
62
|
+
if (!isValid) {
|
|
63
|
+
warnings.push(
|
|
64
|
+
`Attribut ${attrName}="${rawValue}" invalide : l'\xE9l\xE9ment cible est introuvable dans le DOM.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return isValid;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/focus-trap/FocusTrap.ts
|
|
72
|
+
var FOCUSABLE_SELECTOR = [
|
|
73
|
+
"a[href]",
|
|
74
|
+
"area[href]",
|
|
75
|
+
"input:not([disabled])",
|
|
76
|
+
"select:not([disabled])",
|
|
77
|
+
"textarea:not([disabled])",
|
|
78
|
+
"button:not([disabled])",
|
|
79
|
+
"iframe",
|
|
80
|
+
"object",
|
|
81
|
+
"embed",
|
|
82
|
+
'[contenteditable="true"]',
|
|
83
|
+
'[tabindex]:not([tabindex="-1"])'
|
|
84
|
+
].join(",");
|
|
85
|
+
var trapStack = [];
|
|
86
|
+
var FocusTrap = class extends AuditableComponent {
|
|
87
|
+
constructor(target, options = {}) {
|
|
88
|
+
super();
|
|
89
|
+
this.active = false;
|
|
90
|
+
this.paused = false;
|
|
91
|
+
this.previousActiveElement = null;
|
|
92
|
+
this.inertSiblings = [];
|
|
93
|
+
// Sentinelles anti-fuite de focus
|
|
94
|
+
this.startSentinel = null;
|
|
95
|
+
this.endSentinel = null;
|
|
96
|
+
const el = typeof target === "string" ? document.querySelector(target) : target;
|
|
97
|
+
if (!el || !(el instanceof HTMLElement)) {
|
|
98
|
+
throw new Error(`[Wulf.FocusTrap] Conteneur introuvable ou invalide : "${target}"`);
|
|
99
|
+
}
|
|
100
|
+
this.element = el;
|
|
101
|
+
this.options = {
|
|
102
|
+
initialFocus: options.initialFocus !== void 0 ? options.initialFocus : "[data-autofocus], [autofocus]",
|
|
103
|
+
returnFocus: options.returnFocus ?? true,
|
|
104
|
+
fallbackReturnFocus: options.fallbackReturnFocus ?? "",
|
|
105
|
+
escapeDeactivates: options.escapeDeactivates ?? true,
|
|
106
|
+
allowOutsideClick: options.allowOutsideClick ?? false,
|
|
107
|
+
setInert: options.setInert ?? false,
|
|
108
|
+
onActivate: options.onActivate ?? (() => {
|
|
109
|
+
}),
|
|
110
|
+
onDeactivate: options.onDeactivate ?? (() => {
|
|
111
|
+
}),
|
|
112
|
+
audit: options.audit ?? true
|
|
113
|
+
};
|
|
114
|
+
this.handleKeyDownBound = this.handleKeyDown.bind(this);
|
|
115
|
+
this.handleClickOutsideBound = this.handleClickOutside.bind(this);
|
|
116
|
+
this.handleFocusInBound = this.handleFocusIn.bind(this);
|
|
117
|
+
this.runAuditWarning(this.options.audit);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Vérifie si un élément est réellement focusable, visible et actif dans le DOM.
|
|
121
|
+
*/
|
|
122
|
+
isFocusable(el) {
|
|
123
|
+
if (!el || !el.isConnected) return false;
|
|
124
|
+
if (el.disabled) return false;
|
|
125
|
+
if (el.closest("fieldset[disabled]") && !el.closest("legend")) return false;
|
|
126
|
+
if (el.hasAttribute("inert") || el.closest("[inert]")) return false;
|
|
127
|
+
if (el.hidden || el.closest("[hidden]")) return false;
|
|
128
|
+
if (el.style.display === "none" || el.style.visibility === "hidden") return false;
|
|
129
|
+
const style = window.getComputedStyle(el);
|
|
130
|
+
if (style.display === "none" || style.visibility === "hidden") return false;
|
|
131
|
+
if (el.offsetWidth === 0 && el.offsetHeight === 0 && style.position !== "fixed") {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
if (el instanceof HTMLInputElement && el.type === "radio" && el.name) {
|
|
135
|
+
const group = Array.from(
|
|
136
|
+
this.element.querySelectorAll(`input[type="radio"][name="${CSS.escape(el.name)}"]`)
|
|
137
|
+
);
|
|
138
|
+
const checked = group.find((r) => r.checked);
|
|
139
|
+
if (checked) {
|
|
140
|
+
return el === checked;
|
|
141
|
+
}
|
|
142
|
+
return el === group[0];
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Retourne tous les éléments focusables réels ordonnés dans le conteneur.
|
|
148
|
+
*/
|
|
149
|
+
getFocusableElements() {
|
|
150
|
+
const rawElements = Array.from(this.element.querySelectorAll(FOCUSABLE_SELECTOR));
|
|
151
|
+
return rawElements.filter((el) => this.isFocusable(el));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Active le trap.
|
|
155
|
+
*/
|
|
156
|
+
activate() {
|
|
157
|
+
if (this.active) return this;
|
|
158
|
+
const currentTop = trapStack[trapStack.length - 1];
|
|
159
|
+
if (currentTop) {
|
|
160
|
+
currentTop.pause();
|
|
161
|
+
}
|
|
162
|
+
this.active = true;
|
|
163
|
+
this.paused = false;
|
|
164
|
+
trapStack.push(this);
|
|
165
|
+
this.previousActiveElement = document.activeElement;
|
|
166
|
+
this.setupSentinels();
|
|
167
|
+
if (this.options.setInert) {
|
|
168
|
+
this.applyInert();
|
|
169
|
+
}
|
|
170
|
+
document.addEventListener("keydown", this.handleKeyDownBound, true);
|
|
171
|
+
document.addEventListener("focusin", this.handleFocusInBound, true);
|
|
172
|
+
if (!this.options.allowOutsideClick) {
|
|
173
|
+
document.addEventListener("mousedown", this.handleClickOutsideBound, true);
|
|
174
|
+
document.addEventListener("touchstart", this.handleClickOutsideBound, true);
|
|
175
|
+
}
|
|
176
|
+
this.applyInitialFocus();
|
|
177
|
+
this.runAuditWarning(this.options.audit);
|
|
178
|
+
this.options.onActivate();
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Désactive le trap et libère les écouteurs.
|
|
183
|
+
*/
|
|
184
|
+
deactivate() {
|
|
185
|
+
if (!this.active) return this;
|
|
186
|
+
this.active = false;
|
|
187
|
+
this.paused = false;
|
|
188
|
+
const index = trapStack.indexOf(this);
|
|
189
|
+
if (index !== -1) {
|
|
190
|
+
trapStack.splice(index, 1);
|
|
191
|
+
}
|
|
192
|
+
const nextTop = trapStack[trapStack.length - 1];
|
|
193
|
+
if (nextTop) {
|
|
194
|
+
nextTop.unpause();
|
|
195
|
+
}
|
|
196
|
+
document.removeEventListener("keydown", this.handleKeyDownBound, true);
|
|
197
|
+
document.removeEventListener("focusin", this.handleFocusInBound, true);
|
|
198
|
+
document.removeEventListener("mousedown", this.handleClickOutsideBound, true);
|
|
199
|
+
document.removeEventListener("touchstart", this.handleClickOutsideBound, true);
|
|
200
|
+
this.removeSentinels();
|
|
201
|
+
this.removeInert();
|
|
202
|
+
if (this.options.returnFocus) {
|
|
203
|
+
this.restoreFocus();
|
|
204
|
+
}
|
|
205
|
+
this.options.onDeactivate();
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Met en pause le trap (utilisé quand un trap fils s'ouvre au-dessus).
|
|
210
|
+
*/
|
|
211
|
+
pause() {
|
|
212
|
+
this.paused = true;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Sort de pause.
|
|
216
|
+
*/
|
|
217
|
+
unpause() {
|
|
218
|
+
this.paused = false;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Indique si le trap est actuellement actif et non en pause.
|
|
222
|
+
*/
|
|
223
|
+
isActive() {
|
|
224
|
+
return this.active && !this.paused;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Vérifie la conformité d'accessibilité WAI-ARIA du conteneur FocusTrap
|
|
228
|
+
* et retourne la liste des contraintes manquantes.
|
|
229
|
+
*/
|
|
230
|
+
audit() {
|
|
231
|
+
const warnings = [];
|
|
232
|
+
const role = this.element.getAttribute("role");
|
|
233
|
+
const isNativeDialog = this.element.tagName.toLowerCase() === "dialog";
|
|
234
|
+
const validRoles = ["dialog", "alertdialog", "menu", "listbox", "tree", "grid"];
|
|
235
|
+
if (!isNativeDialog && (!role || !validRoles.includes(role))) {
|
|
236
|
+
warnings.push(
|
|
237
|
+
'R\xF4le s\xE9mantique manquant ou inadapt\xE9 : d\xE9finissez role="dialog" (ou "alertdialog", "menu", "listbox") sur le conteneur.'
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
if ((isNativeDialog || role === "dialog" || role === "alertdialog") && this.element.getAttribute("aria-modal") !== "true") {
|
|
241
|
+
warnings.push(
|
|
242
|
+
'Isolation modale manquante : d\xE9finissez aria-modal="true" sur le conteneur de dialogue (WCAG 4.1.2).'
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
const ariaLabel = this.element.getAttribute("aria-label")?.trim();
|
|
246
|
+
const hasValidLabelledby = this.checkAriaIdReferences(this.element, "aria-labelledby", warnings);
|
|
247
|
+
if (!ariaLabel && !hasValidLabelledby) {
|
|
248
|
+
warnings.push(
|
|
249
|
+
'Nom accessible manquant : d\xE9finissez aria-labelledby="id-titre" ou aria-label="..." sur le conteneur (WCAG 4.1.2).'
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
return warnings;
|
|
253
|
+
}
|
|
254
|
+
getAuditPrefix() {
|
|
255
|
+
return "Wulf.FocusTrap";
|
|
256
|
+
}
|
|
257
|
+
getAuditTargetLabel() {
|
|
258
|
+
return this.element.id ? `#${this.element.id}` : `<${this.element.tagName.toLowerCase()}>`;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Détruit l'instance proprement.
|
|
262
|
+
*/
|
|
263
|
+
destroy() {
|
|
264
|
+
this.deactivate();
|
|
265
|
+
}
|
|
266
|
+
// --- Implémentation interne ---
|
|
267
|
+
handleKeyDown(e) {
|
|
268
|
+
if (!this.isActive()) return;
|
|
269
|
+
if (e.key === "Escape" && this.options.escapeDeactivates) {
|
|
270
|
+
e.preventDefault();
|
|
271
|
+
e.stopPropagation();
|
|
272
|
+
this.deactivate();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (e.key !== "Tab") return;
|
|
276
|
+
const focusables = this.getFocusableElements();
|
|
277
|
+
if (focusables.length === 0) {
|
|
278
|
+
e.preventDefault();
|
|
279
|
+
if (this.element.getAttribute("tabindex") === null) {
|
|
280
|
+
this.element.setAttribute("tabindex", "-1");
|
|
281
|
+
}
|
|
282
|
+
this.element.focus();
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const first = focusables[0];
|
|
286
|
+
const last = focusables[focusables.length - 1];
|
|
287
|
+
const current = document.activeElement;
|
|
288
|
+
if (!e.shiftKey && (current === last || !this.element.contains(current))) {
|
|
289
|
+
e.preventDefault();
|
|
290
|
+
first.focus();
|
|
291
|
+
} else if (e.shiftKey && (current === first || !this.element.contains(current))) {
|
|
292
|
+
e.preventDefault();
|
|
293
|
+
last.focus();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
handleFocusIn(e) {
|
|
297
|
+
if (!this.isActive()) return;
|
|
298
|
+
const target = e.target;
|
|
299
|
+
if (!target) return;
|
|
300
|
+
if (target === this.startSentinel) {
|
|
301
|
+
const focusables = this.getFocusableElements();
|
|
302
|
+
if (focusables.length > 0) {
|
|
303
|
+
focusables[focusables.length - 1].focus();
|
|
304
|
+
} else {
|
|
305
|
+
this.element.focus();
|
|
306
|
+
}
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (target === this.endSentinel) {
|
|
310
|
+
const focusables = this.getFocusableElements();
|
|
311
|
+
if (focusables.length > 0) {
|
|
312
|
+
focusables[0].focus();
|
|
313
|
+
} else {
|
|
314
|
+
this.element.focus();
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (!this.element.contains(target)) {
|
|
319
|
+
const focusables = this.getFocusableElements();
|
|
320
|
+
if (focusables.length > 0) {
|
|
321
|
+
focusables[0].focus();
|
|
322
|
+
} else {
|
|
323
|
+
if (this.element.getAttribute("tabindex") === null) {
|
|
324
|
+
this.element.setAttribute("tabindex", "-1");
|
|
325
|
+
}
|
|
326
|
+
this.element.focus();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
handleClickOutside(e) {
|
|
331
|
+
if (!this.isActive()) return;
|
|
332
|
+
const target = e.target;
|
|
333
|
+
if (target && !this.element.contains(target)) {
|
|
334
|
+
e.preventDefault();
|
|
335
|
+
e.stopPropagation();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
setupSentinels() {
|
|
339
|
+
if (!this.startSentinel) {
|
|
340
|
+
this.startSentinel = document.createElement("span");
|
|
341
|
+
this.startSentinel.setAttribute("tabindex", "0");
|
|
342
|
+
this.startSentinel.setAttribute("aria-hidden", "true");
|
|
343
|
+
this.startSentinel.className = "wulf-sentinel-start";
|
|
344
|
+
this.startSentinel.style.cssText = "position:fixed;top:0;left:0;opacity:0;pointer-events:none;width:0;height:0;";
|
|
345
|
+
}
|
|
346
|
+
if (!this.endSentinel) {
|
|
347
|
+
this.endSentinel = document.createElement("span");
|
|
348
|
+
this.endSentinel.setAttribute("tabindex", "0");
|
|
349
|
+
this.endSentinel.setAttribute("aria-hidden", "true");
|
|
350
|
+
this.endSentinel.className = "wulf-sentinel-end";
|
|
351
|
+
this.endSentinel.style.cssText = "position:fixed;top:0;left:0;opacity:0;pointer-events:none;width:0;height:0;";
|
|
352
|
+
}
|
|
353
|
+
if (this.element.parentNode) {
|
|
354
|
+
this.element.parentNode.insertBefore(this.startSentinel, this.element);
|
|
355
|
+
this.element.parentNode.insertBefore(this.endSentinel, this.element.nextSibling);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
removeSentinels() {
|
|
359
|
+
this.startSentinel?.remove();
|
|
360
|
+
this.endSentinel?.remove();
|
|
361
|
+
this.startSentinel = null;
|
|
362
|
+
this.endSentinel = null;
|
|
363
|
+
}
|
|
364
|
+
applyInert() {
|
|
365
|
+
this.inertSiblings = [];
|
|
366
|
+
let parent = this.element.parentElement;
|
|
367
|
+
let currentChild = this.element;
|
|
368
|
+
while (parent && parent !== document.body) {
|
|
369
|
+
currentChild = parent;
|
|
370
|
+
parent = parent.parentElement;
|
|
371
|
+
}
|
|
372
|
+
if (parent === document.body) {
|
|
373
|
+
Array.from(document.body.children).forEach((child) => {
|
|
374
|
+
if (child instanceof HTMLElement && child !== currentChild && child !== this.startSentinel && child !== this.endSentinel && !child.hasAttribute("inert")) {
|
|
375
|
+
child.setAttribute("inert", "");
|
|
376
|
+
this.inertSiblings.push(child);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
removeInert() {
|
|
382
|
+
this.inertSiblings.forEach((el) => el.removeAttribute("inert"));
|
|
383
|
+
this.inertSiblings = [];
|
|
384
|
+
}
|
|
385
|
+
applyInitialFocus() {
|
|
386
|
+
if (this.options.initialFocus === false) return;
|
|
387
|
+
let targetEl = null;
|
|
388
|
+
if (typeof this.options.initialFocus === "string") {
|
|
389
|
+
targetEl = this.element.querySelector(this.options.initialFocus);
|
|
390
|
+
} else if (this.options.initialFocus instanceof HTMLElement) {
|
|
391
|
+
targetEl = this.options.initialFocus;
|
|
392
|
+
}
|
|
393
|
+
if (targetEl && this.isFocusable(targetEl)) {
|
|
394
|
+
targetEl.focus();
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
const focusables = this.getFocusableElements();
|
|
398
|
+
if (focusables.length > 0) {
|
|
399
|
+
focusables[0].focus();
|
|
400
|
+
} else {
|
|
401
|
+
if (this.element.getAttribute("tabindex") === null) {
|
|
402
|
+
this.element.setAttribute("tabindex", "-1");
|
|
403
|
+
}
|
|
404
|
+
this.element.focus();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
restoreFocus() {
|
|
408
|
+
if (this.previousActiveElement && this.previousActiveElement.isConnected) {
|
|
409
|
+
this.previousActiveElement.focus();
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (this.options.fallbackReturnFocus) {
|
|
413
|
+
const fallback = typeof this.options.fallbackReturnFocus === "string" ? document.querySelector(this.options.fallbackReturnFocus) : this.options.fallbackReturnFocus;
|
|
414
|
+
if (fallback && fallback.isConnected) {
|
|
415
|
+
fallback.focus();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/focus-trap/index.ts
|
|
422
|
+
if (typeof window !== "undefined") {
|
|
423
|
+
const win = window;
|
|
424
|
+
const wulfNamespace = win.Wulf || {};
|
|
425
|
+
win.Wulf = wulfNamespace;
|
|
426
|
+
wulfNamespace.FocusTrap = Object.assign(FocusTrap, {
|
|
427
|
+
FocusTrap
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
//# sourceMappingURL=focus-trap.cjs.js.map
|