@react-aria/landmark 3.0.0-nightly.4552 → 3.0.0-nightly.4558

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