@riebeckite/plugin-lightbox 0.0.13 → 0.0.14

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/dist/client.js CHANGED
@@ -5,6 +5,7 @@ function createDialog() {
5
5
  element.setAttribute("role", "dialog");
6
6
  element.setAttribute("aria-modal", "true");
7
7
  element.setAttribute("aria-label", "画像の拡大表示");
8
+ element.tabIndex = -1;
8
9
  element.setAttribute("hidden", "");
9
10
  const frame = document.createElement("figure");
10
11
  frame.className = "rr-lightbox__frame";
@@ -73,7 +74,12 @@ function initLightbox(root = document, options = {}) {
73
74
  }
74
75
  };
75
76
  const handleKeyDown = (event) => {
76
- if (dialog.element.hidden || event.key !== "Escape") return;
77
+ if (dialog.element.hidden) return;
78
+ if (event.key === "Tab") {
79
+ trapFocus(event, dialog.element);
80
+ return;
81
+ }
82
+ if (event.key !== "Escape") return;
77
83
  event.preventDefault();
78
84
  close();
79
85
  };
@@ -93,6 +99,33 @@ function initLightbox(root = document, options = {}) {
93
99
  for (const trigger of wrappedTriggers) unwrapImage(trigger);
94
100
  };
95
101
  }
102
+ function trapFocus(event, container) {
103
+ const focusableElements = getFocusableElements(container);
104
+ if (focusableElements.length === 0) {
105
+ event.preventDefault();
106
+ container.focus();
107
+ return;
108
+ }
109
+ const first = focusableElements[0];
110
+ const last = focusableElements[focusableElements.length - 1];
111
+ const active = document.activeElement;
112
+ if (event.shiftKey && active === first) {
113
+ event.preventDefault();
114
+ last.focus();
115
+ return;
116
+ }
117
+ if (!event.shiftKey && active === last) {
118
+ event.preventDefault();
119
+ first.focus();
120
+ }
121
+ }
122
+ function getFocusableElements(container) {
123
+ return Array.from(
124
+ container.querySelectorAll(
125
+ 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])'
126
+ )
127
+ ).filter((element) => !element.hasAttribute("disabled"));
128
+ }
96
129
  function wrapImages(root, triggerClass) {
97
130
  const images = Array.from(
98
131
  root.querySelectorAll("img[src]")
package/dist/index.js CHANGED
@@ -86,6 +86,7 @@ function createDialog() {
86
86
  element.setAttribute("role", "dialog");
87
87
  element.setAttribute("aria-modal", "true");
88
88
  element.setAttribute("aria-label", "画像の拡大表示");
89
+ element.tabIndex = -1;
89
90
  element.setAttribute("hidden", "");
90
91
  const frame = document.createElement("figure");
91
92
  frame.className = "rr-lightbox__frame";
@@ -151,7 +152,12 @@ function initLightbox(root = document, options = {}) {
151
152
  }
152
153
  };
153
154
  const handleKeyDown = (event) => {
154
- if (dialog.element.hidden || event.key !== "Escape") return;
155
+ if (dialog.element.hidden) return;
156
+ if (event.key === "Tab") {
157
+ trapFocus(event, dialog.element);
158
+ return;
159
+ }
160
+ if (event.key !== "Escape") return;
155
161
  event.preventDefault();
156
162
  close();
157
163
  };
@@ -171,6 +177,33 @@ function initLightbox(root = document, options = {}) {
171
177
  for (const trigger of wrappedTriggers) unwrapImage(trigger);
172
178
  };
173
179
  }
180
+ function trapFocus(event, container) {
181
+ const focusableElements = getFocusableElements(container);
182
+ if (focusableElements.length === 0) {
183
+ event.preventDefault();
184
+ container.focus();
185
+ return;
186
+ }
187
+ const first = focusableElements[0];
188
+ const last = focusableElements[focusableElements.length - 1];
189
+ const active = document.activeElement;
190
+ if (event.shiftKey && active === first) {
191
+ event.preventDefault();
192
+ last.focus();
193
+ return;
194
+ }
195
+ if (!event.shiftKey && active === last) {
196
+ event.preventDefault();
197
+ first.focus();
198
+ }
199
+ }
200
+ function getFocusableElements(container) {
201
+ return Array.from(
202
+ container.querySelectorAll(
203
+ 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])'
204
+ )
205
+ ).filter((element) => !element.hasAttribute("disabled"));
206
+ }
174
207
  function wrapImages(root, triggerClass) {
175
208
  const images = Array.from(
176
209
  root.querySelectorAll("img[src]")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riebeckite/plugin-lightbox",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,7 +21,7 @@
21
21
  "./style.css": "./style.css"
22
22
  },
23
23
  "dependencies": {
24
- "@riebeckite/core": "0.0.13"
24
+ "@riebeckite/core": "0.0.14"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"
@@ -56,6 +56,7 @@
56
56
  "./style.css"
57
57
  ],
58
58
  "scripts": {
59
- "build": "node ../../../scripts/build_package.mjs"
59
+ "build": "node ../../../scripts/build_package.mjs",
60
+ "test": "node --import tsx --test \"test/*.test.ts\""
60
61
  }
61
62
  }