@spectrum-web-components/reactive-controllers 0.2.2 → 0.2.5-devmode.79

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/src/MatchMedia.js CHANGED
@@ -1,26 +1,26 @@
1
- export const DARK_MODE = '(prefers-color-scheme: dark)';
2
- export const IS_MOBILE = '(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)';
1
+ export const DARK_MODE = "(prefers-color-scheme: dark)";
2
+ export const IS_MOBILE = "(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)";
3
3
  export class MatchMediaController {
4
- constructor(host, query) {
5
- this.key = Symbol('match-media-key');
6
- this.matches = false;
7
- this.host = host;
8
- this.media = window.matchMedia(query);
9
- this.matches = this.media.matches;
10
- this.onChange = this.onChange.bind(this);
11
- host.addController(this);
12
- }
13
- hostConnected() {
14
- this.media.addEventListener('change', this.onChange);
15
- }
16
- hostDisconnected() {
17
- this.media.removeEventListener('change', this.onChange);
18
- }
19
- onChange(event) {
20
- if (this.matches === event.matches)
21
- return;
22
- this.matches = event.matches;
23
- this.host.requestUpdate(this.key, !this.matches);
24
- }
4
+ constructor(host, query) {
5
+ this.key = Symbol("match-media-key");
6
+ this.matches = false;
7
+ this.host = host;
8
+ this.media = window.matchMedia(query);
9
+ this.matches = this.media.matches;
10
+ this.onChange = this.onChange.bind(this);
11
+ host.addController(this);
12
+ }
13
+ hostConnected() {
14
+ this.media.addEventListener("change", this.onChange);
15
+ }
16
+ hostDisconnected() {
17
+ this.media.removeEventListener("change", this.onChange);
18
+ }
19
+ onChange(event) {
20
+ if (this.matches === event.matches)
21
+ return;
22
+ this.matches = event.matches;
23
+ this.host.requestUpdate(this.key, !this.matches);
24
+ }
25
25
  }
26
- //# sourceMappingURL=MatchMedia.js.map
26
+ //# sourceMappingURL=MatchMedia.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"MatchMedia.js","sourceRoot":"","sources":["MatchMedia.ts"],"names":[],"mappings":"AAaA,MAAM,CAAC,MAAM,SAAS,GAAG,8BAA8B,CAAC;AACxD,MAAM,CAAC,MAAM,SAAS,GAClB,yHAAyH,CAAC;AAE9H,MAAM,OAAO,oBAAoB;IAS7B,YAAY,IAAqB,EAAE,KAAa;QARhD,QAAG,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;QAEhC,YAAO,GAAG,KAAK,CAAC;QAOZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAEM,aAAa;QAChB,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;IAEM,gBAAgB;QACnB,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC5D,CAAC;IAES,QAAQ,CAAC,KAA0B;QACzC,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO;YAAE,OAAO;QAC3C,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACrD,CAAC;CACJ","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 type { ReactiveController, ReactiveElement } from 'lit';\n\nexport const DARK_MODE = '(prefers-color-scheme: dark)';\nexport const IS_MOBILE =\n '(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)';\n\nexport class MatchMediaController implements ReactiveController {\n key = Symbol('match-media-key');\n\n matches = false;\n\n protected host: ReactiveElement;\n\n protected media: MediaQueryList;\n\n constructor(host: ReactiveElement, query: string) {\n this.host = host;\n this.media = window.matchMedia(query);\n this.matches = this.media.matches;\n this.onChange = this.onChange.bind(this);\n host.addController(this);\n }\n\n public hostConnected(): void {\n this.media.addEventListener('change', this.onChange);\n }\n\n public hostDisconnected(): void {\n this.media.removeEventListener('change', this.onChange);\n }\n\n protected onChange(event: MediaQueryListEvent): void {\n if (this.matches === event.matches) return;\n this.matches = event.matches;\n this.host.requestUpdate(this.key, !this.matches);\n }\n}\n"]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["MatchMedia.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 type { ReactiveController, ReactiveElement } from 'lit';\n\nexport const DARK_MODE = '(prefers-color-scheme: dark)';\nexport const IS_MOBILE =\n '(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)';\n\nexport class MatchMediaController implements ReactiveController {\n key = Symbol('match-media-key');\n\n matches = false;\n\n protected host: ReactiveElement;\n\n protected media: MediaQueryList;\n\n constructor(host: ReactiveElement, query: string) {\n this.host = host;\n this.media = window.matchMedia(query);\n this.matches = this.media.matches;\n this.onChange = this.onChange.bind(this);\n host.addController(this);\n }\n\n public hostConnected(): void {\n this.media.addEventListener('change', this.onChange);\n }\n\n public hostDisconnected(): void {\n this.media.removeEventListener('change', this.onChange);\n }\n\n protected onChange(event: MediaQueryListEvent): void {\n if (this.matches === event.matches) return;\n this.matches = event.matches;\n this.host.requestUpdate(this.key, !this.matches);\n }\n}\n"],
5
+ "mappings": "AAaO,aAAM,YAAY;AAClB,aAAM,YACT;AAEG,aAAM,qBAAmD;AAAA,EAS5D,YAAY,MAAuB,OAAe;AARlD,eAAM,OAAO,iBAAiB;AAE9B,mBAAU;AAON,SAAK,OAAO;AACZ,SAAK,QAAQ,OAAO,WAAW,KAAK;AACpC,SAAK,UAAU,KAAK,MAAM;AAC1B,SAAK,WAAW,KAAK,SAAS,KAAK,IAAI;AACvC,SAAK,cAAc,IAAI;AAAA,EAC3B;AAAA,EAEO,gBAAsB;AACzB,SAAK,MAAM,iBAAiB,UAAU,KAAK,QAAQ;AAAA,EACvD;AAAA,EAEO,mBAAyB;AAC5B,SAAK,MAAM,oBAAoB,UAAU,KAAK,QAAQ;AAAA,EAC1D;AAAA,EAEU,SAAS,OAAkC;AACjD,QAAI,KAAK,YAAY,MAAM;AAAS;AACpC,SAAK,UAAU,MAAM;AACrB,SAAK,KAAK,cAAc,KAAK,KAAK,CAAC,KAAK,OAAO;AAAA,EACnD;AACJ;",
6
+ "names": []
7
+ }
@@ -1,62 +1,19 @@
1
- import type { ReactiveController, ReactiveElement } from 'lit';
2
- declare type DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';
3
- export declare type RovingTabindexConfig<T> = {
4
- focusInIndex?: (_elements: T[]) => number;
5
- direction?: DirectionTypes | (() => DirectionTypes);
6
- elementEnterAction?: (el: T) => void;
7
- elements: () => T[];
8
- isFocusableElement?: (el: T) => boolean;
9
- listenerScope?: HTMLElement | (() => HTMLElement);
10
- };
1
+ import { FocusGroupConfig, FocusGroupController } from './FocusGroup.js';
2
+ export declare type RovingTabindexConfig<T> = FocusGroupConfig<T>;
11
3
  interface UpdateTabIndexes {
12
4
  tabIndex: number;
13
5
  removeTabIndex?: boolean;
14
6
  }
