@internetarchive/modal-manager 0.3.4 → 0.9.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/modal-manager.d.ts +5 -1
- package/dist/src/modal-manager.js +24 -3
- package/dist/src/modal-manager.js.map +1 -1
- package/dist/src/shoelace/active-elements.d.ts +15 -0
- package/dist/src/shoelace/active-elements.js +27 -0
- package/dist/src/shoelace/active-elements.js.map +1 -0
- package/dist/src/shoelace/modal.d.ts +24 -0
- package/dist/src/shoelace/modal.js +131 -0
- package/dist/src/shoelace/modal.js.map +1 -0
- package/dist/src/shoelace/tabbable.d.ts +9 -0
- package/dist/src/shoelace/tabbable.js +168 -0
- package/dist/src/shoelace/tabbable.js.map +1 -0
- package/dist/test/modal-manager.test.js +2 -2
- package/dist/test/modal-manager.test.js.map +1 -1
- package/dist/test/modal-template.test.js +1 -1
- package/dist/test/modal-template.test.js.map +1 -1
- package/index.html +1 -1
- package/package.json +11 -12
- package/src/modal-manager.ts +30 -3
- package/src/shoelace/active-elements.ts +32 -0
- package/src/shoelace/modal.ts +165 -0
- package/src/shoelace/tabbable.ts +221 -0
- package/test/modal-manager.test.ts +3 -3
- package/test/modal-template.test.ts +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LitElement, CSSResult, TemplateResult, PropertyValues } from 'lit';
|
|
2
|
+
import Modal from './shoelace/modal';
|
|
2
3
|
import './modal-template';
|
|
3
4
|
import { ModalConfig } from './modal-config';
|
|
4
5
|
import { ModalManagerInterface } from './modal-manager-interface';
|
|
@@ -22,7 +23,7 @@ export declare class ModalManager extends LitElement implements ModalManagerInte
|
|
|
22
23
|
*/
|
|
23
24
|
customModalContent?: TemplateResult;
|
|
24
25
|
/**
|
|
25
|
-
*
|
|
26
|
+
* This hostBridge handles environmental-specific interactions such as adding classes
|
|
26
27
|
* to the body tag or event listeners needed to support the modal manager in the host environment.
|
|
27
28
|
*
|
|
28
29
|
* There is a default `ModalManagerHostBridge`, but consumers can override it with a custom
|
|
@@ -40,6 +41,9 @@ export declare class ModalManager extends LitElement implements ModalManagerInte
|
|
|
40
41
|
* @memberof ModalManager
|
|
41
42
|
*/
|
|
42
43
|
private modalTemplate;
|
|
44
|
+
modal: Modal;
|
|
45
|
+
firstUpdated(): Promise<void>;
|
|
46
|
+
disconnectedCallback(): void;
|
|
43
47
|
/** @inheritdoc */
|
|
44
48
|
render(): TemplateResult;
|
|
45
49
|
/** @inheritdoc */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __awaiter, __decorate } from "tslib";
|
|
2
2
|
import { LitElement, html, css, } from 'lit';
|
|
3
3
|
import { property, customElement, query } from 'lit/decorators.js';
|
|
4
|
+
import Modal from './shoelace/modal';
|
|
4
5
|
import './modal-template';
|
|
5
6
|
import { ModalConfig } from './modal-config';
|
|
6
7
|
import { ModalManagerHostBridge } from './modal-manager-host-bridge';
|
|
@@ -18,7 +19,7 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
18
19
|
*/
|
|
19
20
|
this.mode = ModalManagerMode.Closed;
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
+
* This hostBridge handles environmental-specific interactions such as adding classes
|
|
22
23
|
* to the body tag or event listeners needed to support the modal manager in the host environment.
|
|
23
24
|
*
|
|
24
25
|
* There is a default `ModalManagerHostBridge`, but consumers can override it with a custom
|
|
@@ -28,6 +29,8 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
28
29
|
* @memberof ModalManager
|
|
29
30
|
*/
|
|
30
31
|
this.hostBridge = new ModalManagerHostBridge(this);
|
|
32
|
+
// Imported tab handling from shoelace
|
|
33
|
+
this.modal = new Modal(this);
|
|
31
34
|
/**
|
|
32
35
|
* Whether the modal should close if the user taps on the backdrop
|
|
33
36
|
*
|
|
@@ -36,6 +39,17 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
36
39
|
*/
|
|
37
40
|
this.closeOnBackdropClick = true;
|
|
38
41
|
}
|
|
42
|
+
firstUpdated() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
// Give the browser a chance to paint
|
|
45
|
+
// eslint-disable-next-line no-promise-executor-return
|
|
46
|
+
yield new Promise(r => setTimeout(r, 0));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
disconnectedCallback() {
|
|
50
|
+
super.disconnectedCallback();
|
|
51
|
+
this.modal.deactivate();
|
|
52
|
+
}
|
|
39
53
|
/** @inheritdoc */
|
|
40
54
|
render() {
|
|
41
55
|
return html `
|
|
@@ -43,7 +57,7 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
43
57
|
<div class="backdrop" @click=${this.backdropClicked}></div>
|
|
44
58
|
<modal-template
|
|
45
59
|
@closeButtonPressed=${this.closeButtonPressed}
|
|
46
|
-
tabindex="
|
|
60
|
+
tabindex="-1"
|
|
47
61
|
>
|
|
48
62
|
${this.customModalContent}
|
|
49
63
|
</modal-template>
|
|
@@ -59,6 +73,7 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
59
73
|
this.mode = ModalManagerMode.Closed;
|
|
60
74
|
this.customModalContent = undefined;
|
|
61
75
|
this.modalTemplate.config = new ModalConfig();
|
|
76
|
+
this.modal.deactivate();
|
|
62
77
|
}
|
|
63
78
|
/**
|
|
64
79
|
* Call the userClosedModalCallback and reset it if it exists
|
|
@@ -84,7 +99,13 @@ let ModalManager = class ModalManager extends LitElement {
|
|
|
84
99
|
this.customModalContent = options.customModalContent;
|
|
85
100
|
this.mode = ModalManagerMode.Open;
|
|
86
101
|
yield this.modalTemplate.updateComplete;
|
|
87
|
-
this.
|
|
102
|
+
this.modal.activate();
|
|
103
|
+
// document.addEventListener('keydown', this.tabHandler);
|
|
104
|
+
// this.firstFocusableElement.focus();
|
|
105
|
+
// console.log('firstFocusableElement', this.firstFocusableElement);
|
|
106
|
+
// console.log('lastFocusableElement', this.lastFocusableElement);
|
|
107
|
+
// console.log('focusableContent', this.focusableContent);
|
|
108
|
+
// this.modalTemplate.focus();
|
|
88
109
|
});
|
|
89
110
|
}
|
|
90
111
|
/** @inheritdoc */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modal-manager.js","sourceRoot":"","sources":["../../src/modal-manager.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,UAAU,EACV,IAAI,EACJ,GAAG,GAIJ,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEnE,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAGrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGjD,IAAM,YAAY,GAAlB,MAAM,YAAa,SAAQ,UAAU;IAArC;;QACL;;;;;;;WAOG;QACwC,SAAI,GAC7C,gBAAgB,CAAC,MAAM,CAAC;QAU1B;;;;;;;;;WASG;QAEH,eAAU,GAAoC,IAAI,sBAAsB,CACtE,IAAI,CACL,CAAC;QAsCF;;;;;WAKG;QACK,yBAAoB,GAAG,IAAI,CAAC;IAqItC,CAAC;IAtKC,kBAAkB;IAClB,MAAM;QACJ,OAAO,IAAI,CAAA;;uCAEwB,IAAI,CAAC,eAAe;;gCAE3B,IAAI,CAAC,kBAAkB;;;YAG3C,IAAI,CAAC,kBAAkB;;;KAG9B,CAAC;IACJ,CAAC;IAED,kBAAkB;IAClB,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,kBAAkB;IAClB,UAAU;QACR,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC,MAAM,CAAC;QACpC,IAAI,CAAC,kBAAkB,GAAG,SAAS,CAAC;QACpC,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;IAChD,CAAC;IAkBD;;;;;O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,aAAa,EAAE;YAC3C,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;SAC5B,CAAC,CAAC;QACH,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACK,kBAAkB;QACxB,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,2BAA2B,EAAE,CAAC;IACrC,CAAC;IAED,kBAAkB;IAClB,MAAM,KAAK,MAAM;QACf,MAAM,kBAAkB,GAAG,GAAG,CAAA,kDAAkD,CAAC;QACjF,MAAM,mBAAmB,GAAG,GAAG,CAAA,kCAAkC,CAAC;QAElE,MAAM,UAAU,GAAG,GAAG,CAAA,0BAA0B,CAAC;QACjD,MAAM,aAAa,GAAG,GAAG,CAAA,2BAA2B,CAAC;QACrD,MAAM,WAAW,GAAG,GAAG,CAAA,0BAA0B,CAAC;QAElD,OAAO,GAAG,CAAA;;;;;;;;;;4BAUc,kBAAkB;;;mBAG3B,mBAAmB;;;;;;;;;mBASnB,WAAW;iBACb,UAAU;qBACN,aAAa;;KAE7B,CAAC;IACJ,CAAC;CACF,CAAA;AAzM4C;IAA1C,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;0CAChB;AAQE;IAA3B,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;wDAAqC;AAahE;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;gDAGzB;AASuB;IAAxB,KAAK,CAAC,gBAAgB,CAAC;mDAAuC;AA1CpD,YAAY;IADxB,aAAa,CAAC,eAAe,CAAC;GAClB,YAAY,CAkNxB;SAlNY,YAAY","sourcesContent":["import {\n LitElement,\n html,\n css,\n CSSResult,\n TemplateResult,\n PropertyValues,\n} from 'lit';\nimport { property, customElement, query } from 'lit/decorators.js';\n\nimport './modal-template';\nimport { ModalTemplate } from './modal-template';\nimport { ModalConfig } from './modal-config';\nimport { ModalManagerHostBridge } from './modal-manager-host-bridge';\nimport { ModalManagerInterface } from './modal-manager-interface';\nimport { ModalManagerHostBridgeInterface } from './modal-manager-host-bridge-interface';\nimport { ModalManagerMode } from './modal-manager-mode';\n\n@customElement('modal-manager')\nexport class ModalManager extends LitElement implements ModalManagerInterface {\n /**\n * The current mode of the ModalManager\n *\n * Current options are `modal` or `closed`\n *\n * @type {ModalManagerMode}\n * @memberof ModalManager\n */\n @property({ type: String, reflect: true }) mode: ModalManagerMode =\n ModalManagerMode.Closed;\n\n /**\n * Custom content to display in the modal's content slot\n *\n * @type {(TemplateResult | undefined)}\n * @memberof ModalManager\n */\n @property({ type: Object }) customModalContent?: TemplateResult;\n\n /**\n * Thie hostBridge handles environmental-specific interactions such as adding classes\n * to the body tag or event listeners needed to support the modal manager in the host environment.\n *\n * There is a default `ModalManagerHostBridge`, but consumers can override it with a custom\n * `ModalManagerHostBridgeInterface`\n *\n * @type {ModalManagerHostBridgeInterface}\n * @memberof ModalManager\n */\n @property({ type: Object })\n hostBridge: ModalManagerHostBridgeInterface = new ModalManagerHostBridge(\n this\n );\n\n /**\n * Reference to the ModalTemplate DOM element\n *\n * @private\n * @type {ModalTemplate}\n * @memberof ModalManager\n */\n @query('modal-template') private modalTemplate!: ModalTemplate;\n\n /** @inheritdoc */\n render(): TemplateResult {\n return html`\n <div class=\"container\">\n <div class=\"backdrop\" @click=${this.backdropClicked}></div>\n <modal-template\n @closeButtonPressed=${this.closeButtonPressed}\n tabindex=\"0\"\n >\n ${this.customModalContent}\n </modal-template>\n </div>\n `;\n }\n\n /** @inheritdoc */\n getMode(): ModalManagerMode {\n return this.mode;\n }\n\n /** @inheritdoc */\n closeModal(): void {\n this.mode = ModalManagerMode.Closed;\n this.customModalContent = undefined;\n this.modalTemplate.config = new ModalConfig();\n }\n\n /**\n * Whether the modal should close if the user taps on the backdrop\n *\n * @private\n * @memberof ModalManager\n */\n private closeOnBackdropClick = true;\n\n /**\n * A callback if the user closes the modal\n *\n * @private\n * @memberof ModalManager\n */\n private userClosedModalCallback?: () => void;\n\n /**\n * Call the userClosedModalCallback and reset it if it exists\n *\n * @private\n * @memberof ModalManager\n */\n private callUserClosedModalCallback(): void {\n // we assign the callback to a temp var and undefine it before calling it\n // otherwise, we run into the potential for an infinite loop if the\n // callback triggers another `showModal()`, which would execute `userClosedModalCallback`\n const callback = this.userClosedModalCallback;\n this.userClosedModalCallback = undefined;\n if (callback) callback();\n }\n\n /** @inheritdoc */\n async showModal(options: {\n config: ModalConfig;\n customModalContent?: TemplateResult;\n userClosedModalCallback?: () => void;\n }): Promise<void> {\n this.closeOnBackdropClick = options.config.closeOnBackdropClick;\n this.userClosedModalCallback = options.userClosedModalCallback;\n this.modalTemplate.config = options.config;\n this.customModalContent = options.customModalContent;\n this.mode = ModalManagerMode.Open;\n await this.modalTemplate.updateComplete;\n this.modalTemplate.focus();\n }\n\n /** @inheritdoc */\n updated(changed: PropertyValues): void {\n /* istanbul ignore else */\n if (changed.has('mode')) {\n this.handleModeChange();\n }\n }\n\n /**\n * Called when the backdrop is clicked\n *\n * @private\n * @memberof ModalManager\n */\n private backdropClicked(): void {\n if (this.closeOnBackdropClick) {\n this.closeModal();\n this.callUserClosedModalCallback();\n }\n }\n\n /**\n * Handle the mode change\n *\n * @private\n * @memberof ModalManager\n */\n private handleModeChange(): void {\n this.hostBridge.handleModeChange(this.mode);\n this.emitModeChangeEvent();\n }\n\n /**\n * Emit a modeChange event\n *\n * @private\n * @memberof ModalManager\n */\n private emitModeChangeEvent(): void {\n const event = new CustomEvent('modeChanged', {\n detail: { mode: this.mode },\n });\n this.dispatchEvent(event);\n }\n\n /**\n * Called when the modal close button is pressed. Closes the modal.\n *\n * @private\n * @memberof ModalManager\n */\n private closeButtonPressed(): void {\n this.closeModal();\n this.callUserClosedModalCallback();\n }\n\n /** @inheritdoc */\n static get styles(): CSSResult {\n const modalBackdropColor = css`var(--modalBackdropColor, rgba(10, 10, 10, 0.9))`;\n const modalBackdropZindex = css`var(--modalBackdropZindex, 1000)`;\n\n const modalWidth = css`var(--modalWidth, 32rem)`;\n const modalMaxWidth = css`var(--modalMaxWidth, 95%)`;\n const modalZindex = css`var(--modalZindex, 2000)`;\n\n return css`\n .container {\n width: 100%;\n height: 100%;\n }\n\n .backdrop {\n position: fixed;\n top: 0;\n left: 0;\n background-color: ${modalBackdropColor};\n width: 100%;\n height: 100%;\n z-index: ${modalBackdropZindex};\n }\n\n modal-template {\n outline: 0;\n position: fixed;\n top: 0;\n left: 50%;\n transform: translate(-50%, 0);\n z-index: ${modalZindex};\n width: ${modalWidth};\n max-width: ${modalMaxWidth};\n }\n `;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"modal-manager.js","sourceRoot":"","sources":["../../src/modal-manager.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,UAAU,EACV,IAAI,EACJ,GAAG,GAIJ,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEnE,OAAO,KAAK,MAAM,kBAAkB,CAAC;AAErC,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAGrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGjD,IAAM,YAAY,GAAlB,MAAM,YAAa,SAAQ,UAAU;IAArC;;QACL;;;;;;;WAOG;QACwC,SAAI,GAC7C,gBAAgB,CAAC,MAAM,CAAC;QAU1B;;;;;;;;;WASG;QAEH,eAAU,GAAoC,IAAI,sBAAsB,CACtE,IAAI,CACL,CAAC;QAWF,sCAAsC;QAC/B,UAAK,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;QAyC/B;;;;;WAKG;QACK,yBAAoB,GAAG,IAAI,CAAC;IA+ItC,CAAC;IA5LO,YAAY;;YAChB,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GAAG,GAAG,CAAA,kCAAkC,CAAC;QAElE,MAAM,UAAU,GAAG,GAAG,CAAA,0BAA0B,CAAC;QACjD,MAAM,aAAa,GAAG,GAAG,CAAA,2BAA2B,CAAC;QACrD,MAAM,WAAW,GAAG,GAAG,CAAA,0BAA0B,CAAC;QAElD,OAAO,GAAG,CAAA;;;;;;;;;;4BAUc,kBAAkB;;;mBAG3B,mBAAmB;;;;;;;;;mBASnB,WAAW;iBACb,UAAU;qBACN,aAAa;;KAE7B,CAAC;IACJ,CAAC;CACF,CAAA;AAlO4C;IAA1C,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;0CAChB;AAQE;IAA3B,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;wDAAqC;AAahE;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;gDAGzB;AASuB;IAAxB,KAAK,CAAC,gBAAgB,CAAC;mDAAuC;AA1CpD,YAAY;IADxB,aAAa,CAAC,eAAe,CAAC;GAClB,YAAY,CA2OxB;SA3OY,YAAY","sourcesContent":["import {\n LitElement,\n html,\n css,\n CSSResult,\n TemplateResult,\n PropertyValues,\n} from 'lit';\nimport { property, customElement, query } from 'lit/decorators.js';\n\nimport Modal from './shoelace/modal';\n\nimport './modal-template';\nimport { ModalTemplate } from './modal-template';\nimport { ModalConfig } from './modal-config';\nimport { ModalManagerHostBridge } from './modal-manager-host-bridge';\nimport { ModalManagerInterface } from './modal-manager-interface';\nimport { ModalManagerHostBridgeInterface } from './modal-manager-host-bridge-interface';\nimport { ModalManagerMode } from './modal-manager-mode';\n\n@customElement('modal-manager')\nexport class ModalManager extends LitElement implements ModalManagerInterface {\n /**\n * The current mode of the ModalManager\n *\n * Current options are `modal` or `closed`\n *\n * @type {ModalManagerMode}\n * @memberof ModalManager\n */\n @property({ type: String, reflect: true }) mode: ModalManagerMode =\n ModalManagerMode.Closed;\n\n /**\n * Custom content to display in the modal's content slot\n *\n * @type {(TemplateResult | undefined)}\n * @memberof ModalManager\n */\n @property({ type: Object }) customModalContent?: TemplateResult;\n\n /**\n * This hostBridge handles environmental-specific interactions such as adding classes\n * to the body tag or event listeners needed to support the modal manager in the host environment.\n *\n * There is a default `ModalManagerHostBridge`, but consumers can override it with a custom\n * `ModalManagerHostBridgeInterface`\n *\n * @type {ModalManagerHostBridgeInterface}\n * @memberof ModalManager\n */\n @property({ type: Object })\n hostBridge: ModalManagerHostBridgeInterface = new ModalManagerHostBridge(\n this\n );\n\n /**\n * Reference to the ModalTemplate DOM element\n *\n * @private\n * @type {ModalTemplate}\n * @memberof ModalManager\n */\n @query('modal-template') private modalTemplate!: ModalTemplate;\n\n // Imported tab handling from shoelace\n public modal = new Modal(this);\n\n async firstUpdated(): Promise<void> {\n // Give the browser a chance to paint\n // eslint-disable-next-line no-promise-executor-return\n await new Promise(r => setTimeout(r, 0));\n }\n\n disconnectedCallback() {\n super.disconnectedCallback();\n this.modal.deactivate();\n }\n\n /** @inheritdoc */\n render(): TemplateResult {\n return html`\n <div class=\"container\">\n <div class=\"backdrop\" @click=${this.backdropClicked}></div>\n <modal-template\n @closeButtonPressed=${this.closeButtonPressed}\n tabindex=\"-1\"\n >\n ${this.customModalContent}\n </modal-template>\n </div>\n `;\n }\n\n /** @inheritdoc */\n getMode(): ModalManagerMode {\n return this.mode;\n }\n\n /** @inheritdoc */\n closeModal(): void {\n this.mode = ModalManagerMode.Closed;\n this.customModalContent = undefined;\n this.modalTemplate.config = new ModalConfig();\n this.modal.deactivate();\n }\n\n /**\n * Whether the modal should close if the user taps on the backdrop\n *\n * @private\n * @memberof ModalManager\n */\n private closeOnBackdropClick = true;\n\n /**\n * A callback if the user closes the modal\n *\n * @private\n * @memberof ModalManager\n */\n private userClosedModalCallback?: () => void;\n\n /**\n * Call the userClosedModalCallback and reset it if it exists\n *\n * @private\n * @memberof ModalManager\n */\n private callUserClosedModalCallback(): void {\n // we assign the callback to a temp var and undefine it before calling it\n // otherwise, we run into the potential for an infinite loop if the\n // callback triggers another `showModal()`, which would execute `userClosedModalCallback`\n const callback = this.userClosedModalCallback;\n this.userClosedModalCallback = undefined;\n if (callback) callback();\n }\n\n /** @inheritdoc */\n async showModal(options: {\n config: ModalConfig;\n customModalContent?: TemplateResult;\n userClosedModalCallback?: () => void;\n }): Promise<void> {\n this.closeOnBackdropClick = options.config.closeOnBackdropClick;\n this.userClosedModalCallback = options.userClosedModalCallback;\n this.modalTemplate.config = options.config;\n this.customModalContent = options.customModalContent;\n this.mode = ModalManagerMode.Open;\n await this.modalTemplate.updateComplete;\n\n this.modal.activate();\n\n // document.addEventListener('keydown', this.tabHandler);\n\n // this.firstFocusableElement.focus();\n // console.log('firstFocusableElement', this.firstFocusableElement);\n // console.log('lastFocusableElement', this.lastFocusableElement);\n // console.log('focusableContent', this.focusableContent);\n\n // this.modalTemplate.focus();\n }\n\n /** @inheritdoc */\n updated(changed: PropertyValues): void {\n /* istanbul ignore else */\n if (changed.has('mode')) {\n this.handleModeChange();\n }\n }\n\n /**\n * Called when the backdrop is clicked\n *\n * @private\n * @memberof ModalManager\n */\n private backdropClicked(): void {\n if (this.closeOnBackdropClick) {\n this.closeModal();\n this.callUserClosedModalCallback();\n }\n }\n\n /**\n * Handle the mode change\n *\n * @private\n * @memberof ModalManager\n */\n private handleModeChange(): void {\n this.hostBridge.handleModeChange(this.mode);\n this.emitModeChangeEvent();\n }\n\n /**\n * Emit a modeChange event\n *\n * @private\n * @memberof ModalManager\n */\n private emitModeChangeEvent(): void {\n const event = new CustomEvent('modeChanged', {\n detail: { mode: this.mode },\n });\n this.dispatchEvent(event);\n }\n\n /**\n * Called when the modal close button is pressed. Closes the modal.\n *\n * @private\n * @memberof ModalManager\n */\n private closeButtonPressed(): void {\n this.closeModal();\n this.callUserClosedModalCallback();\n }\n\n /** @inheritdoc */\n static get styles(): CSSResult {\n const modalBackdropColor = css`var(--modalBackdropColor, rgba(10, 10, 10, 0.9))`;\n const modalBackdropZindex = css`var(--modalBackdropZindex, 1000)`;\n\n const modalWidth = css`var(--modalWidth, 32rem)`;\n const modalMaxWidth = css`var(--modalMaxWidth, 95%)`;\n const modalZindex = css`var(--modalZindex, 2000)`;\n\n return css`\n .container {\n width: 100%;\n height: 100%;\n }\n\n .backdrop {\n position: fixed;\n top: 0;\n left: 0;\n background-color: ${modalBackdropColor};\n width: 100%;\n height: 100%;\n z-index: ${modalBackdropZindex};\n }\n\n modal-template {\n outline: 0;\n position: fixed;\n top: 0;\n left: 50%;\n transform: translate(-50%, 0);\n z-index: ${modalZindex};\n width: ${modalWidth};\n max-width: ${modalMaxWidth};\n }\n `;\n }\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use a generator so we can iterate and possibly break early.
|
|
3
|
+
* @example
|
|
4
|
+
* // to operate like a regular array. This kinda nullifies generator benefits, but worth knowing if you need the whole array.
|
|
5
|
+
* const allActiveElements = [...activeElements()]
|
|
6
|
+
*
|
|
7
|
+
* // Early return
|
|
8
|
+
* for (const activeElement of activeElements()) {
|
|
9
|
+
* if (<cond>) {
|
|
10
|
+
* break; // Break the loop, dont need to iterate over the whole array or store an array in memory!
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export declare function activeElements(activeElement?: Element | null): Generator<Element>;
|
|
15
|
+
export declare function getDeepestActiveElement(): Element | undefined;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use a generator so we can iterate and possibly break early.
|
|
3
|
+
* @example
|
|
4
|
+
* // to operate like a regular array. This kinda nullifies generator benefits, but worth knowing if you need the whole array.
|
|
5
|
+
* const allActiveElements = [...activeElements()]
|
|
6
|
+
*
|
|
7
|
+
* // Early return
|
|
8
|
+
* for (const activeElement of activeElements()) {
|
|
9
|
+
* if (<cond>) {
|
|
10
|
+
* break; // Break the loop, dont need to iterate over the whole array or store an array in memory!
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export function* activeElements(activeElement = document.activeElement) {
|
|
15
|
+
if (activeElement === null || activeElement === undefined)
|
|
16
|
+
return;
|
|
17
|
+
yield activeElement;
|
|
18
|
+
if ('shadowRoot' in activeElement &&
|
|
19
|
+
activeElement.shadowRoot &&
|
|
20
|
+
activeElement.shadowRoot.mode !== 'closed') {
|
|
21
|
+
yield* activeElements(activeElement.shadowRoot.activeElement);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function getDeepestActiveElement() {
|
|
25
|
+
return [...activeElements()].pop();
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=active-elements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"active-elements.js","sourceRoot":"","sources":["../../../src/shoelace/active-elements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,SAAS,CAAC,CAAC,cAAc,CAC7B,gBAAgC,QAAQ,CAAC,aAAa;IAEtD,IAAI,aAAa,KAAK,IAAI,IAAI,aAAa,KAAK,SAAS;QAAE,OAAO;IAElE,MAAM,aAAa,CAAC;IAEpB,IACE,YAAY,IAAI,aAAa;QAC7B,aAAa,CAAC,UAAU;QACxB,aAAa,CAAC,UAAU,CAAC,IAAI,KAAK,QAAQ,EAC1C;QACA,KAAK,CAAC,CAAC,cAAc,CAAC,aAAa,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;KAC/D;AACH,CAAC;AAED,MAAM,UAAU,uBAAuB;IACrC,OAAO,CAAC,GAAG,cAAc,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;AACrC,CAAC","sourcesContent":["/**\n * Use a generator so we can iterate and possibly break early.\n * @example\n * // to operate like a regular array. This kinda nullifies generator benefits, but worth knowing if you need the whole array.\n * const allActiveElements = [...activeElements()]\n *\n * // Early return\n * for (const activeElement of activeElements()) {\n * if (<cond>) {\n * break; // Break the loop, dont need to iterate over the whole array or store an array in memory!\n * }\n * }\n */\nexport function* activeElements(\n activeElement: Element | null = document.activeElement\n): Generator<Element> {\n if (activeElement === null || activeElement === undefined) return;\n\n yield activeElement;\n\n if (\n 'shadowRoot' in activeElement &&\n activeElement.shadowRoot &&\n activeElement.shadowRoot.mode !== 'closed'\n ) {\n yield* activeElements(activeElement.shadowRoot.activeElement);\n }\n}\n\nexport function getDeepestActiveElement() {\n return [...activeElements()].pop();\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default class Modal {
|
|
2
|
+
element: HTMLElement;
|
|
3
|
+
isExternalActivated: boolean;
|
|
4
|
+
tabDirection: 'forward' | 'backward';
|
|
5
|
+
currentFocus: HTMLElement | null;
|
|
6
|
+
previousFocus: HTMLElement | null;
|
|
7
|
+
elementsWithTabbableControls: string[];
|
|
8
|
+
constructor(element: HTMLElement);
|
|
9
|
+
/** Activates focus trapping. */
|
|
10
|
+
activate(): void;
|
|
11
|
+
/** Deactivates focus trapping. */
|
|
12
|
+
deactivate(): void;
|
|
13
|
+
/** Determines if this modal element is currently active or not. */
|
|
14
|
+
isActive(): boolean;
|
|
15
|
+
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
16
|
+
activateExternal(): void;
|
|
17
|
+
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
18
|
+
deactivateExternal(): void;
|
|
19
|
+
private checkFocus;
|
|
20
|
+
private handleFocusIn;
|
|
21
|
+
private possiblyHasTabbableChildren;
|
|
22
|
+
private handleKeyDown;
|
|
23
|
+
private handleKeyUp;
|
|
24
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { activeElements, getDeepestActiveElement } from './active-elements.js';
|
|
2
|
+
import { getTabbableElements } from './tabbable.js';
|
|
3
|
+
let activeModals = [];
|
|
4
|
+
export default class Modal {
|
|
5
|
+
constructor(element) {
|
|
6
|
+
this.isExternalActivated = false;
|
|
7
|
+
this.tabDirection = 'forward';
|
|
8
|
+
this.currentFocus = null;
|
|
9
|
+
this.previousFocus = null;
|
|
10
|
+
this.handleFocusIn = () => {
|
|
11
|
+
if (!this.isActive())
|
|
12
|
+
return;
|
|
13
|
+
this.checkFocus();
|
|
14
|
+
};
|
|
15
|
+
this.handleKeyDown = (event) => {
|
|
16
|
+
var _a;
|
|
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
|
+
// eslint-disable-next-line
|
|
41
|
+
while (true) {
|
|
42
|
+
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
43
|
+
currentFocusIndex = 0;
|
|
44
|
+
}
|
|
45
|
+
else if (currentFocusIndex + addition < 0) {
|
|
46
|
+
currentFocusIndex = tabbableElements.length - 1;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
currentFocusIndex += addition;
|
|
50
|
+
}
|
|
51
|
+
this.previousFocus = this.currentFocus;
|
|
52
|
+
const nextFocus =
|
|
53
|
+
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
54
|
+
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
55
|
+
// that possibly has "controls"
|
|
56
|
+
if (this.tabDirection === 'backward') {
|
|
57
|
+
if (this.previousFocus &&
|
|
58
|
+
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
this.currentFocus = nextFocus;
|
|
67
|
+
(_a = this.currentFocus) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: false });
|
|
68
|
+
// 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.
|
|
69
|
+
const allActiveElements = [...activeElements()];
|
|
70
|
+
if (allActiveElements.includes(this.currentFocus) ||
|
|
71
|
+
!allActiveElements.includes(this.previousFocus)) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
setTimeout(() => this.checkFocus());
|
|
76
|
+
};
|
|
77
|
+
this.handleKeyUp = () => {
|
|
78
|
+
this.tabDirection = 'forward';
|
|
79
|
+
};
|
|
80
|
+
this.element = element;
|
|
81
|
+
this.elementsWithTabbableControls = ['iframe'];
|
|
82
|
+
}
|
|
83
|
+
/** Activates focus trapping. */
|
|
84
|
+
activate() {
|
|
85
|
+
activeModals.push(this.element);
|
|
86
|
+
document.addEventListener('focusin', this.handleFocusIn);
|
|
87
|
+
document.addEventListener('keydown', this.handleKeyDown);
|
|
88
|
+
document.addEventListener('keyup', this.handleKeyUp);
|
|
89
|
+
}
|
|
90
|
+
/** Deactivates focus trapping. */
|
|
91
|
+
deactivate() {
|
|
92
|
+
activeModals = activeModals.filter(modal => modal !== this.element);
|
|
93
|
+
this.currentFocus = null;
|
|
94
|
+
document.removeEventListener('focusin', this.handleFocusIn);
|
|
95
|
+
document.removeEventListener('keydown', this.handleKeyDown);
|
|
96
|
+
document.removeEventListener('keyup', this.handleKeyUp);
|
|
97
|
+
}
|
|
98
|
+
/** Determines if this modal element is currently active or not. */
|
|
99
|
+
isActive() {
|
|
100
|
+
// The "active" modal is always the most recent one shown
|
|
101
|
+
return activeModals[activeModals.length - 1] === this.element;
|
|
102
|
+
}
|
|
103
|
+
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
104
|
+
activateExternal() {
|
|
105
|
+
this.isExternalActivated = true;
|
|
106
|
+
}
|
|
107
|
+
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
108
|
+
deactivateExternal() {
|
|
109
|
+
this.isExternalActivated = false;
|
|
110
|
+
}
|
|
111
|
+
checkFocus() {
|
|
112
|
+
if (this.isActive() && !this.isExternalActivated) {
|
|
113
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
114
|
+
if (!this.element.matches(':focus-within')) {
|
|
115
|
+
const start = tabbableElements[0];
|
|
116
|
+
const end = tabbableElements[tabbableElements.length - 1];
|
|
117
|
+
const target = this.tabDirection === 'forward' ? start : end;
|
|
118
|
+
if (typeof (target === null || target === void 0 ? void 0 : target.focus) === 'function') {
|
|
119
|
+
this.currentFocus = target;
|
|
120
|
+
target.focus({ preventScroll: false });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
possiblyHasTabbableChildren(element) {
|
|
126
|
+
return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
|
|
127
|
+
// 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?
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=modal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modal.js","sourceRoot":"","sources":["../../../src/shoelace/modal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB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AAiF;IACjF,gBAAgB;QACd,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAClC,CAAC;IAED,yEAAyE;IACzE,kBAAkB;QAChB,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;IACnC,CAAC;IAEO,UAAU;QAChB,IAAI,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,mBAAmB,EAAE;YAChD,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC3D,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE;gBAC1C,MAAM,KAAK,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;gBAClC,MAAM,GAAG,GAAG,gBAAgB,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBAC1D,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC;gBAE7D,IAAI,OAAO,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,CAAA,KAAK,UAAU,EAAE;oBACvC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;oBAC3B,MAAM,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;iBACxC;aACF;SACF;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;CAoFF","sourcesContent":["import { 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"]}
|
|
@@ -0,0 +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[];
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Cached compute style calls. This is specifically for browsers that dont support `checkVisibility()`.
|
|
2
|
+
// computedStyle calls are "live" so they only need to be retrieved once for an element.
|
|
3
|
+
const computedStyleMap = new WeakMap();
|
|
4
|
+
function getCachedComputedStyle(el) {
|
|
5
|
+
let computedStyle = computedStyleMap.get(el);
|
|
6
|
+
if (!computedStyle) {
|
|
7
|
+
computedStyle = window.getComputedStyle(el, null);
|
|
8
|
+
computedStyleMap.set(el, computedStyle);
|
|
9
|
+
}
|
|
10
|
+
return computedStyle;
|
|
11
|
+
}
|
|
12
|
+
function isVisible(el) {
|
|
13
|
+
// This is the fastest check, but isn't supported in Safari.
|
|
14
|
+
if ('checkVisibility' in el && typeof el['checkVisibility'] === 'function') {
|
|
15
|
+
// Opacity is focusable, visibility is not.
|
|
16
|
+
return el['checkVisibility']({
|
|
17
|
+
checkOpacity: false,
|
|
18
|
+
checkVisibilityCSS: true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
// Fallback "polyfill" for "checkVisibility"
|
|
22
|
+
const computedStyle = getCachedComputedStyle(el);
|
|
23
|
+
return (computedStyle.visibility !== 'hidden' && computedStyle.display !== 'none');
|
|
24
|
+
}
|
|
25
|
+
// While this behavior isn't standard in Safari / Chrome yet, I think it's the most reasonable
|
|
26
|
+
// way of handling tabbable overflow areas. Browser sniffing seems gross, and it's the most
|
|
27
|
+
// accessible way of handling overflow areas. [Konnor]
|
|
28
|
+
function isOverflowingAndTabbable(el) {
|
|
29
|
+
const computedStyle = getCachedComputedStyle(el);
|
|
30
|
+
const { overflowY, overflowX } = computedStyle;
|
|
31
|
+
if (overflowY === 'scroll' || overflowX === 'scroll') {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (overflowY !== 'auto' || overflowX !== 'auto') {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// Always overflow === "auto" by this point
|
|
38
|
+
const isOverflowingY = el.scrollHeight > el.clientHeight;
|
|
39
|
+
if (isOverflowingY && overflowY === 'auto') {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
const isOverflowingX = el.scrollWidth > el.clientWidth;
|
|
43
|
+
if (isOverflowingX && overflowX === 'auto') {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
/** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */
|
|
49
|
+
function isTabbable(el) {
|
|
50
|
+
const tag = el.tagName.toLowerCase();
|
|
51
|
+
const tabindex = Number(el.getAttribute('tabindex'));
|
|
52
|
+
const hasTabindex = el.hasAttribute('tabindex');
|
|
53
|
+
// elements with a tabindex attribute that is either NaN or <= -1 are not tabbable
|
|
54
|
+
if (hasTabindex && (isNaN(tabindex) || tabindex <= -1)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
// Elements with a disabled attribute are not tabbable
|
|
58
|
+
if (el.hasAttribute('disabled')) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
// If any parents have "inert", we aren't "tabbable"
|
|
62
|
+
if (el.closest('[inert]')) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
// Radios without a checked attribute are not tabbable
|
|
66
|
+
if (tag === 'input' &&
|
|
67
|
+
el.getAttribute('type') === 'radio' &&
|
|
68
|
+
!el.hasAttribute('checked')) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (!isVisible(el)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
// Audio and video elements with the controls attribute are tabbable
|
|
75
|
+
if ((tag === 'audio' || tag === 'video') && el.hasAttribute('controls')) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
// Elements with a tabindex other than -1 are tabbable
|
|
79
|
+
if (el.hasAttribute('tabindex')) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
// Elements with a contenteditable attribute are tabbable
|
|
83
|
+
if (el.hasAttribute('contenteditable') &&
|
|
84
|
+
el.getAttribute('contenteditable') !== 'false') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
// At this point, the following elements are considered tabbable
|
|
88
|
+
const isNativelyTabbable = [
|
|
89
|
+
'button',
|
|
90
|
+
'input',
|
|
91
|
+
'select',
|
|
92
|
+
'textarea',
|
|
93
|
+
'a',
|
|
94
|
+
'audio',
|
|
95
|
+
'video',
|
|
96
|
+
'summary',
|
|
97
|
+
'iframe',
|
|
98
|
+
].includes(tag);
|
|
99
|
+
if (isNativelyTabbable) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
// We save the overflow checks for last, because they're the most expensive
|
|
103
|
+
return isOverflowingAndTabbable(el);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
|
|
107
|
+
* element because it short-circuits after finding the first and last ones.
|
|
108
|
+
*/
|
|
109
|
+
export function getTabbableBoundary(root) {
|
|
110
|
+
var _a, _b;
|
|
111
|
+
const tabbableElements = getTabbableElements(root);
|
|
112
|
+
// Find the first and last tabbable elements
|
|
113
|
+
const start = (_a = tabbableElements[0]) !== null && _a !== void 0 ? _a : null;
|
|
114
|
+
const end = (_b = tabbableElements[tabbableElements.length - 1]) !== null && _b !== void 0 ? _b : null;
|
|
115
|
+
return { start, end };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* This looks funky. Basically a slot's children will always be picked up *if* they're within the `root` element.
|
|
119
|
+
* However, there is an edge case when, if the `root` is wrapped by another shadow DOM, it won't grab the children.
|
|
120
|
+
* This fixes that fun edge case.
|
|
121
|
+
*/
|
|
122
|
+
function getSlottedChildrenOutsideRootElement(slotElement, root) {
|
|
123
|
+
var _a;
|
|
124
|
+
return (((_a = slotElement.getRootNode({ composed: true })) === null || _a === void 0 ? void 0 : _a.host) !==
|
|
125
|
+
root);
|
|
126
|
+
}
|
|
127
|
+
export function getTabbableElements(root) {
|
|
128
|
+
const walkedEls = new WeakMap();
|
|
129
|
+
const tabbableElements = [];
|
|
130
|
+
function walk(el) {
|
|
131
|
+
if (el instanceof Element) {
|
|
132
|
+
// if the element has "inert" we can just no-op it.
|
|
133
|
+
if (el.hasAttribute('inert') || el.closest('[inert]')) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (walkedEls.has(el)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
walkedEls.set(el, true);
|
|
140
|
+
if (!tabbableElements.includes(el) && isTabbable(el)) {
|
|
141
|
+
tabbableElements.push(el);
|
|
142
|
+
}
|
|
143
|
+
if (el instanceof HTMLSlotElement &&
|
|
144
|
+
getSlottedChildrenOutsideRootElement(el, root)) {
|
|
145
|
+
el.assignedElements({ flatten: true }).forEach((assignedEl) => {
|
|
146
|
+
walk(assignedEl);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (el.shadowRoot !== null && el.shadowRoot.mode === 'open') {
|
|
150
|
+
walk(el.shadowRoot);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const e of Array.from(el.children)) {
|
|
154
|
+
walk(e);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// Collect all elements including the root
|
|
158
|
+
walk(root);
|
|
159
|
+
// Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.
|
|
160
|
+
// So is it worth being right? Or fast?
|
|
161
|
+
return tabbableElements.sort((a, b) => {
|
|
162
|
+
// Make sure we sort by tabindex.
|
|
163
|
+
const aTabindex = Number(a.getAttribute('tabindex')) || 0;
|
|
164
|
+
const bTabindex = Number(b.getAttribute('tabindex')) || 0;
|
|
165
|
+
return bTabindex - aTabindex;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=tabbable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tabbable.js","sourceRoot":"","sources":["../../../src/shoelace/tabbable.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,wFAAwF;AACxF,MAAM,gBAAgB,GAAG,IAAI,OAAO,EAAgC,CAAC;AAErE,SAAS,sBAAsB,CAAC,EAAe;IAC7C,IAAI,aAAa,GAAoC,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAE9E,IAAI,CAAC,aAAa,EAAE;QAClB,aAAa,GAAG,MAAM,CAAC,gBAAgB,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QAClD,gBAAgB,CAAC,GAAG,CAAC,EAAE,EAAE,aAAa,CAAC,CAAC;KACzC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,SAAS,SAAS,CAAC,EAAe;IAChC,8DAA8D;IAC9D,IAAI,iBAAiB,IAAI,EAAE,IAAI,OAAO,EAAE,CAAC,iBAAiB,CAAC,KAAK,UAAU,EAAE;QAC1E,2CAA2C;QAC3C,OAAQ,EAAU,CAAC,iBAAiB,CAAC,CAAC;YACpC,YAAY,EAAE,KAAK;YACnB,kBAAkB,EAAE,IAAI;SACzB,CAAC,CAAC;KACJ;IAED,4CAA4C;IAC5C,MAAM,aAAa,GAAG,sBAAsB,CAAC,EAAiB,CAAC,CAAC;IAEhE,OAAO,CACL,aAAa,CAAC,UAAU,KAAK,QAAQ,IAAI,aAAa,CAAC,OAAO,KAAK,MAAM,CAC1E,CAAC;AACJ,CAAC;AAED,8FAA8F;AAC9F,2FAA2F;AAC3F,sDAAsD;AACtD,SAAS,wBAAwB,CAAC,EAAe;IAC/C,MAAM,aAAa,GAAG,sBAAsB,CAAC,EAAE,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KAAK,MAAM,EAAE;gBAC3D,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC;aACrB;SACF;QAED,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAkB,EAAE;YACxD,IAAI,CAAC,CAAC,CAAC,CAAC;SACT;IACH,CAAC;IAED,0CAA0C;IAC1C,IAAI,CAAC,IAAI,CAAC,CAAC;IAEX,yHAAyH;IACzH,uCAAuC;IACvC,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACpC,iCAAiC;QACjC,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC;QAC1D,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC;QAC1D,OAAO,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Cached compute style calls. This is specifically for browsers that dont support `checkVisibility()`.\n// computedStyle calls are \"live\" so they only need to be retrieved once for an element.\nconst computedStyleMap = new WeakMap<Element, CSSStyleDeclaration>();\n\nfunction getCachedComputedStyle(el: HTMLElement): CSSStyleDeclaration {\n let computedStyle: undefined | CSSStyleDeclaration = computedStyleMap.get(el);\n\n if (!computedStyle) {\n computedStyle = window.getComputedStyle(el, null);\n computedStyleMap.set(el, computedStyle);\n }\n\n return computedStyle;\n}\n\nfunction isVisible(el: HTMLElement): boolean {\n // This is the fastest check, but isn't supported in Safari. \n if ('checkVisibility' in el && typeof el['checkVisibility'] === 'function') {\n // Opacity is focusable, visibility is not.\n return (el as any)['checkVisibility']({\n checkOpacity: false,\n checkVisibilityCSS: true,\n });\n }\n\n // Fallback \"polyfill\" for \"checkVisibility\"\n const computedStyle = getCachedComputedStyle(el as HTMLElement);\n\n return (\n computedStyle.visibility !== 'hidden' && computedStyle.display !== 'none'\n );\n}\n\n// While this behavior isn't standard in Safari / Chrome yet, I think it's the most reasonable\n// way of handling tabbable overflow areas. Browser sniffing seems gross, and it's the most\n// accessible way of handling overflow areas. [Konnor]\nfunction isOverflowingAndTabbable(el: HTMLElement): boolean {\n const computedStyle = getCachedComputedStyle(el);\n\n const { overflowY, overflowX } = computedStyle;\n\n if (overflowY === 'scroll' || overflowX === 'scroll') {\n return true;\n }\n\n if (overflowY !== 'auto' || overflowX !== 'auto') {\n return false;\n }\n\n // Always overflow === \"auto\" by this point\n const isOverflowingY = el.scrollHeight > el.clientHeight;\n\n if (isOverflowingY && overflowY === 'auto') {\n return true;\n }\n\n const isOverflowingX = el.scrollWidth > el.clientWidth;\n\n if (isOverflowingX && overflowX === 'auto') {\n return true;\n }\n\n return false;\n}\n\n/** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */\nfunction isTabbable(el: HTMLElement) {\n const tag = el.tagName.toLowerCase();\n\n const tabindex = Number(el.getAttribute('tabindex'));\n const hasTabindex = el.hasAttribute('tabindex');\n\n // elements with a tabindex attribute that is either NaN or <= -1 are not tabbable\n if (hasTabindex && (isNaN(tabindex) || tabindex <= -1)) {\n return false;\n }\n\n // Elements with a disabled attribute are not tabbable\n if (el.hasAttribute('disabled')) {\n return false;\n }\n\n // If any parents have \"inert\", we aren't \"tabbable\"\n if (el.closest('[inert]')) {\n return false;\n }\n\n // Radios without a checked attribute are not tabbable\n if (\n tag === 'input' &&\n el.getAttribute('type') === 'radio' &&\n !el.hasAttribute('checked')\n ) {\n return false;\n }\n\n if (!isVisible(el)) {\n return false;\n }\n\n // Audio and video elements with the controls attribute are tabbable\n if ((tag === 'audio' || tag === 'video') && el.hasAttribute('controls')) {\n return true;\n }\n\n // Elements with a tabindex other than -1 are tabbable\n if (el.hasAttribute('tabindex')) {\n return true;\n }\n\n // Elements with a contenteditable attribute are tabbable\n if (\n el.hasAttribute('contenteditable') &&\n el.getAttribute('contenteditable') !== 'false'\n ) {\n return true;\n }\n\n // At this point, the following elements are considered tabbable\n const isNativelyTabbable = [\n 'button',\n 'input',\n 'select',\n 'textarea',\n 'a',\n 'audio',\n 'video',\n 'summary',\n 'iframe',\n ].includes(tag);\n\n if (isNativelyTabbable) {\n return true;\n }\n\n // We save the overflow checks for last, because they're the most expensive\n return isOverflowingAndTabbable(el);\n}\n\n/**\n * Returns the first and last bounding elements that are tabbable. This is more performant than checking every single\n * element because it short-circuits after finding the first and last ones.\n */\nexport function getTabbableBoundary(root: HTMLElement | ShadowRoot) {\n const tabbableElements = getTabbableElements(root);\n\n // Find the first and last tabbable elements\n const start = tabbableElements[0] ?? null;\n const end = tabbableElements[tabbableElements.length - 1] ?? null;\n\n return { start, end };\n}\n\n/**\n * This looks funky. Basically a slot's children will always be picked up *if* they're within the `root` element.\n * However, there is an edge case when, if the `root` is wrapped by another shadow DOM, it won't grab the children.\n * This fixes that fun edge case.\n */\nfunction getSlottedChildrenOutsideRootElement(\n slotElement: HTMLSlotElement,\n root: HTMLElement | ShadowRoot\n) {\n return (\n (slotElement.getRootNode({ composed: true }) as ShadowRoot | null)?.host !==\n root\n );\n}\n\nexport function getTabbableElements(root: HTMLElement | ShadowRoot) {\n const walkedEls = new WeakMap();\n const tabbableElements: HTMLElement[] = [];\n\n function walk(el: HTMLElement | ShadowRoot) {\n if (el instanceof Element) {\n // if the element has \"inert\" we can just no-op it.\n if (el.hasAttribute('inert') || el.closest('[inert]')) {\n return;\n }\n\n if (walkedEls.has(el)) {\n return;\n }\n walkedEls.set(el, true);\n\n if (!tabbableElements.includes(el) && isTabbable(el)) {\n tabbableElements.push(el);\n }\n\n if (\n el instanceof HTMLSlotElement &&\n getSlottedChildrenOutsideRootElement(el, root)\n ) {\n el.assignedElements({ flatten: true }).forEach(\n (assignedEl: Element) => {\n walk(assignedEl as HTMLElement | ShadowRoot);\n }\n );\n }\n\n if (el.shadowRoot !== null && el.shadowRoot.mode === 'open') {\n walk(el.shadowRoot);\n }\n }\n\n for (const e of Array.from(el.children) as HTMLElement[]) {\n walk(e);\n }\n }\n\n // Collect all elements including the root\n walk(root);\n\n // Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.\n // So is it worth being right? Or fast?\n return tabbableElements.sort((a, b) => {\n // Make sure we sort by tabindex.\n const aTabindex = Number(a.getAttribute('tabindex')) || 0;\n const bTabindex = Number(b.getAttribute('tabindex')) || 0;\n return bTabindex - aTabindex;\n });\n}\n"]}
|
|
@@ -42,7 +42,7 @@ describe('Modal Manager', () => {
|
|
|
42
42
|
setTimeout(() => {
|
|
43
43
|
el.showModal({ config });
|
|
44
44
|
});
|
|
45
|
-
const response = yield oneEvent(el, 'modeChanged');
|
|
45
|
+
const response = yield oneEvent(el, 'modeChanged', false);
|
|
46
46
|
expect(response.detail.mode).to.equal(ModalManagerMode.Open);
|
|
47
47
|
}));
|
|
48
48
|
it('emits a modeChanged event when closing', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -55,7 +55,7 @@ describe('Modal Manager', () => {
|
|
|
55
55
|
setTimeout(() => {
|
|
56
56
|
el.closeModal();
|
|
57
57
|
});
|
|
58
|
-
const response = yield oneEvent(el, 'modeChanged');
|
|
58
|
+
const response = yield oneEvent(el, 'modeChanged', false);
|
|
59
59
|
expect(response.detail.mode).to.equal(ModalManagerMode.Closed);
|
|
60
60
|
}));
|
|
61
61
|
it('can show a modal', () => __awaiter(void 0, void 0, void 0, function* () {
|