@vaadin/a11y-base 24.1.0-rc1 → 24.1.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/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export {
12
12
  isKeyboardActive,
13
13
  } from './src/focus-utils.js';
14
14
  export { FocusTrapController } from './src/focus-trap-controller.js';
15
+ export { FocusRestorationController } from './src/focus-restoration-controller.js';
15
16
  export { KeyboardDirectionMixin } from './src/keyboard-direction-mixin.js';
16
17
  export { KeyboardMixin } from './src/keyboard-mixin.js';
17
18
  export { ListMixin } from './src/list-mixin.js';
package/index.js CHANGED
@@ -5,6 +5,7 @@ export { DisabledMixin } from './src/disabled-mixin.js';
5
5
  export { FieldAriaController } from './src/field-aria-controller.js';
6
6
  export { FocusMixin } from './src/focus-mixin.js';
7
7
  export { FocusTrapController } from './src/focus-trap-controller.js';
8
+ export { FocusRestorationController } from './src/focus-restoration-controller.js';
8
9
  export {
9
10
  getFocusableElements,
10
11
  isElementFocusable,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/a11y-base",
3
- "version": "24.1.0-rc1",
3
+ "version": "24.1.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -32,13 +32,13 @@
32
32
  "dependencies": {
33
33
  "@open-wc/dedupe-mixin": "^1.3.0",
34
34
  "@polymer/polymer": "^3.0.0",
35
- "@vaadin/component-base": "24.1.0-rc1",
35
+ "@vaadin/component-base": "~24.1.1",
36
36
  "lit": "^2.0.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@esm-bundle/chai": "^4.3.4",
40
- "@vaadin/testing-helpers": "^0.4.0",
40
+ "@vaadin/testing-helpers": "^0.4.2",
41
41
  "sinon": "^13.0.2"
42
42
  },
43
- "gitHead": "ae09abc65ea9616629a8924688bb841b5075e0d2"
43
+ "gitHead": "c3a3d904885bd37ebb07a84b09617a340b4fab7e"
44
44
  }
@@ -15,6 +15,12 @@ export class AriaModalController implements ReactiveController {
15
15
  */
16
16
  host: HTMLElement;
17
17
 
18
+ /**
19
+ * The callback used to detect which element
20
+ * to use as a target. Defaults to the host.
21
+ */
22
+ callback: () => HTMLElement | HTMLElement[];
23
+
18
24
  constructor(node: HTMLElement);
19
25
 
20
26
  /**
@@ -16,13 +16,21 @@ export class AriaModalController {
16
16
  /**
17
17
  * @param {HTMLElement} host
18
18
  */
19
- constructor(host) {
19
+ constructor(host, callback) {
20
20
  /**
21
21
  * The controller host element.
22
22
  *
23
23
  * @type {HTMLElement}
24
24
  */
25
25
  this.host = host;
26
+
27
+ /**
28
+ * The callback used to detect which element
29
+ * to use as a target. Defaults to the host.
30
+ *
31
+ * @type {Function}
32
+ */
33
+ this.callback = typeof callback === 'function' ? callback : () => host;
26
34
  }
27
35
 
28
36
  /**
@@ -33,7 +41,8 @@ export class AriaModalController {
33
41
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/showModal
34
42
  */
35
43
  showModal() {
36
- this.__showOthers = hideOthers(this.host);
44
+ const targets = this.callback();
45
+ this.__showOthers = hideOthers(targets);
37
46
  }
38
47
 
39
48
  /**
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+
8
+ /**
9
+ * A controller for saving a focused node and restoring focus to it later.
10
+ */
11
+ export declare function FocusRestorationController<T extends Constructor<HTMLElement>>(
12
+ base: T,
13
+ ): Constructor<FocusRestorationControllerClass> & T;
14
+
15
+ export declare class FocusRestorationControllerClass {
16
+ /**
17
+ * Saves the given node as a target for restoring focus to
18
+ * when `restoreFocus()` is called. If no node is provided,
19
+ * the currently focused node in the DOM is saved as a target.
20
+ */
21
+ saveFocus(node: Node | null | undefined): void;
22
+
23
+ /**
24
+ * Restores focus to the target node that was saved previously with `saveFocus()`.
25
+ */
26
+ restoreFocus(): void;
27
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { getDeepActiveElement } from './focus-utils.js';
7
+
8
+ /**
9
+ * A controller for saving a focused node and restoring focus to it later.
10
+ */
11
+ export class FocusRestorationController {
12
+ /**
13
+ * Saves the given node as a target for restoring focus to
14
+ * when `restoreFocus()` is called. If no node is provided,
15
+ * the currently focused node in the DOM is saved as a target.
16
+ *
17
+ * @param {Node | null | undefined} focusNode
18
+ */
19
+ saveFocus(focusNode) {
20
+ this.focusNode = focusNode || getDeepActiveElement();
21
+ }
22
+
23
+ /**
24
+ * Restores focus to the target node that was saved previously with `saveFocus()`.
25
+ */
26
+ restoreFocus() {
27
+ const focusNode = this.focusNode;
28
+ if (!focusNode) {
29
+ return;
30
+ }
31
+
32
+ if (getDeepActiveElement() === document.body) {
33
+ // In Firefox and Safari, focusing the node synchronously
34
+ // doesn't work as expected when the overlay is closing on outside click.
35
+ // These browsers force focus to move to the body element and retain it
36
+ // there until the next event loop iteration.
37
+ setTimeout(() => focusNode.focus());
38
+ } else {
39
+ focusNode.focus();
40
+ }
41
+
42
+ this.focusNode = null;
43
+ }
44
+ }