@internetarchive/modal-manager 2.0.4 → 2.0.5-webdev-8155.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.
Files changed (101) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +40 -30
  3. package/.github/workflows/gh-pages-main.yml +42 -42
  4. package/.github/workflows/pr-preview.yml +40 -40
  5. package/.prettierrc +4 -0
  6. package/.vscode/extensions.json +10 -0
  7. package/.vscode/tasks.json +12 -0
  8. package/LICENSE +661 -661
  9. package/README.md +139 -139
  10. package/custom-elements.json +170 -170
  11. package/demo/app-root.ts +366 -0
  12. package/dist/demo/app-root.d.ts +22 -0
  13. package/dist/demo/app-root.js +338 -0
  14. package/dist/demo/app-root.js.map +1 -0
  15. package/dist/index.d.ts +7 -7
  16. package/dist/index.js +5 -5
  17. package/dist/src/assets/arrow-left-icon.d.ts +2 -2
  18. package/dist/src/assets/arrow-left-icon.js +2 -2
  19. package/dist/src/assets/ia-logo-icon.d.ts +2 -2
  20. package/dist/src/assets/ia-logo-icon.js +2 -2
  21. package/dist/src/modal-config.d.ts +104 -104
  22. package/dist/src/modal-config.js +23 -24
  23. package/dist/src/modal-config.js.map +1 -1
  24. package/dist/src/modal-manager-host-bridge-interface.d.ts +12 -12
  25. package/dist/src/modal-manager-host-bridge-interface.js +1 -1
  26. package/dist/src/modal-manager-host-bridge.d.ts +34 -34
  27. package/dist/src/modal-manager-host-bridge.js +62 -62
  28. package/dist/src/modal-manager-host-bridge.js.map +1 -1
  29. package/dist/src/modal-manager-interface.d.ts +27 -27
  30. package/dist/src/modal-manager-interface.js +1 -1
  31. package/dist/src/modal-manager-mode.d.ts +10 -10
  32. package/dist/src/modal-manager-mode.js +11 -11
  33. package/dist/src/modal-manager.d.ts +137 -137
  34. package/dist/src/modal-manager.js +243 -249
  35. package/dist/src/modal-manager.js.map +1 -1
  36. package/dist/src/modal-template.d.ts +41 -41
  37. package/dist/src/modal-template.js +119 -118
  38. package/dist/src/modal-template.js.map +1 -1
  39. package/dist/src/shoelace/active-elements.d.ts +15 -15
  40. package/dist/src/shoelace/active-elements.js +28 -27
  41. package/dist/src/shoelace/active-elements.js.map +1 -1
  42. package/dist/src/shoelace/modal.d.ts +24 -24
  43. package/dist/src/shoelace/modal.js +130 -131
  44. package/dist/src/shoelace/modal.js.map +1 -1
  45. package/dist/src/shoelace/tabbable.d.ts +9 -9
  46. package/dist/src/shoelace/tabbable.js +168 -169
  47. package/dist/src/shoelace/tabbable.js.map +1 -1
  48. package/dist/test/modal-config.test.d.ts +1 -1
  49. package/dist/test/modal-config.test.js +68 -69
  50. package/dist/test/modal-config.test.js.map +1 -1
  51. package/dist/test/modal-manager.test.d.ts +1 -1
  52. package/dist/test/modal-manager.test.js +306 -309
  53. package/dist/test/modal-manager.test.js.map +1 -1
  54. package/dist/test/modal-template.test.d.ts +1 -1
  55. package/dist/test/modal-template.test.js +158 -167
  56. package/dist/test/modal-template.test.js.map +1 -1
  57. package/dist/vite.config.d.ts +2 -2
  58. package/dist/vite.config.js +22 -22
  59. package/dist/vitest.config.ci.d.ts +2 -0
  60. package/dist/vitest.config.ci.js +24 -0
  61. package/dist/vitest.config.ci.js.map +1 -0
  62. package/docs/assets/css/main.css +2678 -2678
  63. package/docs/classes/_src_modal_config_.modalconfig.html +429 -429
  64. package/docs/classes/_src_modal_manager_.modalmanager.html +7702 -7702
  65. package/docs/classes/_src_modal_manager_host_bridge_.modalmanagerhostbridge.html +409 -409
  66. package/docs/classes/_src_modal_template_.modaltemplate.html +7096 -7096
  67. package/docs/enums/_src_modal_manager_mode_.modalmanagermode.html +196 -196
  68. package/docs/globals.html +150 -150
  69. package/docs/index.html +252 -252
  70. package/docs/interfaces/_src_modal_manager_host_bridge_interface_.modalmanagerhostbridgeinterface.html +210 -210
  71. package/docs/interfaces/_src_modal_manager_interface_.modalmanagerinterface.html +7095 -7095
  72. package/docs/modules/_index_.html +208 -208
  73. package/docs/modules/_src_modal_config_.html +146 -146
  74. package/docs/modules/_src_modal_manager_.html +146 -146
  75. package/docs/modules/_src_modal_manager_host_bridge_.html +146 -146
  76. package/docs/modules/_src_modal_manager_host_bridge_interface_.html +146 -146
  77. package/docs/modules/_src_modal_manager_interface_.html +146 -146
  78. package/docs/modules/_src_modal_manager_mode_.html +146 -146
  79. package/docs/modules/_src_modal_template_.html +146 -146
  80. package/docs/modules/_test_modal_config_test_.html +106 -106
  81. package/docs/modules/_test_modal_manager_test_.html +106 -106
  82. package/docs/modules/_test_modal_template_test_.html +106 -106
  83. package/eslint.config.mjs +53 -0
  84. package/index.html +33 -300
  85. package/package.json +73 -85
  86. package/renovate.json +7 -7
  87. package/src/modal-config.ts +14 -14
  88. package/src/modal-manager-host-bridge.ts +2 -2
  89. package/src/modal-manager.ts +305 -305
  90. package/src/modal-template.ts +4 -2
  91. package/src/shoelace/LICENSE.md +6 -6
  92. package/src/shoelace/active-elements.ts +3 -2
  93. package/src/shoelace/modal.ts +5 -5
  94. package/src/shoelace/tabbable.ts +4 -3
  95. package/test/modal-config.test.ts +4 -4
  96. package/test/modal-manager.test.ts +402 -393
  97. package/test/modal-template.test.ts +42 -35
  98. package/tsconfig.json +25 -21
  99. package/vitest.config.ci.ts +27 -0
  100. package/.eslintrc.js +0 -14
  101. package/karma.conf.js +0 -24
