@react-aria/landmark 3.0.0-beta.1 → 3.0.0-beta.11

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/import.mjs CHANGED
@@ -1,19 +1,6 @@
1
- import $TvsbU$swchelperssrc_define_propertymjs from "@swc/helpers/src/_define_property.mjs";
2
- import {useState as $TvsbU$useState, useCallback as $TvsbU$useCallback, useEffect as $TvsbU$useEffect} from "react";
3
- import {useLayoutEffect as $TvsbU$useLayoutEffect} from "@react-aria/utils";
4
- import {useSyncExternalStore as $TvsbU$useSyncExternalStore} from "use-sync-external-store/shim/index.js";
1
+ import {createLandmarkController as $a86207c5d7f7e1fb$export$f50151dbd51cd1d9, useLandmark as $a86207c5d7f7e1fb$export$4cc632584fd87fae} from "./useLandmark.mjs";
5
2
 
6
3
  /*
7
- * Copyright 2022 Adobe. All rights reserved.
8
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License. You may obtain a copy
10
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software distributed under
13
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
- * OF ANY KIND, either express or implied. See the License for the specific language
15
- * governing permissions and limitations under the License.
16
- */ /*
17
4
  * Copyright 2022 Adobe. All rights reserved.
18
5
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
19
6
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -26,390 +13,5 @@ import {useSyncExternalStore as $TvsbU$useSyncExternalStore} from "use-sync-exte
26
13
  */
27
14
 
28
15
 