15
- export declare class RovingTabindexController<T extends HTMLElement> implements ReactiveController {
16
- private cachedElements?;
17
- get currentIndex(): number;
18
- set currentIndex(currentIndex: number);
19
- private _currentIndex;
20
- get direction(): DirectionTypes;
21
- _direction: () => DirectionTypes;
22
- directionLength: number;
23
- elementEnterAction: (_el: T) => void;
24
- get elements(): T[];
25
- private _elements;
26
- firstUpdated: boolean;
27
- private set focused(value);
28
- private get focused();
29
- private _focused;
30
- get focusInElement(): T;
31
- get focusInIndex(): number;
32
- _focusInIndex: (_elements: T[]) => number;
33
- host: ReactiveElement;
34
- isFocusableElement: (_el: T) => boolean;
35
- isEventWithinListenerScope(event: Event): boolean;
36
- _listenerScope: () => HTMLElement;
37
- offset: number;
7
+ export declare class RovingTabindexController<T extends HTMLElement> extends FocusGroupController<T> {
8
+ protected set focused(focused: boolean);
9
+ protected get focused(): boolean;
38
10
  private managed;
39
- constructor(host: ReactiveElement, { direction, elementEnterAction, elements, focusInIndex, isFocusableElement, listenerScope, }?: RovingTabindexConfig<T>);
40
- update({ elements }?: RovingTabindexConfig<T>): void;
41
- focus(options?: FocusOptions): void;
42
11
  private manageIndexesAnimationFrame;
43
12
  clearElementCache(offset?: number): void;
44
- setCurrentIndexCircularly(diff: number): void;
45
- hostContainsFocus(): void;
46
- hostNoLongerContainsFocus(): void;
47
- isRelatedTargetAnElement(event: FocusEvent): boolean;
48
- handleFocusin: (event: FocusEvent) => void;
49
- handleFocusout: (event: FocusEvent) => void;
50
- acceptsEventCode(code: string): boolean;
51
- handleKeydown: (event: KeyboardEvent) => void;
52
13
  manageTabindexes(): void;
53
14
  updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void;
54
15
  manage(): void;
55
16
  unmanage(): void;
56
- addEventListeners(): void;
57
- removeEventListeners(): void;
58
17
  hostUpdated(): void;
59
- hostConnected(): void;
60
- hostDisconnected(): void;
61
18
  }
62
19
  export {};
@@ -0,0 +1,65 @@
1
+ import { FocusGroupController } from "./FocusGroup.dev.js";
2
+ export class RovingTabindexController extends FocusGroupController {
3
+ constructor() {
4
+ super(...arguments);
5
+ this.managed = true;
6
+ this.manageIndexesAnimationFrame = 0;
7
+ }
8
+ set focused(focused) {
9
+ if (focused === this.focused)
10
+ return;
11
+ super.focused = focused;
12
+ this.manageTabindexes();
13
+ }
14
+ get focused() {
15
+ return super.focused;
16
+ }
17
+ clearElementCache(offset = 0) {
18
+ cancelAnimationFrame(this.manageIndexesAnimationFrame);
19
+ super.clearElementCache(offset);
20
+ if (!this.managed)
21
+ return;
22
+ this.manageIndexesAnimationFrame = requestAnimationFrame(() => this.manageTabindexes());
23
+ }
24
+ manageTabindexes() {
25
+ if (this.focused) {
26
+ this.updateTabindexes(() => ({ tabIndex: -1 }));
27
+ } else {
28
+ this.updateTabindexes((el) => {
29
+ return {
30
+ removeTabIndex: el.contains(this.focusInElement) && el !== this.focusInElement,
31
+ tabIndex: el === this.focusInElement ? 0 : -1
32
+ };
33
+ });
34
+ }
35
+ }
36
+ updateTabindexes(getTabIndex) {
37
+ this.elements.forEach((el) => {
38
+ const { tabIndex, removeTabIndex } = getTabIndex(el);
39
+ if (!removeTabIndex) {
40
+ el.tabIndex = tabIndex;
41
+ return;
42
+ }
43
+ el.removeAttribute("tabindex");
44
+ const updatable = el;
45
+ if (updatable.requestUpdate)
46
+ updatable.requestUpdate();
47
+ });
48
+ }
49
+ manage() {
50
+ this.managed = true;
51
+ this.manageTabindexes();
52
+ super.manage();
53
+ }
54
+ unmanage() {
55
+ this.managed = false;
56
+ this.updateTabindexes(() => ({ tabIndex: 0 }));
57
+ super.unmanage();
58
+ }
59
+ hostUpdated() {
60
+ if (!this.host.hasUpdated) {
61
+ this.manageTabindexes();
62
+ }
63
+ }
64
+ }
65
+ //# sourceMappingURL=RovingTabindex.dev.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["RovingTabindex.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 { FocusGroupConfig, FocusGroupController } from './FocusGroup.dev.js'\n\nexport type RovingTabindexConfig<T> = FocusGroupConfig<T>;\ninterface UpdateTabIndexes {\n tabIndex: number;\n removeTabIndex?: boolean;\n}\n\nexport class RovingTabindexController<\n T extends HTMLElement\n> extends FocusGroupController<T> {\n protected override set focused(focused: boolean) {\n if (focused === this.focused) return;\n super.focused = focused;\n this.manageTabindexes();\n }\n\n protected override get focused(): boolean {\n return super.focused;\n }\n\n private managed = true;\n\n private manageIndexesAnimationFrame = 0;\n\n override clearElementCache(offset = 0): void {\n cancelAnimationFrame(this.manageIndexesAnimationFrame);\n super.clearElementCache(offset);\n if (!this.managed) return;\n\n this.manageIndexesAnimationFrame = requestAnimationFrame(() =>\n this.manageTabindexes()\n );\n }\n\n manageTabindexes(): void {\n if (this.focused) {\n this.updateTabindexes(() => ({ tabIndex: -1 }));\n } else {\n this.updateTabindexes((el: HTMLElement): UpdateTabIndexes => {\n return {\n removeTabIndex:\n el.contains(this.focusInElement) &&\n el !== this.focusInElement,\n tabIndex: el === this.focusInElement ? 0 : -1,\n };\n });\n }\n }\n\n updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void {\n this.elements.forEach((el) => {\n const { tabIndex, removeTabIndex } = getTabIndex(el);\n if (!removeTabIndex) {\n el.tabIndex = tabIndex;\n return;\n }\n el.removeAttribute('tabindex');\n const updatable = el as unknown as {\n requestUpdate?: () => void;\n };\n if (updatable.requestUpdate) updatable.requestUpdate();\n });\n }\n\n override manage(): void {\n this.managed = true;\n this.manageTabindexes();\n super.manage();\n }\n\n override unmanage(): void {\n this.managed = false;\n this.updateTabindexes(() => ({ tabIndex: 0 }));\n super.unmanage();\n }\n\n hostUpdated(): void {\n if (!this.host.hasUpdated) {\n this.manageTabindexes();\n }\n }\n}\n"],
5
+ "mappings": "AAWA;AAQO,aAAM,iCAEH,qBAAwB;AAAA,EAF3B;AAAA;AAaK,mBAAU;AAEV,uCAA8B;AAAA;AAAA,MAZf,QAAQ,SAAkB;AAC7C,QAAI,YAAY,KAAK;AAAS;AAC9B,UAAM,UAAU;AAChB,SAAK,iBAAiB;AAAA,EAC1B;AAAA,MAEuB,UAAmB;AACtC,WAAO,MAAM;AAAA,EACjB;AAAA,EAMS,kBAAkB,SAAS,GAAS;AACzC,yBAAqB,KAAK,2BAA2B;AACrD,UAAM,kBAAkB,MAAM;AAC9B,QAAI,CAAC,KAAK;AAAS;AAEnB,SAAK,8BAA8B,sBAAsB,MACrD,KAAK,iBAAiB,CAC1B;AAAA,EACJ;AAAA,EAEA,mBAAyB;AACrB,QAAI,KAAK,SAAS;AACd,WAAK,iBAAiB,MAAO,GAAE,UAAU,GAAG,EAAE;AAAA,IAClD,OAAO;AACH,WAAK,iBAAiB,CAAC,OAAsC;AACzD,eAAO;AAAA,UACH,gBACI,GAAG,SAAS,KAAK,cAAc,KAC/B,OAAO,KAAK;AAAA,UAChB,UAAU,OAAO,KAAK,iBAAiB,IAAI;AAAA,QAC/C;AAAA,MACJ,CAAC;AAAA,IACL;AAAA,EACJ;AAAA,EAEA,iBAAiB,aAA0D;AACvE,SAAK,SAAS,QAAQ,CAAC,OAAO;AAC1B,YAAM,EAAE,UAAU,mBAAmB,YAAY,EAAE;AACnD,UAAI,CAAC,gBAAgB;AACjB,WAAG,WAAW;AACd;AAAA,MACJ;AACA,SAAG,gBAAgB,UAAU;AAC7B,YAAM,YAAY;AAGlB,UAAI,UAAU;AAAe,kBAAU,cAAc;AAAA,IACzD,CAAC;AAAA,EACL;AAAA,EAES,SAAe;AACpB,SAAK,UAAU;AACf,SAAK,iBAAiB;AACtB,UAAM,OAAO;AAAA,EACjB;AAAA,EAES,WAAiB;AACtB,SAAK,UAAU;AACf,SAAK,iBAAiB,MAAO,GAAE,UAAU,EAAE,EAAE;AAC7C,UAAM,SAAS;AAAA,EACnB;AAAA,EAEA,cAAoB;AAChB,QAAI,CAAC,KAAK,KAAK,YAAY;AACvB,WAAK,iBAAiB;AAAA,IAC1B;AAAA,EACJ;AACJ;",
6
+ "names": []
7
+ }
@@ -1,267 +1,65 @@
1
- export class RovingTabindexController {
2
- constructor(host, { direction, elementEnterAction, elements, focusInIndex, isFocusableElement, listenerScope, } = { elements: () => [] }) {
3
- this._currentIndex = -1;
4
- this._direction = () => 'both';
5
- this.directionLength = 5;
6
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
7
- this.elementEnterAction = (_el) => {
8
- return;
1
+ import { FocusGroupController } from "./FocusGroup.js";
2
+ export class RovingTabindexController extends FocusGroupController {
3
+ constructor() {
4
+ super(...arguments);
5
+ this.managed = true;
6
+ this.manageIndexesAnimationFrame = 0;
7
+ }
8
+ set focused(focused) {
9
+ if (focused === this.focused)
10
+ return;
11
+ super.focused = focused;
12
+ this.manageTabindexes();
13
+ }
14
+ get focused() {
15
+ return super.focused;
16
+ }
17
+ clearElementCache(offset = 0) {
18
+ cancelAnimationFrame(this.manageIndexesAnimationFrame);
19
+ super.clearElementCache(offset);
20
+ if (!this.managed)
21
+ return;
22
+ this.manageIndexesAnimationFrame = requestAnimationFrame(() => this.manageTabindexes());
23
+ }
24
+ manageTabindexes() {
25
+ if (this.focused) {
26
+ this.updateTabindexes(() => ({ tabIndex: -1 }));
27
+ } else {
28
+ this.updateTabindexes((el) => {
29
+ return {
30
+ removeTabIndex: el.contains(this.focusInElement) && el !== this.focusInElement,
31
+ tabIndex: el === this.focusInElement ? 0 : -1
9
32
  };
10
- this.firstUpdated = true;
11
- this._focused = false;
12
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
- this._focusInIndex = (_elements) => 0;
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- this.isFocusableElement = (_el) => true;
16
- this._listenerScope = () => this.host;
17
- // When elements are virtualized, the delta between the first element
18
- // and the first rendered element.
19
- this.offset = 0;
20
- this.managed = true;
21
- this.manageIndexesAnimationFrame = 0;
22
- this.handleFocusin = (event) => {
23
- if (!this.isEventWithinListenerScope(event))
24
- return;
25
- if (this.isRelatedTargetAnElement(event)) {
26
- this.hostContainsFocus();
27
- }
28
- const path = event.composedPath();
29
- let targetIndex = -1;
30
- path.find((el) => {
31
- targetIndex = this.elements.indexOf(el);
32
- return targetIndex !== -1;
33
- });
34
- this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;
35
- };
36
- this.handleFocusout = (event) => {
37
- if (this.isRelatedTargetAnElement(event)) {
38
- this.hostNoLongerContainsFocus();
39
- }
40
- };
41
- this.handleKeydown = (event) => {
42
- if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {
43
- return;
44
- }
45
- let diff = 0;
46
- switch (event.code) {
47
- case 'ArrowRight':
48
- diff += 1;
49
- break;
50
- case 'ArrowDown':
51
- diff += this.direction === 'grid' ? this.directionLength : 1;
52
- break;
53
- case 'ArrowLeft':
54
- diff -= 1;
55
- break;
56
- case 'ArrowUp':
57
- diff -= this.direction === 'grid' ? this.directionLength : 1;
58
- break;
59
- case 'End':
60
- this.currentIndex = 0;
61
- diff -= 1;
62
- break;
63
- case 'Home':
64
- this.currentIndex = this.elements.length - 1;
65
- diff += 1;
66
- break;
67
- }
68
- event.preventDefault();
69
- if (this.direction === 'grid' && this.currentIndex + diff < 0) {
70
- this.currentIndex = 0;
71
- }
72
- else if (this.direction === 'grid' &&
73
- this.currentIndex + diff > this.elements.length - 1) {
74
- this.currentIndex = this.elements.length - 1;
75
- }
76
- else {
77
- this.setCurrentIndexCircularly(diff);
78
- }
79
- // To allow the `focusInIndex` to be calculated with the "after" state of the keyboard interaction
80
- // do `elementEnterAction` _before_ focusing the next element.
81
- this.elementEnterAction(this.elements[this.currentIndex]);
82
- this.focus();
83
- };
84
- this.host = host;
85
- this.host.addController(this);
86
- this._elements = elements;
87
- this.isFocusableElement = isFocusableElement || this.isFocusableElement;
88
- if (typeof direction === 'string') {
89
- this._direction = () => direction;
90
- }
91
- else if (typeof direction === 'function') {
92
- this._direction = direction;
93
- }
94
- this.elementEnterAction = elementEnterAction || this.elementEnterAction;
95
- if (typeof focusInIndex === 'number') {
96
- this._focusInIndex = () => focusInIndex;
97
- }
98
- else if (typeof focusInIndex === 'function') {
99
- this._focusInIndex = focusInIndex;
100
- }
101
- if (typeof listenerScope === 'object') {
102
- this._listenerScope = () => listenerScope;
103
- }
104
- else if (typeof listenerScope === 'function') {
105
- this._listenerScope = listenerScope;
106
- }
107
- }
108
- get currentIndex() {
109
- if (this._currentIndex === -1) {
110
- this._currentIndex = this.focusInIndex;
111
- }
112
- return this._currentIndex - this.offset;
113
- }
114
- set currentIndex(currentIndex) {
115
- this._currentIndex = currentIndex + this.offset;
116
- }
117
- get direction() {
118
- return this._direction();
119
- }
120
- get elements() {
121
- if (!this.cachedElements) {
122
- this.cachedElements = this._elements();
123
- }
124
- return this.cachedElements;
125
- }
126
- set focused(focused) {
127
- if (focused === this.focused)
128
- return;
129
- this._focused = focused;
130
- this.manageTabindexes();
131
- }
132
- get focused() {
133
- return this._focused;
134
- }
135
- get focusInElement() {
136
- return this.elements[this.focusInIndex];
137
- }
138
- get focusInIndex() {
139
- return this._focusInIndex(this.elements);
140
- }
141
- isEventWithinListenerScope(event) {
142
- if (this._listenerScope() === this.host)
143
- return true;
144
- return event.composedPath().includes(this._listenerScope());
145
- }
146
- update({ elements } = { elements: () => [] }) {
147
- this.unmanage();
148
- this._elements = elements;
149
- this.clearElementCache();
150
- this.manage();
151
- }
152
- focus(options) {
153
- var _a;
154
- (_a = this.elements[this.currentIndex]) === null || _a === void 0 ? void 0 : _a.focus(options);
155
- }
156
- clearElementCache(offset = 0) {
157
- delete this.cachedElements;
158
- cancelAnimationFrame(this.manageIndexesAnimationFrame);
159
- this.offset = offset;
160
- if (!this.managed)
161
- return;
162
- this.manageIndexesAnimationFrame = requestAnimationFrame(() => this.manageTabindexes());
163
- }
164
- setCurrentIndexCircularly(diff) {
165
- const { length } = this.elements;
166
- let steps = length;
167
- // start at a possibly not 0 index
168
- let nextIndex = (length + this.currentIndex + diff) % length;
169
- while (
170
- // don't cycle the elements more than once
171
- steps &&
172
- this.elements[nextIndex] &&
173
- !this.isFocusableElement(this.elements[nextIndex])) {
174
- nextIndex = (length + nextIndex + diff) % length;
175
- steps -= 1;
176
- }
177
- this.currentIndex = nextIndex;
178
- }
179
- hostContainsFocus() {
180
- this.host.addEventListener('focusout', this.handleFocusout);
181
- this.host.addEventListener('keydown', this.handleKeydown);
182
- this.focused = true;
183
- }
184
- hostNoLongerContainsFocus() {
185
- this.host.addEventListener('focusin', this.handleFocusin);
186
- this.host.removeEventListener('focusout', this.handleFocusout);
187
- this.host.removeEventListener('keydown', this.handleKeydown);
188
- this.currentIndex = this.focusInIndex;
189
- this.focused = false;
190
- }
191
- isRelatedTargetAnElement(event) {
192
- const relatedTarget = event.relatedTarget;
193
- return !this.elements.includes(relatedTarget);
194
- }
195
- acceptsEventCode(code) {
196
- if (code === 'End' || code === 'Home') {
197
- return true;
198
- }
199
- switch (this.direction) {
200
- case 'horizontal':
201
- return code === 'ArrowLeft' || code === 'ArrowRight';
202
- case 'vertical':
203
- return code === 'ArrowUp' || code === 'ArrowDown';
204
- case 'both':
205
- case 'grid':
206
- return code.startsWith('Arrow');
207
- }
208
- }
209
- manageTabindexes() {
210
- if (this.focused) {
211
- this.updateTabindexes(() => ({ tabIndex: -1 }));
212
- }
213
- else {
214
- this.updateTabindexes((el) => {
215
- return {
216
- removeTabIndex: el.contains(this.focusInElement) &&
217
- el !== this.focusInElement,
218
- tabIndex: el === this.focusInElement ? 0 : -1,
219
- };
220
- });
221
- }
222
- }
223
- updateTabindexes(getTabIndex) {
224
- this.elements.forEach((el) => {
225
- const { tabIndex, removeTabIndex } = getTabIndex(el);
226
- if (!removeTabIndex) {
227
- el.tabIndex = tabIndex;
228
- return;
229
- }
230
- el.removeAttribute('tabindex');
231
- const updatable = el;
232
- if (updatable.requestUpdate)
233
- updatable.requestUpdate();
234
- });
235
- }
236
- manage() {
237
- this.managed = true;
238
- this.manageTabindexes();
239
- this.addEventListeners();
240
- }
241
- unmanage() {
242
- this.managed = false;
243
- this.updateTabindexes(() => ({ tabIndex: 0 }));
244
- this.removeEventListeners();
245
- }
246
- addEventListeners() {
247
- this.host.addEventListener('focusin', this.handleFocusin);
248
- }
249
- removeEventListeners() {
250
- this.host.removeEventListener('focusin', this.handleFocusin);
251
- this.host.removeEventListener('focusout', this.handleFocusout);
252
- this.host.removeEventListener('keydown', this.handleKeydown);
253
- }
254
- hostUpdated() {
255
- if (this.firstUpdated) {
256
- this.manageTabindexes();
257
- this.firstUpdated = false;
258
- }
259
- }
260
- hostConnected() {
261
- this.addEventListeners();
262
- }
263
- hostDisconnected() {
264
- this.removeEventListeners();
265
- }
33
+ });
34
+ }
35
+ }
36
+ updateTabindexes(getTabIndex) {
37
+ this.elements.forEach((el) => {
38
+ const { tabIndex, removeTabIndex } = getTabIndex(el);
39
+ if (!removeTabIndex) {
40
+ el.tabIndex = tabIndex;
41
+ return;
42
+ }
43
+ el.removeAttribute("tabindex");
44
+ const updatable = el;
45
+ if (updatable.requestUpdate)
46
+ updatable.requestUpdate();
47
+ });
48
+ }
49
+ manage() {
50
+ this.managed = true;
51
+ this.manageTabindexes();
52
+ super.manage();
53
+ }
54
+ unmanage() {
55
+ this.managed = false;
56
+ this.updateTabindexes(() => ({ tabIndex: 0 }));
57
+ super.unmanage();
58
+ }
59
+ hostUpdated() {
60
+ if (!this.host.hasUpdated) {
61
+ this.manageTabindexes();
62
+ }
63
+ }
266
64
  }
267
- //# sourceMappingURL=RovingTabindex.js.map
65
+ //# sourceMappingURL=RovingTabindex.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"RovingTabindex.js","sourceRoot":"","sources":["RovingTabindex.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,wBAAwB;IAkFjC,YACI,IAAqB,EACrB,EACI,SAAS,EACT,kBAAkB,EAClB,QAAQ,EACR,YAAY,EACZ,kBAAkB,EAClB,aAAa,MACY,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE;QA3E/C,kBAAa,GAAG,CAAC,CAAC,CAAC;QAM3B,eAAU,GAAG,GAAmB,EAAE,CAAC,MAAM,CAAC;QAEnC,oBAAe,GAAG,CAAC,CAAC;QAE3B,6DAA6D;QAC7D,uBAAkB,GAAG,CAAC,GAAM,EAAQ,EAAE;YAClC,OAAO;QACX,CAAC,CAAC;QAWF,iBAAY,GAAG,IAAI,CAAC;QAWZ,aAAQ,GAAG,KAAK,CAAC;QAUzB,6DAA6D;QAC7D,kBAAa,GAAG,CAAC,SAAc,EAAU,EAAE,CAAC,CAAC,CAAC;QAI9C,6DAA6D;QAC7D,uBAAkB,GAAG,CAAC,GAAM,EAAW,EAAE,CAAC,IAAI,CAAC;QAO/C,mBAAc,GAAG,GAAgB,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;QAE9C,qEAAqE;QACrE,kCAAkC;QAClC,WAAM,GAAG,CAAC,CAAC;QAEH,YAAO,GAAG,IAAI,CAAC;QAgDf,gCAA2B,GAAG,CAAC,CAAC;QAiDxC,kBAAa,GAAG,CAAC,KAAiB,EAAQ,EAAE;YACxC,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,KAAK,CAAC;gBAAE,OAAO;YAC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C,YAAY,EAAE;YACnB,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACxB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;SAC7B;IACL,CAAC;IAED,aAAa;QACT,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAED,gBAAgB;QACZ,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAChC,CAAC;CACJ","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 type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type RovingTabindexConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\ninterface UpdateTabIndexes {\n tabIndex: number;\n removeTabIndex?: boolean;\n}\n\nexport class RovingTabindexController<T extends HTMLElement>\n implements ReactiveController\n{\n private cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n firstUpdated = true;\n\n private set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n this.manageTabindexes();\n }\n\n private get focused(): boolean {\n return this._focused;\n }\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n private managed = true;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: RovingTabindexConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n if (typeof direction === 'string') {\n this._direction = () => direction;\n } else if (typeof direction === 'function') {\n this._direction = direction;\n }\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n if (typeof focusInIndex === 'number') {\n this._focusInIndex = () => focusInIndex;\n } else if (typeof focusInIndex === 'function') {\n this._focusInIndex = focusInIndex;\n }\n if (typeof listenerScope === 'object') {\n this._listenerScope = () => listenerScope;\n } else if (typeof listenerScope === 'function') {\n this._listenerScope = listenerScope;\n }\n }\n\n update(\n { elements }: RovingTabindexConfig<T> = { elements: () => [] }\n ): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n this.elements[this.currentIndex]?.focus(options);\n }\n\n private manageIndexesAnimationFrame = 0;\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n cancelAnimationFrame(this.manageIndexesAnimationFrame);\n this.offset = offset;\n if (!this.managed) return;\n\n this.manageIndexesAnimationFrame = requestAnimationFrame(() =>\n this.manageTabindexes()\n );\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manageTabindexes(): void {\n if (this.focused) {\n this.updateTabindexes(() => ({ tabIndex: -1 }));\n } else {\n this.updateTabindexes((el: HTMLElement): UpdateTabIndexes => {\n return {\n removeTabIndex:\n el.contains(this.focusInElement) &&\n el !== this.focusInElement,\n tabIndex: el === this.focusInElement ? 0 : -1,\n };\n });\n }\n }\n\n updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void {\n this.elements.forEach((el) => {\n const { tabIndex, removeTabIndex } = getTabIndex(el);\n if (!removeTabIndex) {\n el.tabIndex = tabIndex;\n return;\n }\n el.removeAttribute('tabindex');\n const updatable = el as unknown as {\n requestUpdate?: () => void;\n };\n if (updatable.requestUpdate) updatable.requestUpdate();\n });\n }\n\n manage(): void {\n this.managed = true;\n this.manageTabindexes();\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.managed = false;\n this.updateTabindexes(() => ({ tabIndex: 0 }));\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostUpdated(): void {\n if (this.firstUpdated) {\n this.manageTabindexes();\n this.firstUpdated = false;\n }\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["RovingTabindex.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 { FocusGroupConfig, FocusGroupController } from './FocusGroup.js';\n\nexport type RovingTabindexConfig<T> = FocusGroupConfig<T>;\ninterface UpdateTabIndexes {\n tabIndex: number;\n removeTabIndex?: boolean;\n}\n\nexport class RovingTabindexController<\n T extends HTMLElement\n> extends FocusGroupController<T> {\n protected override set focused(focused: boolean) {\n if (focused === this.focused) return;\n super.focused = focused;\n this.manageTabindexes();\n }\n\n protected override get focused(): boolean {\n return super.focused;\n }\n\n private managed = true;\n\n private manageIndexesAnimationFrame = 0;\n\n override clearElementCache(offset = 0): void {\n cancelAnimationFrame(this.manageIndexesAnimationFrame);\n super.clearElementCache(offset);\n if (!this.managed) return;\n\n this.manageIndexesAnimationFrame = requestAnimationFrame(() =>\n this.manageTabindexes()\n );\n }\n\n manageTabindexes(): void {\n if (this.focused) {\n this.updateTabindexes(() => ({ tabIndex: -1 }));\n } else {\n this.updateTabindexes((el: HTMLElement): UpdateTabIndexes => {\n return {\n removeTabIndex:\n el.contains(this.focusInElement) &&\n el !== this.focusInElement,\n tabIndex: el === this.focusInElement ? 0 : -1,\n };\n });\n }\n }\n\n updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void {\n this.elements.forEach((el) => {\n const { tabIndex, removeTabIndex } = getTabIndex(el);\n if (!removeTabIndex) {\n el.tabIndex = tabIndex;\n return;\n }\n el.removeAttribute('tabindex');\n const updatable = el as unknown as {\n requestUpdate?: () => void;\n };\n if (updatable.requestUpdate) updatable.requestUpdate();\n });\n }\n\n override manage(): void {\n this.managed = true;\n this.manageTabindexes();\n super.manage();\n }\n\n override unmanage(): void {\n this.managed = false;\n this.updateTabindexes(() => ({ tabIndex: 0 }));\n super.unmanage();\n }\n\n hostUpdated(): void {\n if (!this.host.hasUpdated) {\n this.manageTabindexes();\n }\n }\n}\n"],
5
+ "mappings": "AAWA;AAQO,aAAM,iCAEH,qBAAwB;AAAA,EAF3B;AAAA;AAaK,mBAAU;AAEV,uCAA8B;AAAA;AAAA,MAZf,QAAQ,SAAkB;AAC7C,QAAI,YAAY,KAAK;AAAS;AAC9B,UAAM,UAAU;AAChB,SAAK,iBAAiB;AAAA,EAC1B;AAAA,MAEuB,UAAmB;AACtC,WAAO,MAAM;AAAA,EACjB;AAAA,EAMS,kBAAkB,SAAS,GAAS;AACzC,yBAAqB,KAAK,2BAA2B;AACrD,UAAM,kBAAkB,MAAM;AAC9B,QAAI,CAAC,KAAK;AAAS;AAEnB,SAAK,8BAA8B,sBAAsB,MACrD,KAAK,iBAAiB,CAC1B;AAAA,EACJ;AAAA,EAEA,mBAAyB;AACrB,QAAI,KAAK,SAAS;AACd,WAAK,iBAAiB,MAAO,GAAE,UAAU,GAAG,EAAE;AAAA,IAClD,OAAO;AACH,WAAK,iBAAiB,CAAC,OAAsC;AACzD,eAAO;AAAA,UACH,gBACI,GAAG,SAAS,KAAK,cAAc,KAC/B,OAAO,KAAK;AAAA,UAChB,UAAU,OAAO,KAAK,iBAAiB,IAAI;AAAA,QAC/C;AAAA,MACJ,CAAC;AAAA,IACL;AAAA,EACJ;AAAA,EAEA,iBAAiB,aAA0D;AACvE,SAAK,SAAS,QAAQ,CAAC,OAAO;AAC1B,YAAM,EAAE,UAAU,mBAAmB,YAAY,EAAE;AACnD,UAAI,CAAC,gBAAgB;AACjB,WAAG,WAAW;AACd;AAAA,MACJ;AACA,SAAG,gBAAgB,UAAU;AAC7B,YAAM,YAAY;AAGlB,UAAI,UAAU;AAAe,kBAAU,cAAc;AAAA,IACzD,CAAC;AAAA,EACL;AAAA,EAES,SAAe;AACpB,SAAK,UAAU;AACf,SAAK,iBAAiB;AACtB,UAAM,OAAO;AAAA,EACjB;AAAA,EAES,WAAiB;AACtB,SAAK,UAAU;AACf,SAAK,iBAAiB,MAAO,GAAE,UAAU,EAAE,EAAE;AAC7C,UAAM,SAAS;AAAA,EACnB;AAAA,EAEA,cAAoB;AAChB,QAAI,CAAC,KAAK,KAAK,YAAY;AACvB,WAAK,iBAAiB;AAAA,IAC1B;AAAA,EACJ;AACJ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./MatchMedia.dev.js";
2
+ export * from "./RovingTabindex.dev.js";
3
+ //# sourceMappingURL=index.dev.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["index.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\nexport * from './MatchMedia.dev.js'\nexport * from './RovingTabindex.dev.js'\n"],
5
+ "mappings": "AAYA;AACA;",
6
+ "names": []
7
+ }
package/src/index.js CHANGED
@@ -1,14 +1,3 @@
1
- /*
2
- Copyright 2020 Adobe. All rights reserved.
3
- This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License. You may obtain a copy
5
- of the License at http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- Unless required by applicable law or agreed to in writing, software distributed under
8
- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- OF ANY KIND, either express or implied. See the License for the specific language
10
- governing permissions and limitations under the License.
11
- */
12
- export * from './MatchMedia.js';
13
- export * from './RovingTabindex.js';
14
- //# sourceMappingURL=index.js.map
1
+ export * from "./MatchMedia.js";
2
+ export * from "./RovingTabindex.js";
3
+ //# sourceMappingURL=index.js.map