@spectrum-web-components/shared 1.1.0 → 1.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/package.json +3 -3
- package/src/first-focusable-in.d.ts +3 -0
- package/src/first-focusable-in.dev.js +15 -0
- package/src/first-focusable-in.dev.js.map +7 -0
- package/src/first-focusable-in.js +2 -0
- package/src/first-focusable-in.js.map +7 -0
- package/src/focus-visible.d.ts +29 -0
- package/src/focus-visible.dev.js +77 -0
- package/src/focus-visible.dev.js.map +7 -0
- package/src/focus-visible.js +2 -0
- package/src/focus-visible.js.map +7 -0
- package/src/focusable-selectors.d.ts +2 -0
- package/src/focusable-selectors.dev.js +15 -0
- package/src/focusable-selectors.dev.js.map +7 -0
- package/src/focusable-selectors.js +2 -0
- package/src/focusable-selectors.js.map +7 -0
- package/src/focusable.d.ts +56 -0
- package/src/focusable.dev.js +245 -0
- package/src/focusable.dev.js.map +7 -0
- package/src/focusable.js +2 -0
- package/src/focusable.js.map +7 -0
- package/src/get-active-element.d.ts +1 -0
- package/src/get-active-element.dev.js +5 -0
- package/src/get-active-element.dev.js.map +7 -0
- package/src/get-active-element.js +2 -0
- package/src/get-active-element.js.map +7 -0
- package/src/get-deep-element-from-point.d.ts +1 -0
- package/src/get-deep-element-from-point.dev.js +13 -0
- package/src/get-deep-element-from-point.dev.js.map +7 -0
- package/src/get-deep-element-from-point.js +2 -0
- package/src/get-deep-element-from-point.js.map +7 -0
- package/src/get-label-from-slot.d.ts +1 -0
- package/src/get-label-from-slot.dev.js +17 -0
- package/src/get-label-from-slot.dev.js.map +7 -0
- package/src/get-label-from-slot.js +2 -0
- package/src/get-label-from-slot.js.map +7 -0
- package/src/index.d.ts +12 -0
- package/src/index.dev.js +14 -0
- package/src/index.dev.js.map +7 -0
- package/src/index.js +2 -0
- package/src/index.js.map +7 -0
- package/src/like-anchor.d.ts +23 -0
- package/src/like-anchor.dev.js +63 -0
- package/src/like-anchor.dev.js.map +7 -0
- package/src/like-anchor.js +14 -0
- package/src/like-anchor.js.map +7 -0
- package/src/observe-slot-presence.d.ts +12 -0
- package/src/observe-slot-presence.dev.js +63 -0
- package/src/observe-slot-presence.dev.js.map +7 -0
- package/src/observe-slot-presence.js +2 -0
- package/src/observe-slot-presence.js.map +7 -0
- package/src/observe-slot-text.d.ts +11 -0
- package/src/observe-slot-text.dev.js +95 -0
- package/src/observe-slot-text.dev.js.map +7 -0
- package/src/observe-slot-text.js +2 -0
- package/src/observe-slot-text.js.map +7 -0
- package/src/platform.d.ts +10 -0
- package/src/platform.dev.js +39 -0
- package/src/platform.dev.js.map +7 -0
- package/src/platform.js +2 -0
- package/src/platform.js.map +7 -0
- package/src/random-id.d.ts +1 -0
- package/src/random-id.dev.js +8 -0
- package/src/random-id.dev.js.map +7 -0
- package/src/random-id.js +2 -0
- package/src/random-id.js.map +7 -0
- package/src/reparent-children.d.ts +4 -0
- package/src/reparent-children.dev.js +57 -0
- package/src/reparent-children.dev.js.map +7 -0
- package/src/reparent-children.js +2 -0
- package/src/reparent-children.js.map +7 -0
- package/test/focusable.test.js +25 -0
- package/test/focusable.test.js.map +7 -0
- package/test/observe-slot-presence.test.js +30 -0
- package/test/observe-slot-presence.test.js.map +7 -0
- package/test/observe-slot-text.test.js +27 -0
- package/test/observe-slot-text.test.js.map +7 -0
- package/test/random-id.test.js +25 -0
- package/test/random-id.test.js.map +7 -0
- package/test/reparent-children.test.js +254 -0
- package/test/reparent-children.test.js.map +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spectrum-web-components/shared",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
],
|
|
102
102
|
"dependencies": {
|
|
103
103
|
"@lit-labs/observers": "^2.0.2",
|
|
104
|
-
"@spectrum-web-components/base": "^1.1.
|
|
104
|
+
"@spectrum-web-components/base": "^1.1.1",
|
|
105
105
|
"focus-visible": "^5.1.0"
|
|
106
106
|
},
|
|
107
107
|
"types": "./src/index.d.ts",
|
|
@@ -109,5 +109,5 @@
|
|
|
109
109
|
"sideEffects": [
|
|
110
110
|
"./**/*.dev.js"
|
|
111
111
|
],
|
|
112
|
-
"gitHead": "
|
|
112
|
+
"gitHead": "44870aa95001c1b995456d994ae31bbe7277fac9"
|
|
113
113
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { userFocusableSelector } from "./focusable-selectors.dev.js";
|
|
3
|
+
export const firstFocusableIn = (root) => {
|
|
4
|
+
const firstFocusable = root.querySelector(
|
|
5
|
+
userFocusableSelector
|
|
6
|
+
);
|
|
7
|
+
return firstFocusable;
|
|
8
|
+
};
|
|
9
|
+
export const firstFocusableSlottedIn = (root) => {
|
|
10
|
+
const firstFocusable = root.assignedElements().find(
|
|
11
|
+
(element) => element.matches(userFocusableSelector)
|
|
12
|
+
);
|
|
13
|
+
return firstFocusable;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=first-focusable-in.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["first-focusable-in.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { SpectrumElement } from '@spectrum-web-components/base';\nimport { userFocusableSelector } from './focusable-selectors.dev.js'\n\nexport const firstFocusableIn = (\n root: HTMLElement | ShadowRoot\n): SpectrumElement | null => {\n const firstFocusable = root.querySelector(\n userFocusableSelector\n ) as SpectrumElement;\n return firstFocusable;\n};\n\nexport const firstFocusableSlottedIn = (\n root: HTMLSlotElement\n): SpectrumElement | null => {\n const firstFocusable = root\n .assignedElements()\n .find((element) =>\n element.matches(userFocusableSelector)\n ) as SpectrumElement;\n return firstFocusable;\n};\n"],
|
|
5
|
+
"mappings": ";AAaA,SAAS,6BAA6B;AAE/B,aAAM,mBAAmB,CAC5B,SACyB;AACzB,QAAM,iBAAiB,KAAK;AAAA,IACxB;AAAA,EACJ;AACA,SAAO;AACX;AAEO,aAAM,0BAA0B,CACnC,SACyB;AACzB,QAAM,iBAAiB,KAClB,iBAAiB,EACjB;AAAA,IAAK,CAAC,YACH,QAAQ,QAAQ,qBAAqB;AAAA,EACzC;AACJ,SAAO;AACX;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["first-focusable-in.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { SpectrumElement } from '@spectrum-web-components/base';\nimport { userFocusableSelector } from './focusable-selectors.js';\n\nexport const firstFocusableIn = (\n root: HTMLElement | ShadowRoot\n): SpectrumElement | null => {\n const firstFocusable = root.querySelector(\n userFocusableSelector\n ) as SpectrumElement;\n return firstFocusable;\n};\n\nexport const firstFocusableSlottedIn = (\n root: HTMLSlotElement\n): SpectrumElement | null => {\n const firstFocusable = root\n .assignedElements()\n .find((element) =>\n element.matches(userFocusableSelector)\n ) as SpectrumElement;\n return firstFocusable;\n};\n"],
|
|
5
|
+
"mappings": "aAaA,OAAS,yBAAAA,MAA6B,2BAE/B,aAAM,iBACTC,GAEuBA,EAAK,cACxBD,CACJ,EAIS,wBACTC,GAEuBA,EAClB,iBAAiB,EACjB,KAAMC,GACHA,EAAQ,QAAQF,CAAqB,CACzC",
|
|
6
|
+
"names": ["userFocusableSelector", "root", "element"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
applyFocusVisiblePolyfill?: (scope: Document | ShadowRoot) => void;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
type Constructor<T = Record<string, unknown>> = {
|
|
7
|
+
new (...args: any[]): T;
|
|
8
|
+
prototype: T;
|
|
9
|
+
};
|
|
10
|
+
interface OptionalLifecycleCallbacks {
|
|
11
|
+
connectedCallback?(): void;
|
|
12
|
+
disconnectedCallback?(): void;
|
|
13
|
+
manageAutoFocus?(): void;
|
|
14
|
+
}
|
|
15
|
+
type MixableBaseClass = HTMLElement & OptionalLifecycleCallbacks;
|
|
16
|
+
/**
|
|
17
|
+
* This mixin function is designed to be applied to a class that inherits
|
|
18
|
+
* from HTMLElement. It makes it easy for a custom element to coordinate with
|
|
19
|
+
* the :focus-visible polyfill.
|
|
20
|
+
*
|
|
21
|
+
* NOTE(cdata): The code here was adapted from an example proposed with the
|
|
22
|
+
* introduction of ShadowDOM support in the :focus-visible polyfill.
|
|
23
|
+
*
|
|
24
|
+
* @see https://github.com/WICG/focus-visible/pull/196
|
|
25
|
+
* @param {Function} SuperClass The base class implementation to decorate with
|
|
26
|
+
* implementation that coordinates with the :focus-visible polyfill
|
|
27
|
+
*/
|
|
28
|
+
export declare const FocusVisiblePolyfillMixin: <T extends Constructor<MixableBaseClass>>(SuperClass: T) => T;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
let hasFocusVisible = true;
|
|
3
|
+
try {
|
|
4
|
+
document.body.querySelector(":focus-visible");
|
|
5
|
+
} catch (error) {
|
|
6
|
+
hasFocusVisible = false;
|
|
7
|
+
import("focus-visible");
|
|
8
|
+
}
|
|
9
|
+
export const FocusVisiblePolyfillMixin = (SuperClass) => {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
const coordinateWithPolyfill = (instance) => {
|
|
12
|
+
if (instance.shadowRoot == null || instance.hasAttribute("data-js-focus-visible")) {
|
|
13
|
+
return () => {
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (self.applyFocusVisiblePolyfill) {
|
|
17
|
+
self.applyFocusVisiblePolyfill(instance.shadowRoot);
|
|
18
|
+
if (instance.manageAutoFocus) {
|
|
19
|
+
instance.manageAutoFocus();
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
const coordinationHandler = () => {
|
|
23
|
+
if (self.applyFocusVisiblePolyfill && instance.shadowRoot) {
|
|
24
|
+
self.applyFocusVisiblePolyfill(instance.shadowRoot);
|
|
25
|
+
}
|
|
26
|
+
if (instance.manageAutoFocus) {
|
|
27
|
+
instance.manageAutoFocus();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
self.addEventListener(
|
|
31
|
+
"focus-visible-polyfill-ready",
|
|
32
|
+
coordinationHandler,
|
|
33
|
+
{ once: true }
|
|
34
|
+
);
|
|
35
|
+
return () => {
|
|
36
|
+
self.removeEventListener(
|
|
37
|
+
"focus-visible-polyfill-ready",
|
|
38
|
+
coordinationHandler
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return () => {
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const $endPolyfillCoordination = Symbol("endPolyfillCoordination");
|
|
46
|
+
class FocusVisibleCoordinator extends (_b = SuperClass, _a = $endPolyfillCoordination, _b) {
|
|
47
|
+
constructor() {
|
|
48
|
+
super(...arguments);
|
|
49
|
+
this[_a] = null;
|
|
50
|
+
}
|
|
51
|
+
// Attempt to coordinate with the polyfill when connected to the
|
|
52
|
+
// document:
|
|
53
|
+
connectedCallback() {
|
|
54
|
+
super.connectedCallback && super.connectedCallback();
|
|
55
|
+
if (!hasFocusVisible) {
|
|
56
|
+
requestAnimationFrame(() => {
|
|
57
|
+
if (this[$endPolyfillCoordination] == null) {
|
|
58
|
+
this[$endPolyfillCoordination] = coordinateWithPolyfill(this);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
disconnectedCallback() {
|
|
64
|
+
super.disconnectedCallback && super.disconnectedCallback();
|
|
65
|
+
if (!hasFocusVisible) {
|
|
66
|
+
requestAnimationFrame(() => {
|
|
67
|
+
if (this[$endPolyfillCoordination] != null) {
|
|
68
|
+
this[$endPolyfillCoordination]();
|
|
69
|
+
this[$endPolyfillCoordination] = null;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return FocusVisibleCoordinator;
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=focus-visible.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["focus-visible.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\ndeclare global {\n interface Window {\n applyFocusVisiblePolyfill?: (scope: Document | ShadowRoot) => void;\n }\n}\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\ninterface OptionalLifecycleCallbacks {\n connectedCallback?(): void;\n disconnectedCallback?(): void;\n manageAutoFocus?(): void;\n}\n\ntype MixableBaseClass = HTMLElement & OptionalLifecycleCallbacks;\n\ntype EndPolyfillCoordinationCallback = () => void;\n\nlet hasFocusVisible = true;\n\ntry {\n document.body.querySelector(':focus-visible');\n} catch (error) {\n hasFocusVisible = false;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n import('focus-visible');\n}\n\n/**\n * This mixin function is designed to be applied to a class that inherits\n * from HTMLElement. It makes it easy for a custom element to coordinate with\n * the :focus-visible polyfill.\n *\n * NOTE(cdata): The code here was adapted from an example proposed with the\n * introduction of ShadowDOM support in the :focus-visible polyfill.\n *\n * @see https://github.com/WICG/focus-visible/pull/196\n * @param {Function} SuperClass The base class implementation to decorate with\n * implementation that coordinates with the :focus-visible polyfill\n */\nexport const FocusVisiblePolyfillMixin = <\n T extends Constructor<MixableBaseClass>\n>(\n SuperClass: T\n): T => {\n const coordinateWithPolyfill = (\n instance: MixableBaseClass\n ): EndPolyfillCoordinationCallback => {\n // If there is no shadow root, there is no need to coordinate with\n // the polyfill. If we already coordinated with the polyfill, we can\n // skip subsequent invokcations:\n if (\n instance.shadowRoot == null ||\n instance.hasAttribute('data-js-focus-visible')\n ) {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n return () => {};\n }\n\n // The polyfill might already be loaded. If so, we can apply it to\n // the shadow root immediately:\n if (self.applyFocusVisiblePolyfill) {\n self.applyFocusVisiblePolyfill(instance.shadowRoot);\n\n if (instance.manageAutoFocus) {\n instance.manageAutoFocus();\n }\n } else {\n const coordinationHandler = (): void => {\n if (self.applyFocusVisiblePolyfill && instance.shadowRoot) {\n self.applyFocusVisiblePolyfill(instance.shadowRoot);\n }\n\n if (instance.manageAutoFocus) {\n instance.manageAutoFocus();\n }\n };\n // Otherwise, wait for the polyfill to be loaded lazily. It might\n // never be loaded, but if it is then we can apply it to the\n // shadow root at the appropriate time by waiting for the ready\n // event:\n self.addEventListener(\n 'focus-visible-polyfill-ready',\n coordinationHandler,\n { once: true }\n );\n\n return () => {\n self.removeEventListener(\n 'focus-visible-polyfill-ready',\n coordinationHandler\n );\n };\n }\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n return () => {};\n };\n\n const $endPolyfillCoordination = Symbol('endPolyfillCoordination');\n\n // IE11 doesn't natively support custom elements or JavaScript class\n // syntax The mixin implementation assumes that the user will take the\n // appropriate steps to support both:\n class FocusVisibleCoordinator extends SuperClass {\n private [$endPolyfillCoordination]: EndPolyfillCoordinationCallback | null =\n null;\n\n // Attempt to coordinate with the polyfill when connected to the\n // document:\n override connectedCallback(): void {\n super.connectedCallback && super.connectedCallback();\n if (!hasFocusVisible) {\n requestAnimationFrame(() => {\n if (this[$endPolyfillCoordination] == null) {\n this[$endPolyfillCoordination] =\n coordinateWithPolyfill(this);\n }\n });\n }\n }\n\n override disconnectedCallback(): void {\n super.disconnectedCallback && super.disconnectedCallback();\n // It's important to remove the polyfill event listener when we\n // disconnect, otherwise we will leak the whole element via window:\n if (!hasFocusVisible) {\n requestAnimationFrame(() => {\n if (this[$endPolyfillCoordination] != null) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this[$endPolyfillCoordination]!();\n this[$endPolyfillCoordination] = null;\n }\n });\n }\n }\n }\n\n return FocusVisibleCoordinator;\n};\n"],
|
|
5
|
+
"mappings": ";AAkCA,IAAI,kBAAkB;AAEtB,IAAI;AACA,WAAS,KAAK,cAAc,gBAAgB;AAChD,SAAS,OAAO;AACZ,oBAAkB;AAGlB,SAAO,eAAe;AAC1B;AAcO,aAAM,4BAA4B,CAGrC,eACI;AA7DR;AA8DI,QAAM,yBAAyB,CAC3B,aACkC;AAIlC,QACI,SAAS,cAAc,QACvB,SAAS,aAAa,uBAAuB,GAC/C;AAEE,aAA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|
7
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";let i=!0;try{document.body.querySelector(":focus-visible")}catch(a){i=!1,import("focus-visible")}export const FocusVisiblePolyfillMixin=a=>{var s,t;const n=l=>{if(l.shadowRoot==null||l.hasAttribute("data-js-focus-visible"))return()=>{};if(self.applyFocusVisiblePolyfill)self.applyFocusVisiblePolyfill(l.shadowRoot),l.manageAutoFocus&&l.manageAutoFocus();else{const e=()=>{self.applyFocusVisiblePolyfill&&l.shadowRoot&&self.applyFocusVisiblePolyfill(l.shadowRoot),l.manageAutoFocus&&l.manageAutoFocus()};return self.addEventListener("focus-visible-polyfill-ready",e,{once:!0}),()=>{self.removeEventListener("focus-visible-polyfill-ready",e)}}return()=>{}},o=Symbol("endPolyfillCoordination");class c extends(t=a,s=o,t){constructor(){super(...arguments);this[s]=null}connectedCallback(){super.connectedCallback&&super.connectedCallback(),i||requestAnimationFrame(()=>{this[o]==null&&(this[o]=n(this))})}disconnectedCallback(){super.disconnectedCallback&&super.disconnectedCallback(),i||requestAnimationFrame(()=>{this[o]!=null&&(this[o](),this[o]=null)})}}return c};
|
|
2
|
+
//# sourceMappingURL=focus-visible.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["focus-visible.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\ndeclare global {\n interface Window {\n applyFocusVisiblePolyfill?: (scope: Document | ShadowRoot) => void;\n }\n}\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\ninterface OptionalLifecycleCallbacks {\n connectedCallback?(): void;\n disconnectedCallback?(): void;\n manageAutoFocus?(): void;\n}\n\ntype MixableBaseClass = HTMLElement & OptionalLifecycleCallbacks;\n\ntype EndPolyfillCoordinationCallback = () => void;\n\nlet hasFocusVisible = true;\n\ntry {\n document.body.querySelector(':focus-visible');\n} catch (error) {\n hasFocusVisible = false;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n import('focus-visible');\n}\n\n/**\n * This mixin function is designed to be applied to a class that inherits\n * from HTMLElement. It makes it easy for a custom element to coordinate with\n * the :focus-visible polyfill.\n *\n * NOTE(cdata): The code here was adapted from an example proposed with the\n * introduction of ShadowDOM support in the :focus-visible polyfill.\n *\n * @see https://github.com/WICG/focus-visible/pull/196\n * @param {Function} SuperClass The base class implementation to decorate with\n * implementation that coordinates with the :focus-visible polyfill\n */\nexport const FocusVisiblePolyfillMixin = <\n T extends Constructor<MixableBaseClass>\n>(\n SuperClass: T\n): T => {\n const coordinateWithPolyfill = (\n instance: MixableBaseClass\n ): EndPolyfillCoordinationCallback => {\n // If there is no shadow root, there is no need to coordinate with\n // the polyfill. If we already coordinated with the polyfill, we can\n // skip subsequent invokcations:\n if (\n instance.shadowRoot == null ||\n instance.hasAttribute('data-js-focus-visible')\n ) {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n return () => {};\n }\n\n // The polyfill might already be loaded. If so, we can apply it to\n // the shadow root immediately:\n if (self.applyFocusVisiblePolyfill) {\n self.applyFocusVisiblePolyfill(instance.shadowRoot);\n\n if (instance.manageAutoFocus) {\n instance.manageAutoFocus();\n }\n } else {\n const coordinationHandler = (): void => {\n if (self.applyFocusVisiblePolyfill && instance.shadowRoot) {\n self.applyFocusVisiblePolyfill(instance.shadowRoot);\n }\n\n if (instance.manageAutoFocus) {\n instance.manageAutoFocus();\n }\n };\n // Otherwise, wait for the polyfill to be loaded lazily. It might\n // never be loaded, but if it is then we can apply it to the\n // shadow root at the appropriate time by waiting for the ready\n // event:\n self.addEventListener(\n 'focus-visible-polyfill-ready',\n coordinationHandler,\n { once: true }\n );\n\n return () => {\n self.removeEventListener(\n 'focus-visible-polyfill-ready',\n coordinationHandler\n );\n };\n }\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n return () => {};\n };\n\n const $endPolyfillCoordination = Symbol('endPolyfillCoordination');\n\n // IE11 doesn't natively support custom elements or JavaScript class\n // syntax The mixin implementation assumes that the user will take the\n // appropriate steps to support both:\n class FocusVisibleCoordinator extends SuperClass {\n private [$endPolyfillCoordination]: EndPolyfillCoordinationCallback | null =\n null;\n\n // Attempt to coordinate with the polyfill when connected to the\n // document:\n override connectedCallback(): void {\n super.connectedCallback && super.connectedCallback();\n if (!hasFocusVisible) {\n requestAnimationFrame(() => {\n if (this[$endPolyfillCoordination] == null) {\n this[$endPolyfillCoordination] =\n coordinateWithPolyfill(this);\n }\n });\n }\n }\n\n override disconnectedCallback(): void {\n super.disconnectedCallback && super.disconnectedCallback();\n // It's important to remove the polyfill event listener when we\n // disconnect, otherwise we will leak the whole element via window:\n if (!hasFocusVisible) {\n requestAnimationFrame(() => {\n if (this[$endPolyfillCoordination] != null) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this[$endPolyfillCoordination]!();\n this[$endPolyfillCoordination] = null;\n }\n });\n }\n }\n }\n\n return FocusVisibleCoordinator;\n};\n"],
|
|
5
|
+
"mappings": "aAkCA,IAAIA,EAAkB,GAEtB,GAAI,CACA,SAAS,KAAK,cAAc,gBAAgB,CAChD,OAASC,EAAO,CACZD,EAAkB,GAGlB,OAAO,eAAe,CAC1B,CAcO,aAAM,0BAGTE,GACI,CA7DR,IAAAC,EAAAC,EA8DI,MAAMC,EACFC,GACkC,CAIlC,GACIA,EAAS,YAAc,MACvBA,EAAS,aAAa,uBAAuB,EAG7C,MAAO,IAAM,CAAC,EAKlB,GAAI,KAAK,0BACL,KAAK,0BAA0BA,EAAS,UAAU,EAE9CA,EAAS,iBACTA,EAAS,gBAAgB,MAE1B,CACH,MAAMC,EAAsB,IAAY,CAChC,KAAK,2BAA6BD,EAAS,YAC3C,KAAK,0BAA0BA,EAAS,UAAU,EAGlDA,EAAS,iBACTA,EAAS,gBAAgB,CAEjC,EAKA,YAAK,iBACD,+BACAC,EACA,CAAE,KAAM,EAAK,CACjB,EAEO,IAAM,CACT,KAAK,oBACD,+BACAA,CACJ,CACJ,CACJ,CAGA,MAAO,IAAM,CAAC,CAClB,EAEMC,EAA2B,OAAO,yBAAyB,EAKjE,MAAMC,UAAgCL,EAAAF,EACzBC,EAAAK,EADyBJ,EAAW,CAAjD,kCACI,KAASD,GACL,KAIK,mBAA0B,CAC/B,MAAM,mBAAqB,MAAM,kBAAkB,EAC9CH,GACD,sBAAsB,IAAM,CACpB,KAAKQ,CAAwB,GAAK,OAClC,KAAKA,CAAwB,EACzBH,EAAuB,IAAI,EAEvC,CAAC,CAET,CAES,sBAA6B,CAClC,MAAM,sBAAwB,MAAM,qBAAqB,EAGpDL,GACD,sBAAsB,IAAM,CACpB,KAAKQ,CAAwB,GAAK,OAElC,KAAKA,CAAwB,EAAG,EAChC,KAAKA,CAAwB,EAAI,KAEzC,CAAC,CAET,CACJ,CAEA,OAAOC,CACX",
|
|
6
|
+
"names": ["hasFocusVisible", "error", "SuperClass", "_a", "_b", "coordinateWithPolyfill", "instance", "coordinationHandler", "$endPolyfillCoordination", "FocusVisibleCoordinator"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const focusables = [
|
|
3
|
+
"button",
|
|
4
|
+
"[focusable]",
|
|
5
|
+
"[href]",
|
|
6
|
+
"input",
|
|
7
|
+
"label",
|
|
8
|
+
"select",
|
|
9
|
+
"textarea",
|
|
10
|
+
"[tabindex]"
|
|
11
|
+
];
|
|
12
|
+
const userFocuable = ':not([tabindex="-1"])';
|
|
13
|
+
export const userFocusableSelector = focusables.join(`${userFocuable}, `) + userFocuable;
|
|
14
|
+
export const focusableSelector = focusables.join(", ");
|
|
15
|
+
//# sourceMappingURL=focusable-selectors.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["focusable-selectors.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2023 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nconst focusables = [\n 'button',\n '[focusable]',\n '[href]',\n 'input',\n 'label',\n 'select',\n 'textarea',\n '[tabindex]',\n];\n\nconst userFocuable = ':not([tabindex=\"-1\"])';\n\nexport const userFocusableSelector =\n focusables.join(`${userFocuable}, `) + userFocuable;\n\nexport const focusableSelector = focusables.join(', ');\n"],
|
|
5
|
+
"mappings": ";AAYA,MAAM,aAAa;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AAEA,MAAM,eAAe;AAEd,aAAM,wBACT,WAAW,KAAK,GAAG,YAAY,IAAI,IAAI;AAEpC,aAAM,oBAAoB,WAAW,KAAK,IAAI;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["focusable-selectors.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2023 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nconst focusables = [\n 'button',\n '[focusable]',\n '[href]',\n 'input',\n 'label',\n 'select',\n 'textarea',\n '[tabindex]',\n];\n\nconst userFocuable = ':not([tabindex=\"-1\"])';\n\nexport const userFocusableSelector =\n focusables.join(`${userFocuable}, `) + userFocuable;\n\nexport const focusableSelector = focusables.join(', ');\n"],
|
|
5
|
+
"mappings": "aAYA,MAAMA,EAAa,CACf,SACA,cACA,SACA,QACA,QACA,SACA,WACA,YACJ,EAEMC,EAAe,wBAEd,aAAM,sBACTD,EAAW,KAAK,GAAGC,CAAY,IAAI,EAAIA,EAE9B,kBAAoBD,EAAW,KAAK,IAAI",
|
|
6
|
+
"names": ["focusables", "userFocuable"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { PropertyValues, SpectrumElement } from '@spectrum-web-components/base';
|
|
2
|
+
type DisableableElement = HTMLElement & {
|
|
3
|
+
disabled?: boolean;
|
|
4
|
+
};
|
|
5
|
+
declare const Focusable_base: typeof SpectrumElement;
|
|
6
|
+
/**
|
|
7
|
+
* Focusable base class handles tabindex setting into shadowed elements automatically.
|
|
8
|
+
*
|
|
9
|
+
* This implementation is based heavily on the aybolit delegate-focus-mixin at
|
|
10
|
+
* https://github.com/web-padawan/aybolit/blob/master/packages/core/src/mixins/delegate-focus-mixin.js
|
|
11
|
+
*/
|
|
12
|
+
export declare class Focusable extends Focusable_base {
|
|
13
|
+
/**
|
|
14
|
+
* Disable this control. It will not receive focus or events
|
|
15
|
+
*/
|
|
16
|
+
disabled: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* When this control is rendered, focus it automatically
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
autofocus: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The tab index to apply to this control. See general documentation about
|
|
24
|
+
* the tabindex HTML property
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
get tabIndex(): number;
|
|
29
|
+
set tabIndex(tabIndex: number);
|
|
30
|
+
private _tabIndex;
|
|
31
|
+
private onPointerdownManagementOfTabIndex;
|
|
32
|
+
private manageFocusElementTabindex;
|
|
33
|
+
private manipulatingTabindex;
|
|
34
|
+
/**
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
get focusElement(): DisableableElement;
|
|
38
|
+
/**
|
|
39
|
+
* @public
|
|
40
|
+
* @returns {boolean} whether the component should manage its focusElement tab-index or not
|
|
41
|
+
* Needed for action-menu to be supported in action-group in an accessible way
|
|
42
|
+
*/
|
|
43
|
+
get selfManageFocusElement(): boolean;
|
|
44
|
+
focus(options?: FocusOptions): void;
|
|
45
|
+
blur(): void;
|
|
46
|
+
click(): void;
|
|
47
|
+
protected manageAutoFocus(): void;
|
|
48
|
+
protected firstUpdated(changes: PropertyValues): void;
|
|
49
|
+
protected update(changedProperties: PropertyValues): void;
|
|
50
|
+
protected updated(changedProperties: PropertyValues): void;
|
|
51
|
+
private handleDisabledChanged;
|
|
52
|
+
protected getUpdateComplete(): Promise<boolean>;
|
|
53
|
+
private autofocusReady;
|
|
54
|
+
connectedCallback(): void;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
5
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
6
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
7
|
+
if (decorator = decorators[i])
|
|
8
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
9
|
+
if (kind && result) __defProp(target, key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
import { SpectrumElement } from "@spectrum-web-components/base";
|
|
13
|
+
import { property } from "@spectrum-web-components/base/src/decorators.js";
|
|
14
|
+
import { FocusVisiblePolyfillMixin } from "./focus-visible.dev.js";
|
|
15
|
+
function nextFrame() {
|
|
16
|
+
return new Promise((res) => requestAnimationFrame(() => res()));
|
|
17
|
+
}
|
|
18
|
+
export class Focusable extends FocusVisiblePolyfillMixin(SpectrumElement) {
|
|
19
|
+
constructor() {
|
|
20
|
+
super(...arguments);
|
|
21
|
+
this.disabled = false;
|
|
22
|
+
this.autofocus = false;
|
|
23
|
+
this._tabIndex = 0;
|
|
24
|
+
this.manipulatingTabindex = false;
|
|
25
|
+
this.autofocusReady = Promise.resolve();
|
|
26
|
+
}
|
|
27
|
+
get tabIndex() {
|
|
28
|
+
if (this.focusElement === this) {
|
|
29
|
+
const tabindex = this.hasAttribute("tabindex") ? Number(this.getAttribute("tabindex")) : NaN;
|
|
30
|
+
return !isNaN(tabindex) ? tabindex : -1;
|
|
31
|
+
}
|
|
32
|
+
const tabIndexAttribute = parseFloat(
|
|
33
|
+
this.hasAttribute("tabindex") ? this.getAttribute("tabindex") || "0" : "0"
|
|
34
|
+
);
|
|
35
|
+
if (this.disabled || tabIndexAttribute < 0) {
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
if (!this.focusElement) {
|
|
39
|
+
return tabIndexAttribute;
|
|
40
|
+
}
|
|
41
|
+
return this._tabIndex;
|
|
42
|
+
}
|
|
43
|
+
set tabIndex(tabIndex) {
|
|
44
|
+
var _a;
|
|
45
|
+
if (this.manipulatingTabindex) {
|
|
46
|
+
this.manipulatingTabindex = false;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (this.focusElement === this) {
|
|
50
|
+
if (this.disabled) {
|
|
51
|
+
this._tabIndex = tabIndex;
|
|
52
|
+
} else if (tabIndex !== this._tabIndex) {
|
|
53
|
+
this._tabIndex = tabIndex;
|
|
54
|
+
const tabindex = "" + tabIndex;
|
|
55
|
+
this.manipulatingTabindex = true;
|
|
56
|
+
this.setAttribute("tabindex", tabindex);
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (tabIndex === -1) {
|
|
61
|
+
this.addEventListener(
|
|
62
|
+
"pointerdown",
|
|
63
|
+
this.onPointerdownManagementOfTabIndex
|
|
64
|
+
);
|
|
65
|
+
} else {
|
|
66
|
+
this.manipulatingTabindex = true;
|
|
67
|
+
this.removeEventListener(
|
|
68
|
+
"pointerdown",
|
|
69
|
+
this.onPointerdownManagementOfTabIndex
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (tabIndex === -1 || this.disabled) {
|
|
73
|
+
this.manipulatingTabindex = true;
|
|
74
|
+
this.setAttribute("tabindex", "-1");
|
|
75
|
+
this.removeAttribute("focusable");
|
|
76
|
+
if (this.selfManageFocusElement) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (tabIndex !== -1) {
|
|
80
|
+
this._tabIndex = tabIndex;
|
|
81
|
+
this.manageFocusElementTabindex(tabIndex);
|
|
82
|
+
} else {
|
|
83
|
+
(_a = this.focusElement) == null ? void 0 : _a.removeAttribute("tabindex");
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.setAttribute("focusable", "");
|
|
88
|
+
if (this.hasAttribute("tabindex")) {
|
|
89
|
+
this.removeAttribute("tabindex");
|
|
90
|
+
} else {
|
|
91
|
+
this.manipulatingTabindex = false;
|
|
92
|
+
}
|
|
93
|
+
this._tabIndex = tabIndex;
|
|
94
|
+
this.manageFocusElementTabindex(tabIndex);
|
|
95
|
+
}
|
|
96
|
+
onPointerdownManagementOfTabIndex() {
|
|
97
|
+
if (this.tabIndex === -1) {
|
|
98
|
+
setTimeout(() => {
|
|
99
|
+
this.tabIndex = 0;
|
|
100
|
+
this.focus({ preventScroll: true });
|
|
101
|
+
this.tabIndex = -1;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async manageFocusElementTabindex(tabIndex) {
|
|
106
|
+
if (!this.focusElement) {
|
|
107
|
+
await this.updateComplete;
|
|
108
|
+
}
|
|
109
|
+
if (tabIndex === null) {
|
|
110
|
+
this.focusElement.removeAttribute("tabindex");
|
|
111
|
+
} else {
|
|
112
|
+
if (this.focusElement !== this) {
|
|
113
|
+
this.focusElement.tabIndex = tabIndex;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
get focusElement() {
|
|
121
|
+
throw new Error("Must implement focusElement getter!");
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @public
|
|
125
|
+
* @returns {boolean} whether the component should manage its focusElement tab-index or not
|
|
126
|
+
* Needed for action-menu to be supported in action-group in an accessible way
|
|
127
|
+
*/
|
|
128
|
+
get selfManageFocusElement() {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
focus(options) {
|
|
132
|
+
if (this.disabled || !this.focusElement) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (this.focusElement !== this) {
|
|
136
|
+
this.focusElement.focus(options);
|
|
137
|
+
} else {
|
|
138
|
+
HTMLElement.prototype.focus.apply(this, [options]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
blur() {
|
|
142
|
+
const focusElement = this.focusElement || this;
|
|
143
|
+
if (focusElement !== this) {
|
|
144
|
+
focusElement.blur();
|
|
145
|
+
} else {
|
|
146
|
+
HTMLElement.prototype.blur.apply(this);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
click() {
|
|
150
|
+
if (this.disabled) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const focusElement = this.focusElement || this;
|
|
154
|
+
if (focusElement !== this) {
|
|
155
|
+
focusElement.click();
|
|
156
|
+
} else {
|
|
157
|
+
HTMLElement.prototype.click.apply(this);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
manageAutoFocus() {
|
|
161
|
+
if (this.autofocus) {
|
|
162
|
+
this.dispatchEvent(
|
|
163
|
+
new KeyboardEvent("keydown", {
|
|
164
|
+
code: "Tab"
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
this.focusElement.focus();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
firstUpdated(changes) {
|
|
171
|
+
super.firstUpdated(changes);
|
|
172
|
+
if (!this.hasAttribute("tabindex") || this.getAttribute("tabindex") !== "-1") {
|
|
173
|
+
this.setAttribute("focusable", "");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
update(changedProperties) {
|
|
177
|
+
if (changedProperties.has("disabled")) {
|
|
178
|
+
this.handleDisabledChanged(
|
|
179
|
+
this.disabled,
|
|
180
|
+
changedProperties.get("disabled")
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
super.update(changedProperties);
|
|
184
|
+
}
|
|
185
|
+
updated(changedProperties) {
|
|
186
|
+
super.updated(changedProperties);
|
|
187
|
+
if (changedProperties.has("disabled") && this.disabled) {
|
|
188
|
+
this.blur();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async handleDisabledChanged(disabled, oldDisabled) {
|
|
192
|
+
const canSetDisabled = () => this.focusElement !== this && typeof this.focusElement.disabled !== "undefined";
|
|
193
|
+
if (disabled) {
|
|
194
|
+
this.manipulatingTabindex = true;
|
|
195
|
+
this.setAttribute("tabindex", "-1");
|
|
196
|
+
await this.updateComplete;
|
|
197
|
+
if (canSetDisabled()) {
|
|
198
|
+
this.focusElement.disabled = true;
|
|
199
|
+
} else {
|
|
200
|
+
this.setAttribute("aria-disabled", "true");
|
|
201
|
+
}
|
|
202
|
+
} else if (oldDisabled) {
|
|
203
|
+
this.manipulatingTabindex = true;
|
|
204
|
+
if (this.focusElement === this) {
|
|
205
|
+
this.setAttribute("tabindex", "" + this._tabIndex);
|
|
206
|
+
} else {
|
|
207
|
+
this.removeAttribute("tabindex");
|
|
208
|
+
}
|
|
209
|
+
await this.updateComplete;
|
|
210
|
+
if (canSetDisabled()) {
|
|
211
|
+
this.focusElement.disabled = false;
|
|
212
|
+
} else {
|
|
213
|
+
this.removeAttribute("aria-disabled");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async getUpdateComplete() {
|
|
218
|
+
const complete = await super.getUpdateComplete();
|
|
219
|
+
await this.autofocusReady;
|
|
220
|
+
return complete;
|
|
221
|
+
}
|
|
222
|
+
connectedCallback() {
|
|
223
|
+
super.connectedCallback();
|
|
224
|
+
if (this.autofocus) {
|
|
225
|
+
this.autofocusReady = new Promise(async (res) => {
|
|
226
|
+
await nextFrame();
|
|
227
|
+
await nextFrame();
|
|
228
|
+
res();
|
|
229
|
+
});
|
|
230
|
+
this.updateComplete.then(() => {
|
|
231
|
+
this.manageAutoFocus();
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
__decorateClass([
|
|
237
|
+
property({ type: Boolean, reflect: true })
|
|
238
|
+
], Focusable.prototype, "disabled", 2);
|
|
239
|
+
__decorateClass([
|
|
240
|
+
property({ type: Boolean })
|
|
241
|
+
], Focusable.prototype, "autofocus", 2);
|
|
242
|
+
__decorateClass([
|
|
243
|
+
property({ type: Number })
|
|
244
|
+
], Focusable.prototype, "tabIndex", 1);
|
|
245
|
+
//# sourceMappingURL=focusable.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["focusable.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { PropertyValues, SpectrumElement } from '@spectrum-web-components/base';\nimport { property } from '@spectrum-web-components/base/src/decorators.js';\n\nimport { FocusVisiblePolyfillMixin } from './focus-visible.dev.js'\n\ntype DisableableElement = HTMLElement & { disabled?: boolean };\n\nfunction nextFrame(): Promise<void> {\n return new Promise((res) => requestAnimationFrame(() => res()));\n}\n\n/**\n * Focusable base class handles tabindex setting into shadowed elements automatically.\n *\n * This implementation is based heavily on the aybolit delegate-focus-mixin at\n * https://github.com/web-padawan/aybolit/blob/master/packages/core/src/mixins/delegate-focus-mixin.js\n */\nexport class Focusable extends FocusVisiblePolyfillMixin(SpectrumElement) {\n /**\n * Disable this control. It will not receive focus or events\n */\n @property({ type: Boolean, reflect: true })\n public disabled = false;\n\n /**\n * When this control is rendered, focus it automatically\n * @private\n */\n @property({ type: Boolean })\n public override autofocus = false;\n\n /**\n * The tab index to apply to this control. See general documentation about\n * the tabindex HTML property\n *\n * @private\n */\n @property({ type: Number })\n public override get tabIndex(): number {\n if (this.focusElement === this) {\n const tabindex = this.hasAttribute('tabindex')\n ? Number(this.getAttribute('tabindex'))\n : NaN;\n return !isNaN(tabindex) ? tabindex : -1;\n }\n const tabIndexAttribute = parseFloat(\n this.hasAttribute('tabindex')\n ? (this.getAttribute('tabindex') as string) || '0'\n : '0'\n );\n // When `disabled` tabindex is -1.\n // When host tabindex -1, use that as the cache.\n if (this.disabled || tabIndexAttribute < 0) {\n return -1;\n }\n // When `focusElement` isn't available yet,\n // use host tabindex as the cache.\n if (!this.focusElement) {\n return tabIndexAttribute;\n }\n // All other times, use the tabindex of `focusElement`\n // as the cache for this value.\n // return this.focusElement.tabIndex;\n return this._tabIndex;\n }\n public override set tabIndex(tabIndex: number) {\n // Flipping `manipulatingTabindex` to true before a change\n // allows for that change NOT to effect the cached value of tabindex\n if (this.manipulatingTabindex) {\n this.manipulatingTabindex = false;\n return;\n }\n\n if (this.focusElement === this) {\n if (this.disabled) {\n this._tabIndex = tabIndex;\n } else if (tabIndex !== this._tabIndex) {\n this._tabIndex = tabIndex;\n const tabindex = '' + tabIndex;\n this.manipulatingTabindex = true;\n this.setAttribute('tabindex', tabindex);\n }\n return;\n }\n\n if (tabIndex === -1) {\n this.addEventListener(\n 'pointerdown',\n this.onPointerdownManagementOfTabIndex\n );\n } else {\n // All code paths are about to address the host tabindex without side effect.\n this.manipulatingTabindex = true;\n this.removeEventListener(\n 'pointerdown',\n this.onPointerdownManagementOfTabIndex\n );\n }\n\n if (tabIndex === -1 || this.disabled) {\n this.manipulatingTabindex = true;\n this.setAttribute('tabindex', '-1');\n this.removeAttribute('focusable');\n\n if (this.selfManageFocusElement) {\n return;\n }\n\n if (tabIndex !== -1) {\n this._tabIndex = tabIndex;\n this.manageFocusElementTabindex(tabIndex);\n } else {\n this.focusElement?.removeAttribute('tabindex');\n }\n return;\n }\n\n this.setAttribute('focusable', '');\n if (this.hasAttribute('tabindex')) {\n this.removeAttribute('tabindex');\n } else {\n // You can't remove an attribute that isn't there,\n // manually end the `manipulatingTabindex` guard.\n this.manipulatingTabindex = false;\n }\n\n this._tabIndex = tabIndex;\n this.manageFocusElementTabindex(tabIndex);\n }\n private _tabIndex = 0;\n\n private onPointerdownManagementOfTabIndex(): void {\n if (this.tabIndex === -1) {\n setTimeout(() => {\n // Ensure this happens _after_ WebKit attempts to focus the :host.\n this.tabIndex = 0;\n this.focus({ preventScroll: true });\n this.tabIndex = -1;\n });\n }\n }\n\n private async manageFocusElementTabindex(tabIndex: number): Promise<void> {\n if (!this.focusElement) {\n // allow setting these values to be async when needed.\n await this.updateComplete;\n }\n if (tabIndex === null) {\n this.focusElement.removeAttribute('tabindex');\n } else {\n if (this.focusElement !== this) {\n this.focusElement.tabIndex = tabIndex;\n }\n }\n }\n\n private manipulatingTabindex = false;\n\n /**\n * @private\n */\n public get focusElement(): DisableableElement {\n throw new Error('Must implement focusElement getter!');\n }\n\n /**\n * @public\n * @returns {boolean} whether the component should manage its focusElement tab-index or not\n * Needed for action-menu to be supported in action-group in an accessible way\n */\n public get selfManageFocusElement(): boolean {\n return false;\n }\n\n public override focus(options?: FocusOptions): void {\n if (this.disabled || !this.focusElement) {\n return;\n }\n\n if (this.focusElement !== this) {\n this.focusElement.focus(options);\n } else {\n HTMLElement.prototype.focus.apply(this, [options]);\n }\n }\n\n public override blur(): void {\n const focusElement = this.focusElement || this;\n if (focusElement !== this) {\n focusElement.blur();\n } else {\n HTMLElement.prototype.blur.apply(this);\n }\n }\n\n public override click(): void {\n if (this.disabled) {\n return;\n }\n\n const focusElement = this.focusElement || this;\n if (focusElement !== this) {\n focusElement.click();\n } else {\n HTMLElement.prototype.click.apply(this);\n }\n }\n\n protected manageAutoFocus(): void {\n if (this.autofocus) {\n /**\n * Trick :focus-visible polyfill into thinking keyboard based focus\n *\n * @private\n **/\n this.dispatchEvent(\n new KeyboardEvent('keydown', {\n code: 'Tab',\n })\n );\n this.focusElement.focus();\n }\n }\n\n protected override firstUpdated(changes: PropertyValues): void {\n super.firstUpdated(changes);\n if (\n !this.hasAttribute('tabindex') ||\n this.getAttribute('tabindex') !== '-1'\n ) {\n this.setAttribute('focusable', '');\n }\n }\n\n protected override update(changedProperties: PropertyValues): void {\n if (changedProperties.has('disabled')) {\n this.handleDisabledChanged(\n this.disabled,\n changedProperties.get('disabled') as boolean\n );\n }\n\n super.update(changedProperties);\n }\n\n protected override updated(changedProperties: PropertyValues): void {\n super.updated(changedProperties);\n\n if (changedProperties.has('disabled') && this.disabled) {\n this.blur();\n }\n }\n\n private async handleDisabledChanged(\n disabled: boolean,\n oldDisabled: boolean\n ): Promise<void> {\n const canSetDisabled = (): boolean =>\n this.focusElement !== this &&\n typeof this.focusElement.disabled !== 'undefined';\n if (disabled) {\n this.manipulatingTabindex = true;\n this.setAttribute('tabindex', '-1');\n await this.updateComplete;\n if (canSetDisabled()) {\n this.focusElement.disabled = true;\n } else {\n this.setAttribute('aria-disabled', 'true');\n }\n } else if (oldDisabled) {\n this.manipulatingTabindex = true;\n if (this.focusElement === this) {\n this.setAttribute('tabindex', '' + this._tabIndex);\n } else {\n this.removeAttribute('tabindex');\n }\n await this.updateComplete;\n if (canSetDisabled()) {\n this.focusElement.disabled = false;\n } else {\n this.removeAttribute('aria-disabled');\n }\n }\n }\n\n protected override async getUpdateComplete(): Promise<boolean> {\n const complete = (await super.getUpdateComplete()) as boolean;\n await this.autofocusReady;\n return complete;\n }\n\n private autofocusReady = Promise.resolve();\n\n public override connectedCallback(): void {\n super.connectedCallback();\n if (this.autofocus) {\n this.autofocusReady = new Promise(async (res) => {\n // If at connect time the [autofocus] content is placed within\n // content that needs to be \"hidden\" by default, it would need to wait\n // two rAFs for animations to be triggered on that content in\n // order for the [autofocus] to become \"visisble\" and have its\n // focus() capabilities enabled.\n //\n // Await this with `getUpdateComplete` so that the element cannot\n // become \"ready\" until `manageFocus` has occured.\n await nextFrame();\n await nextFrame();\n res();\n });\n this.updateComplete.then(() => {\n this.manageAutoFocus();\n });\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;AAWA,SAAyB,uBAAuB;AAChD,SAAS,gBAAgB;AAEzB,SAAS,iCAAiC;AAI1C,SAAS,YAA2B;AAChC,SAAO,IAAI,QAAQ,CAAC,QAAQ,sBAAsB,MAAM,IAAI,CAAC,CAAC;AAClE;AAQO,aAAM,kBAAkB,0BAA0B,eAAe,EAAE;AAAA,EAAnE;AAAA;AAKH,SAAO,WAAW;AAOlB,SAAgB,YAAY;AAoG5B,SAAQ,YAAY;AA2BpB,SAAQ,uBAAuB;AAuI/B,SAAQ,iBAAiB,QAAQ,QAAQ;AAAA;AAAA,EA7PzC,IAAoB,WAAmB;AACnC,QAAI,KAAK,iBAAiB,MAAM;AAC5B,YAAM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B,WAAK,uBAAuB;AAC5B,UAAI,KAAK,iBAAiB,MAAM;AAC5B,aAAK,aAAa,YAAY,KAAK,KAAK,SAAS;AAAA,MACrD,OAAO;AACH,aAAK,gBAAgB,UAAU;AAAA,MACnC;AACA,YAAM,KAAK;AACX,UAAI,eAAe,GAAG;AAClB,aAAK,aAAa,WAAW;AAAA,MACjC,OAAO;AACH,aAAK,gBAAgB,eAAe;AAAA,MACxC;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,MAAyB,oBAAsC;AAC3D,UAAM,WAAY,MAAM,MAAM,kBAAkB;AAChD,UAAM,KAAK;AACX,WAAO;AAAA,EACX;AAAA,EAIgB,oBAA0B;AACtC,UAAM,kBAAkB;AACxB,QAAI,KAAK,WAAW;AAChB,WAAK,iBAAiB,IAAI,QAAQ,OAAO,QAAQ;AAS7C,cAAM,UAAU;AAChB,cAAM,UAAU;AAChB,YAAI;AAAA,MACR,CAAC;AACD,WAAK,eAAe,KAAK,MAAM;AAC3B,aAAK,gBAAgB;AAAA,MACzB,CAAC;AAAA,IACL;AAAA,EACJ;AACJ;AApSW;AAAA,EADN,SAAS,EAAE,MAAM,SAAS,SAAS,KAAK,CAAC;AAAA,GAJjC,UAKF;AAOS;AAAA,EADf,SAAS,EAAE,MAAM,QAAQ,CAAC;AAAA,GAXlB,UAYO;AASI;AAAA,EADnB,SAAS,EAAE,MAAM,OAAO,CAAC;AAAA,GApBjB,UAqBW;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/src/focusable.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var n=(s,a,e,t)=>{for(var i=t>1?void 0:t?b(a,e):a,o=s.length-1,r;o>=0;o--)(r=s[o])&&(i=(t?r(a,e,i):r(i))||i);return t&&i&&d(a,e,i),i};import{SpectrumElement as h}from"@spectrum-web-components/base";import{property as l}from"@spectrum-web-components/base/src/decorators.js";import{FocusVisiblePolyfillMixin as m}from"./focus-visible.js";function u(){return new Promise(s=>requestAnimationFrame(()=>s()))}export class Focusable extends m(h){constructor(){super(...arguments);this.disabled=!1;this.autofocus=!1;this._tabIndex=0;this.manipulatingTabindex=!1;this.autofocusReady=Promise.resolve()}get tabIndex(){if(this.focusElement===this){const t=this.hasAttribute("tabindex")?Number(this.getAttribute("tabindex")):NaN;return isNaN(t)?-1:t}const e=parseFloat(this.hasAttribute("tabindex")&&this.getAttribute("tabindex")||"0");return this.disabled||e<0?-1:this.focusElement?this._tabIndex:e}set tabIndex(e){var t;if(this.manipulatingTabindex){this.manipulatingTabindex=!1;return}if(this.focusElement===this){if(this.disabled)this._tabIndex=e;else if(e!==this._tabIndex){this._tabIndex=e;const i=""+e;this.manipulatingTabindex=!0,this.setAttribute("tabindex",i)}return}if(e===-1?this.addEventListener("pointerdown",this.onPointerdownManagementOfTabIndex):(this.manipulatingTabindex=!0,this.removeEventListener("pointerdown",this.onPointerdownManagementOfTabIndex)),e===-1||this.disabled){if(this.manipulatingTabindex=!0,this.setAttribute("tabindex","-1"),this.removeAttribute("focusable"),this.selfManageFocusElement)return;e!==-1?(this._tabIndex=e,this.manageFocusElementTabindex(e)):(t=this.focusElement)==null||t.removeAttribute("tabindex");return}this.setAttribute("focusable",""),this.hasAttribute("tabindex")?this.removeAttribute("tabindex"):this.manipulatingTabindex=!1,this._tabIndex=e,this.manageFocusElementTabindex(e)}onPointerdownManagementOfTabIndex(){this.tabIndex===-1&&setTimeout(()=>{this.tabIndex=0,this.focus({preventScroll:!0}),this.tabIndex=-1})}async manageFocusElementTabindex(e){this.focusElement||await this.updateComplete,e===null?this.focusElement.removeAttribute("tabindex"):this.focusElement!==this&&(this.focusElement.tabIndex=e)}get focusElement(){throw new Error("Must implement focusElement getter!")}get selfManageFocusElement(){return!1}focus(e){this.disabled||!this.focusElement||(this.focusElement!==this?this.focusElement.focus(e):HTMLElement.prototype.focus.apply(this,[e]))}blur(){const e=this.focusElement||this;e!==this?e.blur():HTMLElement.prototype.blur.apply(this)}click(){if(this.disabled)return;const e=this.focusElement||this;e!==this?e.click():HTMLElement.prototype.click.apply(this)}manageAutoFocus(){this.autofocus&&(this.dispatchEvent(new KeyboardEvent("keydown",{code:"Tab"})),this.focusElement.focus())}firstUpdated(e){super.firstUpdated(e),(!this.hasAttribute("tabindex")||this.getAttribute("tabindex")!=="-1")&&this.setAttribute("focusable","")}update(e){e.has("disabled")&&this.handleDisabledChanged(this.disabled,e.get("disabled")),super.update(e)}updated(e){super.updated(e),e.has("disabled")&&this.disabled&&this.blur()}async handleDisabledChanged(e,t){const i=()=>this.focusElement!==this&&typeof this.focusElement.disabled!="undefined";e?(this.manipulatingTabindex=!0,this.setAttribute("tabindex","-1"),await this.updateComplete,i()?this.focusElement.disabled=!0:this.setAttribute("aria-disabled","true")):t&&(this.manipulatingTabindex=!0,this.focusElement===this?this.setAttribute("tabindex",""+this._tabIndex):this.removeAttribute("tabindex"),await this.updateComplete,i()?this.focusElement.disabled=!1:this.removeAttribute("aria-disabled"))}async getUpdateComplete(){const e=await super.getUpdateComplete();return await this.autofocusReady,e}connectedCallback(){super.connectedCallback(),this.autofocus&&(this.autofocusReady=new Promise(async e=>{await u(),await u(),e()}),this.updateComplete.then(()=>{this.manageAutoFocus()}))}}n([l({type:Boolean,reflect:!0})],Focusable.prototype,"disabled",2),n([l({type:Boolean})],Focusable.prototype,"autofocus",2),n([l({type:Number})],Focusable.prototype,"tabIndex",1);
|
|
2
|
+
//# sourceMappingURL=focusable.js.map
|