@@ -1,132 +1,131 @@
1
- /* istanbul ignore file */
2
- import { activeElements, getDeepestActiveElement } from './active-elements.js';
3
- import { getTabbableElements } from './tabbable.js';
4
- let activeModals = [];
5
- export default class Modal {
6
- constructor(element) {
7
- this.isExternalActivated = false;
8
- this.tabDirection = 'forward';
9
- this.currentFocus = null;
10
- this.previousFocus = null;
11
- this.handleFocusIn = () => {
12
- if (!this.isActive())
13
- return;
14
- this.checkFocus();
15
- };
16
- this.handleKeyDown = (event) => {
17
- var _a;
18
- if (event.key !== 'Tab' || this.isExternalActivated)
19
- return;
20
- if (!this.isActive())
21
- return;
22
- // Because sometimes focus can actually be taken over from outside sources,
23
- // we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
24
- // recurse through shadowRoots.
25
- const currentActiveElement = getDeepestActiveElement();
26
- this.previousFocus = currentActiveElement;
27
- if (this.previousFocus &&
28
- this.possiblyHasTabbableChildren(this.previousFocus)) {
29
- return;
30
- }
31
- if (event.shiftKey) {
32
- this.tabDirection = 'backward';
33
- }
34
- else {
35
- this.tabDirection = 'forward';
36
- }
37
- const tabbableElements = getTabbableElements(this.element);
38
- let currentFocusIndex = tabbableElements.findIndex(el => el === currentActiveElement);
39
- this.previousFocus = this.currentFocus;
40
- const addition = this.tabDirection === 'forward' ? 1 : -1;
41
- // eslint-disable-next-line
42
- while (true) {
43
- if (currentFocusIndex + addition >= tabbableElements.length) {
44
- currentFocusIndex = 0;
45
- }
46
- else if (currentFocusIndex + addition < 0) {
47
- currentFocusIndex = tabbableElements.length - 1;
48
- }
49
- else {
50
- currentFocusIndex += addition;
51
- }
52
- this.previousFocus = this.currentFocus;
53
- const nextFocus =
54
- /** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
55
- // This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
56
- // that possibly has "controls"
57
- if (this.tabDirection === 'backward') {
58
- if (this.previousFocus &&
59
- this.possiblyHasTabbableChildren(this.previousFocus)) {
60
- return;
61
- }
62
- }
63
- if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
64
- return;
65
- }
66
- event.preventDefault();
67
- this.currentFocus = nextFocus;
68
- (_a = this.currentFocus) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: false });
69
- // Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.
70
- const allActiveElements = [...activeElements()];
71
- if (allActiveElements.includes(this.currentFocus) ||
72
- !allActiveElements.includes(this.previousFocus)) {
73
- break;
74
- }
75
- }
76
- setTimeout(() => this.checkFocus());
77
- };
78
- this.handleKeyUp = () => {
79
- this.tabDirection = 'forward';
80
- };
81
- this.element = element;
82
- this.elementsWithTabbableControls = ['iframe'];
83
- }
84
- /** Activates focus trapping. */
85
- activate() {
86
- activeModals.push(this.element);
87
- document.addEventListener('focusin', this.handleFocusIn);
88
- document.addEventListener('keydown', this.handleKeyDown);
89
- document.addEventListener('keyup', this.handleKeyUp);
90
- }
91
- /** Deactivates focus trapping. */
92
- deactivate() {
93
- activeModals = activeModals.filter(modal => modal !== this.element);
94
- this.currentFocus = null;
95
- document.removeEventListener('focusin', this.handleFocusIn);
96
- document.removeEventListener('keydown', this.handleKeyDown);
97
- document.removeEventListener('keyup', this.handleKeyUp);
98
- }
99
- /** Determines if this modal element is currently active or not. */
100
- isActive() {
101
- // The "active" modal is always the most recent one shown
102
- return activeModals[activeModals.length - 1] === this.element;
103
- }
104
- /** Activates external modal behavior and temporarily disables focus trapping. */
105
- activateExternal() {
106
- this.isExternalActivated = true;
107
- }
108
- /** Deactivates external modal behavior and re-enables focus trapping. */
109
- deactivateExternal() {
110
- this.isExternalActivated = false;
111
- }
112
- checkFocus() {
113
- if (this.isActive() && !this.isExternalActivated) {
114
- const tabbableElements = getTabbableElements(this.element);
115
- if (!this.element.matches(':focus-within')) {
116
- const start = tabbableElements[0];
117
- const end = tabbableElements[tabbableElements.length - 1];
118
- const target = this.tabDirection === 'forward' ? start : end;
119
- if (typeof (target === null || target === void 0 ? void 0 : target.focus) === 'function') {
120
- this.currentFocus = target;
121
- target.focus({ preventScroll: false });
122
- }
123
- }
124
- }
125
- }
126
- possiblyHasTabbableChildren(element) {
127
- return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
128
- // Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?
129
- );
130
- }
131
- }
1
+ /* c8 ignore start */
2
+ import { activeElements, getDeepestActiveElement } from './active-elements.js';
3
+ import { getTabbableElements } from './tabbable.js';
4
+ let activeModals = [];
5
+ export default class Modal {
6
+ constructor(element) {
7
+ this.isExternalActivated = false;
8
+ this.tabDirection = 'forward';
9
+ this.currentFocus = null;
10
+ this.previousFocus = null;
11
+ this.handleFocusIn = () => {
12
+ if (!this.isActive())
13
+ return;
14
+ this.checkFocus();
15
+ };
16
+ this.handleKeyDown = (event) => {
17
+ if (event.key !== 'Tab' || this.isExternalActivated)
18
+ return;
19
+ if (!this.isActive())
20
+ return;
21
+ // Because sometimes focus can actually be taken over from outside sources,
22
+ // we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
23
+ // recurse through shadowRoots.
24
+ const currentActiveElement = getDeepestActiveElement();
25
+ this.previousFocus = currentActiveElement;
26
+ if (this.previousFocus &&
27
+ this.possiblyHasTabbableChildren(this.previousFocus)) {
28
+ return;
29
+ }
30
+ if (event.shiftKey) {
31
+ this.tabDirection = 'backward';
32
+ }
33
+ else {
34
+ this.tabDirection = 'forward';
35
+ }
36
+ const tabbableElements = getTabbableElements(this.element);
37
+ let currentFocusIndex = tabbableElements.findIndex((el) => el === currentActiveElement);
38
+ this.previousFocus = this.currentFocus;
39
+ const addition = this.tabDirection === 'forward' ? 1 : -1;
40
+ while (true) {
41
+ if (currentFocusIndex + addition >= tabbableElements.length) {
42
+ currentFocusIndex = 0;
43
+ }
44
+ else if (currentFocusIndex + addition < 0) {
45
+ currentFocusIndex = tabbableElements.length - 1;
46
+ }
47
+ else {
48
+ currentFocusIndex += addition;
49
+ }
50
+ this.previousFocus = this.currentFocus;
51
+ const nextFocus =
52
+ /** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
53
+ // This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
54
+ // that possibly has "controls"
55
+ if (this.tabDirection === 'backward') {
56
+ if (this.previousFocus &&
57
+ this.possiblyHasTabbableChildren(this.previousFocus)) {
58
+ return;
59
+ }
60
+ }
61
+ if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
62
+ return;
63
+ }
64
+ event.preventDefault();
65
+ this.currentFocus = nextFocus;
66
+ this.currentFocus?.focus({ preventScroll: false });
67
+ // Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.
68
+ const allActiveElements = [...activeElements()];
69
+ if (allActiveElements.includes(this.currentFocus) ||
70
+ !allActiveElements.includes(this.previousFocus)) {
71
+ break;
72
+ }
73
+ }
74
+ setTimeout(() => this.checkFocus());
75
+ };
76
+ this.handleKeyUp = () => {
77
+ this.tabDirection = 'forward';
78
+ };
79
+ this.element = element;
80
+ this.elementsWithTabbableControls = ['iframe'];
81
+ }
82
+ /** Activates focus trapping. */
83
+ activate() {
84
+ activeModals.push(this.element);
85
+ document.addEventListener('focusin', this.handleFocusIn);
86
+ document.addEventListener('keydown', this.handleKeyDown);
87
+ document.addEventListener('keyup', this.handleKeyUp);
88
+ }
89
+ /** Deactivates focus trapping. */
90
+ deactivate() {
91
+ activeModals = activeModals.filter((modal) => modal !== this.element);
92
+ this.currentFocus = null;
93
+ document.removeEventListener('focusin', this.handleFocusIn);
94
+ document.removeEventListener('keydown', this.handleKeyDown);
95
+ document.removeEventListener('keyup', this.handleKeyUp);
96
+ }
97
+ /** Determines if this modal element is currently active or not. */
98
+ isActive() {
99
+ // The "active" modal is always the most recent one shown
100
+ return activeModals[activeModals.length - 1] === this.element;
101
+ }
102
+ /** Activates external modal behavior and temporarily disables focus trapping. */
103
+ activateExternal() {
104
+ this.isExternalActivated = true;
105
+ }
106
+ /** Deactivates external modal behavior and re-enables focus trapping. */
107
+ deactivateExternal() {
108
+ this.isExternalActivated = false;
109
+ }
110
+ checkFocus() {
111
+ if (this.isActive() && !this.isExternalActivated) {
112
+ const tabbableElements = getTabbableElements(this.element);
113
+ if (!this.element.matches(':focus-within')) {
114
+ const start = tabbableElements[0];
115
+ const end = tabbableElements[tabbableElements.length - 1];
116
+ const target = this.tabDirection === 'forward' ? start : end;
117
+ if (typeof target?.focus === 'function') {
118
+ this.currentFocus = target;
119
+ target.focus({ preventScroll: false });
120
+ }
121
+ }
122
+ }
123
+ }
124
+ possiblyHasTabbableChildren(element) {
125
+ return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
126
+ // Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?
127
+ );
128
+ }
129
+ }
130
+ /* c8 ignore end */
132
131
  //# sourceMappingURL=modal.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"modal.js","sourceRoot":"","sources":["../../../src/shoelace/modal.ts"],"names":[],"mappings":"AAAA,0BAA0B;AAC1B,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,IAAI,YAAY,GAAkB,EAAE,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,KAAK;IAQxB,YAAY,OAAoB;QANhC,wBAAmB,GAAY,KAAK,CAAC;QACrC,iBAAY,GAA2B,SAAS,CAAC;QACjD,iBAAY,GAAuB,IAAI,CAAC;QACxC,kBAAa,GAAuB,IAAI,CAAC;QA0DjC,kBAAa,GAAG,GAAG,EAAE;YAC3B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAC7B,IAAI,CAAC,UAAU,EAAE,CAAC;QACpB,CAAC,CAAC;QAWM,kBAAa,GAAG,CAAC,KAAoB,EAAE,EAAE;;YAC/C,IAAI,KAAK,CAAC,GAAG,KAAK,KAAK,IAAI,IAAI,CAAC,mBAAmB;gBAAE,OAAO;YAC5D,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAE7B,2EAA2E;YAC3E,gGAAgG;YAChG,+BAA+B;YAC/B,MAAM,oBAAoB,GAAG,uBAAuB,EAAE,CAAC;YACvD,IAAI,CAAC,aAAa,GAAG,oBAA0C,CAAC;YAEhE,IACE,IAAI,CAAC,aAAa;gBAClB,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,aAAa,CAAC,EACpD;gBACA,OAAO;aACR;YAED,IAAI,KAAK,CAAC,QAAQ,EAAE;gBAClB,IAAI,CAAC,YAAY,GAAG,UAAU,CAA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BAA4B,CAAC,QAAQ,CACxC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAC9B,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC;QACrC,+IAA+I;SAChJ,CAAC;IACJ,CAAC;CAoFF","sourcesContent":["/* istanbul ignore file */\nimport { activeElements, getDeepestActiveElement } from './active-elements.js';\nimport { getTabbableElements } from './tabbable.js';\n\nlet activeModals: HTMLElement[] = [];\n\nexport default class Modal {\n element: HTMLElement;\n isExternalActivated: boolean = false;\n tabDirection: 'forward' | 'backward' = 'forward';\n currentFocus: HTMLElement | null = null;\n previousFocus: HTMLElement | null = null;\n elementsWithTabbableControls: string[];\n\n constructor(element: HTMLElement) {\n this.element = element;\n\n this.elementsWithTabbableControls = ['iframe'];\n }\n\n /** Activates focus trapping. */\n activate() {\n activeModals.push(this.element);\n document.addEventListener('focusin', this.handleFocusIn);\n document.addEventListener('keydown', this.handleKeyDown);\n document.addEventListener('keyup', this.handleKeyUp);\n }\n\n /** Deactivates focus trapping. */\n deactivate() {\n activeModals = activeModals.filter(modal => modal !== this.element);\n this.currentFocus = null;\n document.removeEventListener('focusin', this.handleFocusIn);\n document.removeEventListener('keydown', this.handleKeyDown);\n document.removeEventListener('keyup', this.handleKeyUp);\n }\n\n /** Determines if this modal element is currently active or not. */\n isActive() {\n // The \"active\" modal is always the most recent one shown\n return activeModals[activeModals.length - 1] === this.element;\n }\n\n /** Activates external modal behavior and temporarily disables focus trapping. */\n activateExternal() {\n this.isExternalActivated = true;\n }\n\n /** Deactivates external modal behavior and re-enables focus trapping. */\n deactivateExternal() {\n this.isExternalActivated = false;\n }\n\n private checkFocus() {\n if (this.isActive() && !this.isExternalActivated) {\n const tabbableElements = getTabbableElements(this.element);\n if (!this.element.matches(':focus-within')) {\n const start = tabbableElements[0];\n const end = tabbableElements[tabbableElements.length - 1];\n const target = this.tabDirection === 'forward' ? start : end;\n\n if (typeof target?.focus === 'function') {\n this.currentFocus = target;\n target.focus({ preventScroll: false });\n }\n }\n }\n }\n\n private handleFocusIn = () => {\n if (!this.isActive()) return;\n this.checkFocus();\n };\n\n private possiblyHasTabbableChildren(element: HTMLElement) {\n return (\n this.elementsWithTabbableControls.includes(\n element.tagName.toLowerCase()\n ) || element.hasAttribute('controls')\n // Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?\n );\n }\n\n private handleKeyDown = (event: KeyboardEvent) => {\n if (event.key !== 'Tab' || this.isExternalActivated) return;\n if (!this.isActive()) return;\n\n // Because sometimes focus can actually be taken over from outside sources,\n // we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and\n // recurse through shadowRoots.\n const currentActiveElement = getDeepestActiveElement();\n this.previousFocus = currentActiveElement as HTMLElement | null;\n\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n\n if (event.shiftKey) {\n this.tabDirection = 'backward';\n } else {\n this.tabDirection = 'forward';\n }\n\n const tabbableElements = getTabbableElements(this.element);\n\n let currentFocusIndex = tabbableElements.findIndex(\n el => el === currentActiveElement\n );\n\n this.previousFocus = this.currentFocus;\n\n const addition = this.tabDirection === 'forward' ? 1 : -1;\n\n // eslint-disable-next-line\n while (true) {\n if (currentFocusIndex + addition >= tabbableElements.length) {\n currentFocusIndex = 0;\n } else if (currentFocusIndex + addition < 0) {\n currentFocusIndex = tabbableElements.length - 1;\n } else {\n currentFocusIndex += addition;\n }\n\n this.previousFocus = this.currentFocus;\n const nextFocus =\n /** @type {HTMLElement} */ tabbableElements[currentFocusIndex];\n\n // This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element\n // that possibly has \"controls\"\n if (this.tabDirection === 'backward') {\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n }\n\n if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {\n return;\n }\n\n event.preventDefault();\n this.currentFocus = nextFocus;\n this.currentFocus?.focus({ preventScroll: false });\n\n // Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.\n const allActiveElements = [...activeElements()];\n if (\n allActiveElements.includes(this.currentFocus) ||\n !allActiveElements.includes(this.previousFocus!)\n ) {\n break;\n }\n }\n\n setTimeout(() => this.checkFocus());\n };\n\n private handleKeyUp = () => {\n this.tabDirection = 'forward';\n };\n}\n"]}
1
+ {"version":3,"file":"modal.js","sourceRoot":"","sources":["../../../src/shoelace/modal.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,IAAI,YAAY,GAAkB,EAAE,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,KAAK;IAQxB,YAAY,OAAoB;QANhC,wBAAmB,GAAY,KAAK,CAAC;QACrC,iBAAY,GAA2B,SAAS,CAAC;QACjD,iBAAY,GAAuB,IAAI,CAAC;QACxC,kBAAa,GAAuB,IAAI,CAAC;QA0DjC,kBAAa,GAAG,GAAG,EAAE;YAC3B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAC7B,IAAI,CAAC,UAAU,EAAE,CAAC;QACpB,CAAC,CAAC;QAWM,kBAAa,GAAG,CAAC,KAAoB,EAAE,EAAE;YAC/C,IAAI,KAAK,CAAC,GAAG,KAAK,KAAK,IAAI,IAAI,CAAC,mBAAmB;gBAAE,OAAO;YAC5D,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAE7B,2EAA2E;YAC3E,gGAAgG;YAChG,+BAA+B;YAC/B,MAAM,oBAAoB,GAAG,uBAAuB,EAAE,CAAC;YACvD,IAAI,CAAC,aAAa,GAAG,oBAA0C,CAAC;YAEhE,IACE,IAAI,CAAC,aAAa;gBAClB,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,aAAa,CAAC,EACpD,CAAC;gBACD,OAAO;YACT,C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AAM,CAAC;oBAC3B,MAAM,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;gBACzC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAOO,2BAA2B,CAAC,OAAoB;QACtD,OAAO,CACL,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CACxC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAC9B,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC;QACrC,+IAA+I;SAChJ,CAAC;IACJ,CAAC;CAmFF;AACD,mBAAmB","sourcesContent":["/* c8 ignore start */\nimport { activeElements, getDeepestActiveElement } from './active-elements.js';\nimport { getTabbableElements } from './tabbable.js';\n\nlet activeModals: HTMLElement[] = [];\n\nexport default class Modal {\n element: HTMLElement;\n isExternalActivated: boolean = false;\n tabDirection: 'forward' | 'backward' = 'forward';\n currentFocus: HTMLElement | null = null;\n previousFocus: HTMLElement | null = null;\n elementsWithTabbableControls: string[];\n\n constructor(element: HTMLElement) {\n this.element = element;\n\n this.elementsWithTabbableControls = ['iframe'];\n }\n\n /** Activates focus trapping. */\n activate() {\n activeModals.push(this.element);\n document.addEventListener('focusin', this.handleFocusIn);\n document.addEventListener('keydown', this.handleKeyDown);\n document.addEventListener('keyup', this.handleKeyUp);\n }\n\n /** Deactivates focus trapping. */\n deactivate() {\n activeModals = activeModals.filter((modal) => modal !== this.element);\n this.currentFocus = null;\n document.removeEventListener('focusin', this.handleFocusIn);\n document.removeEventListener('keydown', this.handleKeyDown);\n document.removeEventListener('keyup', this.handleKeyUp);\n }\n\n /** Determines if this modal element is currently active or not. */\n isActive() {\n // The \"active\" modal is always the most recent one shown\n return activeModals[activeModals.length - 1] === this.element;\n }\n\n /** Activates external modal behavior and temporarily disables focus trapping. */\n activateExternal() {\n this.isExternalActivated = true;\n }\n\n /** Deactivates external modal behavior and re-enables focus trapping. */\n deactivateExternal() {\n this.isExternalActivated = false;\n }\n\n private checkFocus() {\n if (this.isActive() && !this.isExternalActivated) {\n const tabbableElements = getTabbableElements(this.element);\n if (!this.element.matches(':focus-within')) {\n const start = tabbableElements[0];\n const end = tabbableElements[tabbableElements.length - 1];\n const target = this.tabDirection === 'forward' ? start : end;\n\n if (typeof target?.focus === 'function') {\n this.currentFocus = target;\n target.focus({ preventScroll: false });\n }\n }\n }\n }\n\n private handleFocusIn = () => {\n if (!this.isActive()) return;\n this.checkFocus();\n };\n\n private possiblyHasTabbableChildren(element: HTMLElement) {\n return (\n this.elementsWithTabbableControls.includes(\n element.tagName.toLowerCase(),\n ) || element.hasAttribute('controls')\n // Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?\n );\n }\n\n private handleKeyDown = (event: KeyboardEvent) => {\n if (event.key !== 'Tab' || this.isExternalActivated) return;\n if (!this.isActive()) return;\n\n // Because sometimes focus can actually be taken over from outside sources,\n // we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and\n // recurse through shadowRoots.\n const currentActiveElement = getDeepestActiveElement();\n this.previousFocus = currentActiveElement as HTMLElement | null;\n\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n\n if (event.shiftKey) {\n this.tabDirection = 'backward';\n } else {\n this.tabDirection = 'forward';\n }\n\n const tabbableElements = getTabbableElements(this.element);\n\n let currentFocusIndex = tabbableElements.findIndex(\n (el) => el === currentActiveElement,\n );\n\n this.previousFocus = this.currentFocus;\n\n const addition = this.tabDirection === 'forward' ? 1 : -1;\n\n while (true) {\n if (currentFocusIndex + addition >= tabbableElements.length) {\n currentFocusIndex = 0;\n } else if (currentFocusIndex + addition < 0) {\n currentFocusIndex = tabbableElements.length - 1;\n } else {\n currentFocusIndex += addition;\n }\n\n this.previousFocus = this.currentFocus;\n const nextFocus =\n /** @type {HTMLElement} */ tabbableElements[currentFocusIndex];\n\n // This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element\n // that possibly has \"controls\"\n if (this.tabDirection === 'backward') {\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n }\n\n if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {\n return;\n }\n\n event.preventDefault();\n this.currentFocus = nextFocus;\n this.currentFocus?.focus({ preventScroll: false });\n\n // Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.\n const allActiveElements = [...activeElements()];\n if (\n allActiveElements.includes(this.currentFocus) ||\n !allActiveElements.includes(this.previousFocus!)\n ) {\n break;\n }\n }\n\n setTimeout(() => this.checkFocus());\n };\n\n private handleKeyUp = () => {\n this.tabDirection = 'forward';\n };\n}\n/* c8 ignore end */\n"]}
@@ -1,9 +1,9 @@
1
- /**
2
- * Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
3
- * element because it short-circuits after finding the first and last ones.
4
- */
5
- export declare function getTabbableBoundary(root: HTMLElement | ShadowRoot): {
6
- start: HTMLElement;
7
- end: HTMLElement;
8
- };
9
- export declare function getTabbableElements(root: HTMLElement | ShadowRoot): HTMLElement[];
1
+ /**
2
+ * Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
3
+ * element because it short-circuits after finding the first and last ones.
4
+ */
5
+ export declare function getTabbableBoundary(root: HTMLElement | ShadowRoot): {
6
+ start: HTMLElement;
7
+ end: HTMLElement;
8
+ };
9
+ export declare function getTabbableElements(root: HTMLElement | ShadowRoot): HTMLElement[];