29
-
30
- // Increment this version number whenever the
31
- // LandmarkManagerApi or Landmark interfaces change.
32
- const $a86207c5d7f7e1fb$var$LANDMARK_API_VERSION = 1;
33
- // Symbol under which the singleton landmark manager instance is attached to the document.
34
- const $a86207c5d7f7e1fb$var$landmarkSymbol = Symbol.for("react-aria-landmark-manager");
35
- function $a86207c5d7f7e1fb$var$subscribe(fn) {
36
- document.addEventListener("react-aria-landmark-manager-change", fn);
37
- return ()=>document.removeEventListener("react-aria-landmark-manager-change", fn);
38
- }
39
- function $a86207c5d7f7e1fb$var$getLandmarkManager() {
40
- if (typeof document === "undefined") return null;
41
- // Reuse an existing instance if it has the same or greater version.
42
- let instance = document[$a86207c5d7f7e1fb$var$landmarkSymbol];
43
- if (instance && instance.version >= $a86207c5d7f7e1fb$var$LANDMARK_API_VERSION) return instance;
44
- // Otherwise, create a new instance and dispatch an event so anything using the existing
45
- // instance updates and re-registers their landmarks with the new one.
46
- document[$a86207c5d7f7e1fb$var$landmarkSymbol] = new $a86207c5d7f7e1fb$var$LandmarkManager();
47
- document.dispatchEvent(new CustomEvent("react-aria-landmark-manager-change"));
48
- return document[$a86207c5d7f7e1fb$var$landmarkSymbol];
49
- }
50
- // Subscribes a React component to the current landmark manager instance.
51
- function $a86207c5d7f7e1fb$var$useLandmarkManager() {
52
- return (0, $TvsbU$useSyncExternalStore)($a86207c5d7f7e1fb$var$subscribe, $a86207c5d7f7e1fb$var$getLandmarkManager, $a86207c5d7f7e1fb$var$getLandmarkManager);
53
- }
54
- class $a86207c5d7f7e1fb$var$LandmarkManager {
55
- setupIfNeeded() {
56
- if (this.isListening) return;
57
- document.addEventListener("keydown", this.f6Handler, {
58
- capture: true
59
- });
60
- document.addEventListener("focusin", this.focusinHandler, {
61
- capture: true
62
- });
63
- document.addEventListener("focusout", this.focusoutHandler, {
64
- capture: true
65
- });
66
- this.isListening = true;
67
- }
68
- teardownIfNeeded() {
69
- if (!this.isListening || this.landmarks.length > 0 || this.refCount > 0) return;
70
- document.removeEventListener("keydown", this.f6Handler, {
71
- capture: true
72
- });
73
- document.removeEventListener("focusin", this.focusinHandler, {
74
- capture: true
75
- });
76
- document.removeEventListener("focusout", this.focusoutHandler, {
77
- capture: true
78
- });
79
- this.isListening = false;
80
- }
81
- focusLandmark(landmark, direction) {
82
- var _this_landmarks_find, _this_landmarks_find_focus;
83
- (_this_landmarks_find = this.landmarks.find((l)=>l.ref.current === landmark)) === null || _this_landmarks_find === void 0 ? void 0 : (_this_landmarks_find_focus = _this_landmarks_find.focus) === null || _this_landmarks_find_focus === void 0 ? void 0 : _this_landmarks_find_focus.call(_this_landmarks_find, direction);
84
- }
85
- /**
86
- * Return set of landmarks with a specific role.
87
- */ getLandmarksByRole(role) {
88
- return new Set(this.landmarks.filter((l)=>l.role === role));
89
- }
90
- /**
91
- * Return first landmark with a specific role.
92
- */ getLandmarkByRole(role) {
93
- return this.landmarks.find((l)=>l.role === role);
94
- }
95
- addLandmark(newLandmark) {
96
- this.setupIfNeeded();
97
- if (this.landmarks.find((landmark)=>landmark.ref === newLandmark.ref) || !newLandmark.ref.current) return;
98
- if (this.landmarks.filter((landmark)=>landmark.role === "main").length > 1) console.error('Page can contain no more than one landmark with the role "main".');
99
- if (this.landmarks.length === 0) {
100
- this.landmarks = [
101
- newLandmark
102
- ];
103
- this.checkLabels(newLandmark.role);
104
- return;
105
- }
106
- // Binary search to insert new landmark based on position in document relative to existing landmarks.
107
- // https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
108
- let start = 0;
109
- let end = this.landmarks.length - 1;
110
- while(start <= end){
111
- let mid = Math.floor((start + end) / 2);
112
- let comparedPosition = newLandmark.ref.current.compareDocumentPosition(this.landmarks[mid].ref.current);
113
- let isNewAfterExisting = Boolean(comparedPosition & Node.DOCUMENT_POSITION_PRECEDING || comparedPosition & Node.DOCUMENT_POSITION_CONTAINS);
114
- if (isNewAfterExisting) start = mid + 1;
115
- else end = mid - 1;
116
- }
117
- this.landmarks.splice(start, 0, newLandmark);
118
- this.checkLabels(newLandmark.role);
119
- }
120
- updateLandmark(landmark) {
121
- let index = this.landmarks.findIndex((l)=>l.ref === landmark.ref);
122
- if (index >= 0) {
123
- this.landmarks[index] = {
124
- ...this.landmarks[index],
125
- ...landmark
126
- };
127
- this.checkLabels(this.landmarks[index].role);
128
- }
129
- }
130
- removeLandmark(ref) {
131
- this.landmarks = this.landmarks.filter((landmark)=>landmark.ref !== ref);
132
- this.teardownIfNeeded();
133
- }
134
- /**
135
- * Warn if there are 2+ landmarks with the same role but no label.
136
- * Labels for landmarks with the same role must also be unique.
137
- *
138
- * See https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/.
139
- */ checkLabels(role) {
140
- let landmarksWithRole = this.getLandmarksByRole(role);
141
- if (landmarksWithRole.size > 1) {
142
- let duplicatesWithoutLabel = [
143
- ...landmarksWithRole
144
- ].filter((landmark)=>!landmark.label);
145
- if (duplicatesWithoutLabel.length > 0) console.warn(`Page contains more than one landmark with the '${role}' role. If two or more landmarks on a page share the same role, all must be labeled with an aria-label or aria-labelledby attribute: `, duplicatesWithoutLabel.map((landmark)=>landmark.ref.current));
146
- else {
147
- let labels = [
148
- ...landmarksWithRole
149
- ].map((landmark)=>landmark.label);
150
- let duplicateLabels = labels.filter((item, index)=>labels.indexOf(item) !== index);
151
- duplicateLabels.forEach((label)=>{
152
- console.warn(`Page contains more than one landmark with the '${role}' role and '${label}' label. If two or more landmarks on a page share the same role, they must have unique labels: `, [
153
- ...landmarksWithRole
154
- ].filter((landmark)=>landmark.label === label).map((landmark)=>landmark.ref.current));
155
- });
156
- }
157
- }
158
- }
159
- /**
160
- * Get the landmark that is the closest parent in the DOM.
161
- * Returns undefined if no parent is a landmark.
162
- */ closestLandmark(element) {
163
- let landmarkMap = new Map(this.landmarks.map((l)=>[
164
- l.ref.current,
165
- l
166
- ]));
167
- let currentElement = element;
168
- while(currentElement && !landmarkMap.has(currentElement) && currentElement !== document.body && currentElement.parentElement)currentElement = currentElement.parentElement;
169
- return landmarkMap.get(currentElement);
170
- }
171
- /**
172
- * Gets the next landmark, in DOM focus order, or previous if backwards is specified.
173
- * If last landmark, next should be the first landmark.
174
- * If not inside a landmark, will return first landmark.
175
- * Returns undefined if there are no landmarks.
176
- */ getNextLandmark(element, { backward: backward }) {
177
- var _this_landmarks_nextLandmarkIndex_ref_current;
178
- let currentLandmark = this.closestLandmark(element);
179
- let nextLandmarkIndex = backward ? this.landmarks.length - 1 : 0;
180
- if (currentLandmark) nextLandmarkIndex = this.landmarks.indexOf(currentLandmark) + (backward ? -1 : 1);
181
- let wrapIfNeeded = ()=>{
182
- // When we reach the end of the landmark sequence, fire a custom event that can be listened for by applications.
183
- // If this event is canceled, we return immediately. This can be used to implement landmark navigation across iframes.
184
- if (nextLandmarkIndex < 0) {
185
- if (!element.dispatchEvent(new CustomEvent("react-aria-landmark-navigation", {
186
- detail: {
187
- direction: "backward"
188
- },
189
- bubbles: true,
190
- cancelable: true
191
- }))) return true;
192
- nextLandmarkIndex = this.landmarks.length - 1;
193
- } else if (nextLandmarkIndex >= this.landmarks.length) {
194
- if (!element.dispatchEvent(new CustomEvent("react-aria-landmark-navigation", {
195
- detail: {
196
- direction: "forward"
197
- },
198
- bubbles: true,
199
- cancelable: true
200
- }))) return true;
201
- nextLandmarkIndex = 0;
202
- }
203
- if (nextLandmarkIndex < 0 || nextLandmarkIndex >= this.landmarks.length) return true;
204
- return false;
205
- };
206
- if (wrapIfNeeded()) return undefined;
207
- // Skip over hidden landmarks.
208
- let i = nextLandmarkIndex;
209
- while((_this_landmarks_nextLandmarkIndex_ref_current = this.landmarks[nextLandmarkIndex].ref.current) === null || _this_landmarks_nextLandmarkIndex_ref_current === void 0 ? void 0 : _this_landmarks_nextLandmarkIndex_ref_current.closest("[aria-hidden=true]")){
210
- nextLandmarkIndex += backward ? -1 : 1;
211
- if (wrapIfNeeded()) return undefined;
212
- if (nextLandmarkIndex === i) break;
213
- }
214
- return this.landmarks[nextLandmarkIndex];
215
- }
216
- /**
217
- * Look at next landmark. If an element was previously focused inside, restore focus there.
218
- * If not, focus the landmark itself.
219
- * If no landmarks at all, or none with focusable elements, don't move focus.
220
- */ f6Handler(e) {
221
- if (e.key === "F6") {
222
- // If alt key pressed, focus main landmark, otherwise navigate forward or backward based on shift key.
223
- let handled = e.altKey ? this.focusMain() : this.navigate(e.target, e.shiftKey);
224
- if (handled) {
225
- e.preventDefault();
226
- e.stopPropagation();
227
- }
228
- }
229
- }
230
- focusMain() {
231
- let main = this.getLandmarkByRole("main");
232
- if (main && main.ref.current && document.contains(main.ref.current)) {
233
- this.focusLandmark(main.ref.current, "forward");
234
- return true;
235
- }
236
- return false;
237
- }
238
- navigate(from, backward) {
239
- let nextLandmark = this.getNextLandmark(from, {
240
- backward: backward
241
- });
242
- if (!nextLandmark) return false;
243
- // If something was previously focused in the next landmark, then return focus to it
244
- if (nextLandmark.lastFocused) {
245
- let lastFocused = nextLandmark.lastFocused;
246
- if (document.body.contains(lastFocused)) {
247
- lastFocused.focus();
248
- return true;
249
- }
250
- }
251
- // Otherwise, focus the landmark itself
252
- if (nextLandmark.ref.current && document.contains(nextLandmark.ref.current)) {
253
- this.focusLandmark(nextLandmark.ref.current, backward ? "backward" : "forward");
254
- return true;
255
- }
256
- return false;
257
- }
258
- /**
259
- * Sets lastFocused for a landmark, if focus is moved within that landmark.
260
- * Lets the last focused landmark know it was blurred if something else is focused.
261
- */ focusinHandler(e) {
262
- let currentLandmark = this.closestLandmark(e.target);
263
- if (currentLandmark && currentLandmark.ref.current !== e.target) this.updateLandmark({
264
- ref: currentLandmark.ref,
265
- lastFocused: e.target
266
- });
267
- let previousFocusedElement = e.relatedTarget;
268
- if (previousFocusedElement) {
269
- let closestPreviousLandmark = this.closestLandmark(previousFocusedElement);
270
- if (closestPreviousLandmark && closestPreviousLandmark.ref.current === previousFocusedElement) closestPreviousLandmark.blur();
271
- }
272
- }
273
- /**
274
- * Track if the focus is lost to the body. If it is, do cleanup on the landmark that last had focus.
275
- */ focusoutHandler(e) {
276
- let previousFocusedElement = e.target;
277
- let nextFocusedElement = e.relatedTarget;
278
- // the === document seems to be a jest thing for focus to go there on generic blur event such as landmark.blur();
279
- // browsers appear to send focus instead to document.body and the relatedTarget is null when that happens
280
- if (!nextFocusedElement || nextFocusedElement === document) {
281
- let closestPreviousLandmark = this.closestLandmark(previousFocusedElement);
282
- if (closestPreviousLandmark && closestPreviousLandmark.ref.current === previousFocusedElement) closestPreviousLandmark.blur();
283
- }
284
- }
285
- createLandmarkController() {
286
- let instance = this;
287
- instance.refCount++;
288
- instance.setupIfNeeded();
289
- return {
290
- navigate (direction, opts) {
291
- let element = (opts === null || opts === void 0 ? void 0 : opts.from) || document.activeElement;
292
- return instance.navigate(element, direction === "backward");
293
- },
294
- focusNext (opts) {
295
- let element = (opts === null || opts === void 0 ? void 0 : opts.from) || document.activeElement;
296
- return instance.navigate(element, false);
297
- },
298
- focusPrevious (opts) {
299
- let element = (opts === null || opts === void 0 ? void 0 : opts.from) || document.activeElement;
300
- return instance.navigate(element, true);
301
- },
302
- focusMain () {
303
- return instance.focusMain();
304
- },
305
- dispose () {
306
- if (instance) {
307
- instance.refCount--;
308
- instance.teardownIfNeeded();
309
- instance = null;
310
- }
311
- }
312
- };
313
- }
314
- registerLandmark(landmark) {
315
- if (this.landmarks.find((l)=>l.ref === landmark.ref)) this.updateLandmark(landmark);
316
- else this.addLandmark(landmark);
317
- return ()=>this.removeLandmark(landmark.ref);
318
- }
319
- constructor(){
320
- (0, $TvsbU$swchelperssrc_define_propertymjs)(this, "landmarks", []);
321
- (0, $TvsbU$swchelperssrc_define_propertymjs)(this, "isListening", false);
322
- (0, $TvsbU$swchelperssrc_define_propertymjs)(this, "refCount", 0);
323
- (0, $TvsbU$swchelperssrc_define_propertymjs)(this, "version", $a86207c5d7f7e1fb$var$LANDMARK_API_VERSION);
324
- this.f6Handler = this.f6Handler.bind(this);
325
- this.focusinHandler = this.focusinHandler.bind(this);
326
- this.focusoutHandler = this.focusoutHandler.bind(this);
327
- }
328
- }
329
- function $a86207c5d7f7e1fb$export$f50151dbd51cd1d9() {
330
- // Get the current landmark manager and create a controller using it.
331
- let instance = $a86207c5d7f7e1fb$var$getLandmarkManager();
332
- let controller = instance === null || instance === void 0 ? void 0 : instance.createLandmarkController();
333
- let unsubscribe = $a86207c5d7f7e1fb$var$subscribe(()=>{
334
- // If the landmark manager changes, dispose the old
335
- // controller and create a new one.
336
- controller === null || controller === void 0 ? void 0 : controller.dispose();
337
- instance = $a86207c5d7f7e1fb$var$getLandmarkManager();
338
- controller = instance === null || instance === void 0 ? void 0 : instance.createLandmarkController();
339
- });
340
- // Return a wrapper that proxies requests to the current controller instance.
341
- return {
342
- navigate (direction, opts) {
343
- return controller.navigate(direction, opts);
344
- },
345
- focusNext (opts) {
346
- return controller.focusNext(opts);
347
- },
348
- focusPrevious (opts) {
349
- return controller.focusPrevious(opts);
350
- },
351
- focusMain () {
352
- return controller.focusMain();
353
- },
354
- dispose () {
355
- controller === null || controller === void 0 ? void 0 : controller.dispose();
356
- unsubscribe();
357
- controller = undefined;
358
- instance = null;
359
- }
360
- };
361
- }
362
- function $a86207c5d7f7e1fb$export$4cc632584fd87fae(props, ref) {
363
- const { role: role , "aria-label": ariaLabel , "aria-labelledby": ariaLabelledby , focus: focus } = props;
364
- let manager = $a86207c5d7f7e1fb$var$useLandmarkManager();
365
- let label = ariaLabel || ariaLabelledby;
366
- let [isLandmarkFocused, setIsLandmarkFocused] = (0, $TvsbU$useState)(false);
367
- let defaultFocus = (0, $TvsbU$useCallback)(()=>{
368
- setIsLandmarkFocused(true);
369
- }, [
370
- setIsLandmarkFocused
371
- ]);
372
- let blur = (0, $TvsbU$useCallback)(()=>{
373
- setIsLandmarkFocused(false);
374
- }, [
375
- setIsLandmarkFocused
376
- ]);
377
- (0, $TvsbU$useLayoutEffect)(()=>{
378
- if (manager) return manager.registerLandmark({
379
- ref: ref,
380
- label: label,
381
- role: role,
382
- focus: focus || defaultFocus,
383
- blur: blur
384
- });
385
- }, [
386
- manager,
387
- label,
388
- ref,
389
- role,
390
- focus,
391
- defaultFocus,
392
- blur
393
- ]);
394
- (0, $TvsbU$useEffect)(()=>{
395
- var _ref_current;
396
- if (isLandmarkFocused) (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.focus();
397
- }, [
398
- isLandmarkFocused,
399
- ref
400
- ]);
401
- return {
402
- landmarkProps: {
403
- role: role,
404
- tabIndex: isLandmarkFocused ? -1 : undefined,
405
- "aria-label": ariaLabel,
406
- "aria-labelledby": ariaLabelledby
407
- }
408
- };
409
- }
410
-
411
-
412
-
413
-
414
16
  export {$a86207c5d7f7e1fb$export$4cc632584fd87fae as useLandmark, $a86207c5d7f7e1fb$export$f50151dbd51cd1d9 as createLandmarkController};
415
17
  //# sourceMappingURL=module.js.map