@primer/behaviors 0.0.0-2022027215135 → 0.0.0-202202813730

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.
@@ -0,0 +1,8 @@
1
+ export declare class ModalDialogElement extends HTMLElement {
2
+ private abortController;
3
+ constructor();
4
+ connectedCallback(): void;
5
+ disconnectedCallback(): void;
6
+ show(): void;
7
+ close(): void;
8
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModalDialogElement = void 0;
4
+ const focus_trap_js_1 = require("../focus-trap.js");
5
+ class ModalDialogElement extends HTMLElement {
6
+ constructor() {
7
+ var _a, _b;
8
+ super();
9
+ (_a = this.querySelector('.close-button')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => this.close());
10
+ (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', () => this.show());
11
+ }
12
+ connectedCallback() {
13
+ if (!this.hasAttribute('role'))
14
+ this.setAttribute('role', 'dialog');
15
+ const subscriptions = [
16
+ fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
17
+ fromEvent(this, 'compositionend', e => trackComposition(this, e)),
18
+ fromEvent(this, 'keydown', e => keydown(this, e))
19
+ ];
20
+ states.set(this, { subscriptions, loaded: false, isComposing: false });
21
+ }
22
+ disconnectedCallback() {
23
+ const state = states.get(this);
24
+ if (!state)
25
+ return;
26
+ states.delete(this);
27
+ for (const sub of state.subscriptions) {
28
+ sub.unsubscribe();
29
+ }
30
+ }
31
+ show() {
32
+ var _a;
33
+ const isClosed = this.classList.contains('hidden');
34
+ if (!isClosed)
35
+ return;
36
+ this.classList.remove('hidden');
37
+ this.setAttribute('open', '');
38
+ if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
39
+ this.parentElement.classList.add('active');
40
+ }
41
+ document.body.style.overflow = 'hidden';
42
+ this.abortController = (0, focus_trap_js_1.focusTrap)(this);
43
+ }
44
+ close() {
45
+ var _a, _b;
46
+ const isClosed = this.classList.contains('hidden');
47
+ if (isClosed)
48
+ return;
49
+ this.classList.add('hidden');
50
+ this.removeAttribute('open');
51
+ if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
52
+ this.parentElement.classList.remove('active');
53
+ }
54
+ document.body.style.overflow = 'initial';
55
+ (_b = this.abortController) === null || _b === void 0 ? void 0 : _b.abort();
56
+ }
57
+ }
58
+ exports.ModalDialogElement = ModalDialogElement;
59
+ const states = new WeakMap();
60
+ function fromEvent(target, eventName, onNext, options = false) {
61
+ target.addEventListener(eventName, onNext, options);
62
+ return {
63
+ unsubscribe: () => {
64
+ target.removeEventListener(eventName, onNext, options);
65
+ }
66
+ };
67
+ }
68
+ function keydown(dialog, event) {
69
+ if (!(event instanceof KeyboardEvent))
70
+ return;
71
+ const state = states.get(dialog);
72
+ if (!state || state.isComposing)
73
+ return;
74
+ switch (event.key) {
75
+ case 'Escape':
76
+ if (dialog.hasAttribute('open')) {
77
+ dialog.close();
78
+ event.preventDefault();
79
+ event.stopPropagation();
80
+ }
81
+ break;
82
+ }
83
+ }
84
+ function trackComposition(dialog, event) {
85
+ const state = states.get(dialog);
86
+ if (!state)
87
+ return;
88
+ state.isComposing = event.type === 'compositionstart';
89
+ }
@@ -0,0 +1,8 @@
1
+ export declare class ModalDialogElement extends HTMLElement {
2
+ private abortController;
3
+ constructor();
4
+ connectedCallback(): void;
5
+ disconnectedCallback(): void;
6
+ show(): void;
7
+ close(): void;
8
+ }
@@ -0,0 +1,85 @@
1
+ import { focusTrap } from '../focus-trap.js';
2
+ export class ModalDialogElement extends HTMLElement {
3
+ constructor() {
4
+ var _a, _b;
5
+ super();
6
+ (_a = this.querySelector('.close-button')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => this.close());
7
+ (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', () => this.show());
8
+ }
9
+ connectedCallback() {
10
+ if (!this.hasAttribute('role'))
11
+ this.setAttribute('role', 'dialog');
12
+ const subscriptions = [
13
+ fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
14
+ fromEvent(this, 'compositionend', e => trackComposition(this, e)),
15
+ fromEvent(this, 'keydown', e => keydown(this, e))
16
+ ];
17
+ states.set(this, { subscriptions, loaded: false, isComposing: false });
18
+ }
19
+ disconnectedCallback() {
20
+ const state = states.get(this);
21
+ if (!state)
22
+ return;
23
+ states.delete(this);
24
+ for (const sub of state.subscriptions) {
25
+ sub.unsubscribe();
26
+ }
27
+ }
28
+ show() {
29
+ var _a;
30
+ const isClosed = this.classList.contains('hidden');
31
+ if (!isClosed)
32
+ return;
33
+ this.classList.remove('hidden');
34
+ this.setAttribute('open', '');
35
+ if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
36
+ this.parentElement.classList.add('active');
37
+ }
38
+ document.body.style.overflow = 'hidden';
39
+ this.abortController = focusTrap(this);
40
+ }
41
+ close() {
42
+ var _a, _b;
43
+ const isClosed = this.classList.contains('hidden');
44
+ if (isClosed)
45
+ return;
46
+ this.classList.add('hidden');
47
+ this.removeAttribute('open');
48
+ if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
49
+ this.parentElement.classList.remove('active');
50
+ }
51
+ document.body.style.overflow = 'initial';
52
+ (_b = this.abortController) === null || _b === void 0 ? void 0 : _b.abort();
53
+ }
54
+ }
55
+ const states = new WeakMap();
56
+ function fromEvent(target, eventName, onNext, options = false) {
57
+ target.addEventListener(eventName, onNext, options);
58
+ return {
59
+ unsubscribe: () => {
60
+ target.removeEventListener(eventName, onNext, options);
61
+ }
62
+ };
63
+ }
64
+ function keydown(dialog, event) {
65
+ if (!(event instanceof KeyboardEvent))
66
+ return;
67
+ const state = states.get(dialog);
68
+ if (!state || state.isComposing)
69
+ return;
70
+ switch (event.key) {
71
+ case 'Escape':
72
+ if (dialog.hasAttribute('open')) {
73
+ dialog.close();
74
+ event.preventDefault();
75
+ event.stopPropagation();
76
+ }
77
+ break;
78
+ }
79
+ }
80
+ function trackComposition(dialog, event) {
81
+ const state = states.get(dialog);
82
+ if (!state)
83
+ return;
84
+ state.isComposing = event.type === 'compositionstart';
85
+ }
package/package.json CHANGED
@@ -1,19 +1,24 @@
1
1
  {
2
2
  "name": "@primer/behaviors",
3
- "version": "0.0.0-2022027215135",
3
+ "version": "0.0.0-202202813730",
4
4
  "description": "Shared behaviors for JavaScript components",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
7
7
  "exports": {
8
8
  ".": {
9
- "types": "./dist/index.d.ts",
9
+ "types": "./dist/cjs/index.d.ts",
10
10
  "require": "./dist/cjs/index.js",
11
11
  "module": "./dist/esm/index.js"
12
12
  },
13
13
  "./utils": {
14
- "types": "./dist/utils/index.d.ts",
14
+ "types": "./dist/cjs/utils/index.d.ts",
15
15
  "require": "./dist/cjs/utils/index.js",
16
16
  "module": "./dist/esm/utils/index.js"
17
+ },
18
+ "./modal-dialog": {
19
+ "types": "./dist/cjs/components/modal-dialog.d.ts",
20
+ "require": "./dist/cjs/components/modal-dialog.js",
21
+ "module": "./dist/esm/components/modal-dialog.js"
17
22
  }
18
23
  },
19
24
  "types": "dist/cjs/index.d.ts",