@justeattakeaway/pie-modal 0.9.0 → 0.10.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.
@@ -1,14 +1,14 @@
1
- [13:51:16] @custom-elements-manifest/analyzer: Created new manifest.
1
+ [2:59:55 PM] @custom-elements-manifest/analyzer: Created new manifest.
2
2
  react wrapper has been added!
3
3
  vite v4.3.9 building for production...
4
4
  transforming...
5
5
  ✓ 23 modules transformed.
6
6
  rendering chunks...
7
7
  computing gzip size...
8
- dist/index.js 12.03 kB │ gzip: 3.79 kB
8
+ dist/index.js 13.21 kB │ gzip: 4.16 kB
9
9
  dist/react.js 59.04 kB │ gzip: 15.92 kB
10
-
11
- [vite:dts] Start generate declaration files...
12
- ✓ built in 4.35s
13
- [vite:dts] Declaration files built in 3800ms.
14
-
10
+ 
11
+ [vite:dts] Start generate declaration files...
12
+ ✓ built in 10.22s
13
+ [vite:dts] Declaration files built in 9231ms.
14
+ 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @justeattakeaway/pie-modal
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [Added] - Add returnFocusAfterCloseSelector prop ([#599](https://github.com/justeattakeaway/pie/pull/599)) by [@xander-marjoram](https://github.com/xander-marjoram)
8
+
9
+ [Changed] - Extract `renderTestPieModal` into test helper file so it can be used for component tests too
10
+ [Added] - Add README documentation for new props
11
+
12
+ - [Fixed] - Fix console errors when switching between stories ([#599](https://github.com/justeattakeaway/pie/pull/599)) by [@xander-marjoram](https://github.com/xander-marjoram)
13
+
14
+ - [Added] - New `isFullWidthBelowMid` prop for pie-modal ([#600](https://github.com/justeattakeaway/pie/pull/600)) by [@LTurns](https://github.com/LTurns)
15
+
16
+ - [Added] isDismissible prop and tests ([#588](https://github.com/justeattakeaway/pie/pull/588)) by [@kevinrodrigues](https://github.com/kevinrodrigues)
17
+
3
18
  ## 0.9.0
4
19
 
5
20
  ### Minor Changes
package/README.md CHANGED
@@ -14,6 +14,7 @@
14
14
  2. [Local Development](#local-development)
15
15
  3. [Importing the component](#importing-the-component)
16
16
  4. [Props](#props)
17
+ 5. [Testing](#testing)
17
18
 
18
19
  # pie-modal
19
20
 
@@ -56,12 +57,19 @@ import { PieModal } from '@justeattakeaway/pie-modal/dist/react';
56
57
 
57
58
  ## Props
58
59
 
59
- | Property | Type | Default | Description |
60
- |----------------|-----------|---------|-------------------------------------------------------|
61
- | isOpen | `Boolean` | `false` | Controls if the modal element is open or closed |
62
- | heading* | `String` | - | Sets the heading of the modal |
63
- | headingLevel | `String` | `h2` | Allows you to set the heading tag (from `h1` to `h6`) |
64
-
60
+ | Property | Type | Default | Description |
61
+ |---|---|---|---|
62
+ | headingLevel | `String` | `h2` | |
63
+
64
+ | Property | Type | Default | Description |
65
+ |---|---|---|---|
66
+ | heading | `String` | n/a (Required) | The heading text of the modal |
67
+ | headingLevel | `String` | `h2` | The HTML tag to use for the modal's heading (can be `h1`-`h6`) |
68
+ | isDismissible | `Boolean` | `false` | If true, the modal includes a close button and can be dismissed by clicking on the backdrop or pressing the Esc key |
69
+ | isFullWidthBelowMid | `Boolean` | `false` | If true and the page is narrower than the mid breakpoint, a **medium-sized** modal will take up the full width of the screen. |
70
+ | isOpen | `Boolean` | `false` | Controls if the modal element is open or closed |
71
+ | size | `String` | `medium` | Determines the maximum width of the modal. Large modals will expand to fill the entire page width at narrow viewports. Can be `small`, `medium` or `large`. |
72
+ | returnFocusAfterCloseSelector | `String` | `undefined` | If provided, focus will be sent to the first element that matches this selector when the modal is closed. If not provided, the `dialog` element will return focus to the element that opened the modal. |
65
73
 
66
74
  In your markup or JSX, you can then use these to set the properties for the `pie-modal` component:
67
75
 
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
- import { unsafeCSS as C, LitElement as x } from "lit";
2
- import { unsafeStatic as E, html as L } from "lit/static-html.js";
3
- import { property as y, query as T } from "lit/decorators.js";
4
- import { property as $ } from "lit/decorators/property.js";
5
- var D = Object.defineProperty, B = Object.getOwnPropertyDescriptor, k = (i, e, t, n) => {
6
- for (var o = n > 1 ? void 0 : n ? B(e, t) : e, d = i.length - 1, a; d >= 0; d--)
7
- (a = i[d]) && (o = (n ? a(e, t, o) : a(o)) || o);
8
- return n && o && D(e, t, o), o;
1
+ import { nothing as x, unsafeCSS as D, LitElement as B } from "lit";
2
+ import { html as z, unsafeStatic as E } from "lit/static-html.js";
3
+ import { property as u, query as L } from "lit/decorators.js";
4
+ import { property as T } from "lit/decorators/property.js";
5
+ var $ = Object.defineProperty, F = Object.getOwnPropertyDescriptor, A = (n, e, t, o) => {
6
+ for (var i = o > 1 ? void 0 : o ? F(e, t) : e, d = n.length - 1, l; d >= 0; d--)
7
+ (l = n[d]) && (i = (o ? l(e, t, i) : l(i)) || i);
8
+ return o && i && $(e, t, i), i;
9
9
  };
10
- const R = (i) => {
11
- class e extends i {
10
+ const k = (n) => {
11
+ class e extends n {
12
12
  constructor() {
13
13
  super(...arguments), this.dir = "";
14
14
  }
@@ -22,168 +22,180 @@ const R = (i) => {
22
22
  * will not be reactive and is only computed once
23
23
  */
24
24
  get isRTL() {
25
- var n;
26
- return this.dir === "ltr" ? !1 : this.dir === "rtl" || ((n = window == null ? void 0 : window.getComputedStyle(this)) == null ? void 0 : n.direction) === "rtl";
25
+ var o;
26
+ return this.dir === "ltr" ? !1 : this.dir === "rtl" || ((o = window == null ? void 0 : window.getComputedStyle(this)) == null ? void 0 : o.direction) === "rtl";
27
27
  }
28
28
  }
29
- return k([
30
- $({ type: String })
29
+ return A([
30
+ T({ type: String })
31
31
  ], e.prototype, "dir", 2), e;
32
- }, S = (i, e, t) => function(o, d) {
33
- const a = `#${d}`;
34
- Object.defineProperty(o, d, {
32
+ }, C = (n, e, t) => function(i, d) {
33
+ const l = `#${d}`;
34
+ Object.defineProperty(i, d, {
35
35
  get() {
36
- return this[a];
36
+ return this[l];
37
37
  },
38
- set(f) {
39
- const m = this[a];
40
- e.includes(f) ? this[a] = f : (console.error(
41
- `<${i}> Invalid value "${f}" provided for property "${d}".`,
38
+ set(m) {
39
+ const p = this[l];
40
+ e.includes(m) ? this[l] = m : (console.error(
41
+ `<${n}> Invalid value "${m}" provided for property "${d}".`,
42
42
  `Must be one of: ${e.join(" | ")}.`,
43
43
  `Falling back to default value: "${t}"`
44
- ), this[a] = t), this.requestUpdate(d, m);
44
+ ), this[l] = t), this.requestUpdate(d, p);
45
45
  }
46
46
  });
47
- }, A = (i) => function(t, n) {
48
- const o = `#${n}`;
49
- Object.defineProperty(t, n, {
47
+ }, R = (n) => function(t, o) {
48
+ const i = `#${o}`;
49
+ Object.defineProperty(t, o, {
50
50
  get() {
51
- return this[o];
51
+ return this[i];
52
52
  },
53
53
  set(d) {
54
- const a = this[o];
55
- (d == null || typeof d == "string" && d.trim() === "") && console.error(`<${i}> Missing required attribute "${n}"`), this[o] = d, this.requestUpdate(n, a);
54
+ const l = this[i];
55
+ (d == null || typeof d == "string" && d.trim() === "") && console.error(`<${n}> Missing required attribute "${o}"`), this[i] = d, this.requestUpdate(o, l);
56
56
  }
57
57
  });
58
58
  };
59
- function q(i) {
60
- if (Array.isArray(i)) {
61
- for (var e = 0, t = Array(i.length); e < i.length; e++)
62
- t[e] = i[e];
59
+ function q(n) {
60
+ if (Array.isArray(n)) {
61
+ for (var e = 0, t = Array(n.length); e < n.length; e++)
62
+ t[e] = n[e];
63
63
  return t;
64
64
  } else
65
- return Array.from(i);
65
+ return Array.from(n);
66
66
  }
67
- var _ = !1;
67
+ var O = !1;
68
68
  if (typeof window < "u") {
69
- var z = {
69
+ var S = {
70
70
  get passive() {
71
- _ = !0;
71
+ O = !0;
72
72
  }
73
73
  };
74
- window.addEventListener("testPassive", null, z), window.removeEventListener("testPassive", null, z);
74
+ window.addEventListener("testPassive", null, S), window.removeEventListener("testPassive", null, S);
75
75
  }
76
- var p = typeof window < "u" && window.navigator && window.navigator.platform && (/iP(ad|hone|od)/.test(window.navigator.platform) || window.navigator.platform === "MacIntel" && window.navigator.maxTouchPoints > 1), r = [], v = !1, P = -1, c = void 0, l = void 0, u = void 0, M = function(e) {
77
- return r.some(function(t) {
76
+ var g = typeof window < "u" && window.navigator && window.navigator.platform && (/iP(ad|hone|od)/.test(window.navigator.platform) || window.navigator.platform === "MacIntel" && window.navigator.maxTouchPoints > 1), c = [], y = !1, P = -1, h = void 0, r = void 0, f = void 0, M = function(e) {
77
+ return c.some(function(t) {
78
78
  return !!(t.options.allowTouchMove && t.options.allowTouchMove(e));
79
79
  });
80
- }, g = function(e) {
80
+ }, b = function(e) {
81
81
  var t = e || window.event;
82
82
  return M(t.target) || t.touches.length > 1 ? !0 : (t.preventDefault && t.preventDefault(), !1);
83
83
  }, I = function(e) {
84
- if (u === void 0) {
85
- var t = !!e && e.reserveScrollBarGap === !0, n = window.innerWidth - document.documentElement.clientWidth;
86
- if (t && n > 0) {
87
- var o = parseInt(window.getComputedStyle(document.body).getPropertyValue("padding-right"), 10);
88
- u = document.body.style.paddingRight, document.body.style.paddingRight = o + n + "px";
84
+ if (f === void 0) {
85
+ var t = !!e && e.reserveScrollBarGap === !0, o = window.innerWidth - document.documentElement.clientWidth;
86
+ if (t && o > 0) {
87
+ var i = parseInt(window.getComputedStyle(document.body).getPropertyValue("padding-right"), 10);
88
+ f = document.body.style.paddingRight, document.body.style.paddingRight = i + o + "px";
89
89
  }
90
90
  }
91
- c === void 0 && (c = document.body.style.overflow, document.body.style.overflow = "hidden");
91
+ h === void 0 && (h = document.body.style.overflow, document.body.style.overflow = "hidden");
92
92
  }, V = function() {
93
- u !== void 0 && (document.body.style.paddingRight = u, u = void 0), c !== void 0 && (document.body.style.overflow = c, c = void 0);
93
+ f !== void 0 && (document.body.style.paddingRight = f, f = void 0), h !== void 0 && (document.body.style.overflow = h, h = void 0);
94
94
  }, j = function() {
95
95
  return window.requestAnimationFrame(function() {
96
- if (l === void 0) {
97
- l = {
96
+ if (r === void 0) {
97
+ r = {
98
98
  position: document.body.style.position,
99
99
  top: document.body.style.top,
100
100
  left: document.body.style.left
101
101
  };
102
- var e = window, t = e.scrollY, n = e.scrollX, o = e.innerHeight;
103
- document.body.style.position = "fixed", document.body.style.top = -t, document.body.style.left = -n, setTimeout(function() {
102
+ var e = window, t = e.scrollY, o = e.scrollX, i = e.innerHeight;
103
+ document.body.style.position = "fixed", document.body.style.top = -t, document.body.style.left = -o, setTimeout(function() {
104
104
  return window.requestAnimationFrame(function() {
105
- var d = o - window.innerHeight;
106
- d && t >= o && (document.body.style.top = -(t + d));
105
+ var d = i - window.innerHeight;
106
+ d && t >= i && (document.body.style.top = -(t + d));
107
107
  });
108
108
  }, 300);
109
109
  }
110
110
  });
111
- }, F = function() {
112
- if (l !== void 0) {
111
+ }, H = function() {
112
+ if (r !== void 0) {
113
113
  var e = -parseInt(document.body.style.top, 10), t = -parseInt(document.body.style.left, 10);
114
- document.body.style.position = l.position, document.body.style.top = l.top, document.body.style.left = l.left, window.scrollTo(t, e), l = void 0;
114
+ document.body.style.position = r.position, document.body.style.top = r.top, document.body.style.left = r.left, window.scrollTo(t, e), r = void 0;
115
115
  }
116
- }, H = function(e) {
116
+ }, W = function(e) {
117
117
  return e ? e.scrollHeight - e.scrollTop <= e.clientHeight : !1;
118
118
  }, N = function(e, t) {
119
- var n = e.targetTouches[0].clientY - P;
120
- return M(e.target) ? !1 : t && t.scrollTop === 0 && n > 0 || H(t) && n < 0 ? g(e) : (e.stopPropagation(), !0);
119
+ var o = e.targetTouches[0].clientY - P;
120
+ return M(e.target) ? !1 : t && t.scrollTop === 0 && o > 0 || W(t) && o < 0 ? b(e) : (e.stopPropagation(), !0);
121
121
  }, Y = function(e, t) {
122
122
  if (!e) {
123
123
  console.error("disableBodyScroll unsuccessful - targetElement must be provided when calling disableBodyScroll on IOS devices.");
124
124
  return;
125
125
  }
126
- if (!r.some(function(o) {
127
- return o.targetElement === e;
126
+ if (!c.some(function(i) {
127
+ return i.targetElement === e;
128
128
  })) {
129
- var n = {
129
+ var o = {
130
130
  targetElement: e,
131
131
  options: t || {}
132
132
  };
133
- r = [].concat(q(r), [n]), p ? j() : I(t), p && (e.ontouchstart = function(o) {
134
- o.targetTouches.length === 1 && (P = o.targetTouches[0].clientY);
135
- }, e.ontouchmove = function(o) {
136
- o.targetTouches.length === 1 && N(o, e);
137
- }, v || (document.addEventListener("touchmove", g, _ ? { passive: !1 } : void 0), v = !0));
133
+ c = [].concat(q(c), [o]), g ? j() : I(t), g && (e.ontouchstart = function(i) {
134
+ i.targetTouches.length === 1 && (P = i.targetTouches[0].clientY);
135
+ }, e.ontouchmove = function(i) {
136
+ i.targetTouches.length === 1 && N(i, e);
137
+ }, y || (document.addEventListener("touchmove", b, O ? { passive: !1 } : void 0), y = !0));
138
138
  }
139
139
  }, U = function(e) {
140
140
  if (!e) {
141
141
  console.error("enableBodyScroll unsuccessful - targetElement must be provided when calling enableBodyScroll on IOS devices.");
142
142
  return;
143
143
  }
144
- r = r.filter(function(t) {
144
+ c = c.filter(function(t) {
145
145
  return t.targetElement !== e;
146
- }), p && (e.ontouchstart = null, e.ontouchmove = null, v && r.length === 0 && (document.removeEventListener("touchmove", g, _ ? { passive: !1 } : void 0), v = !1)), p ? F() : V();
146
+ }), g && (e.ontouchstart = null, e.ontouchmove = null, y && c.length === 0 && (document.removeEventListener("touchmove", b, O ? { passive: !1 } : void 0), y = !1)), g ? H() : V();
147
147
  };
148
- const X = `.c-modal{--modal-size-s: 450px;--modal-size-m: 600px;--modal-size-l: 1080px;--modal-border-radius: var(--dt-radius-rounded-d);--modal-font: var(--dt-font-interactive-m-family);--modal-bg-color: var(--dt-color-container-default);--modal-elevation: var(--dt-elevation-04);border-radius:var(--modal-border-radius);border:none;box-shadow:var(--modal-elevation);font-family:var(--modal-font);background-color:var(--modal-bg-color);padding:0;--modal-max-inline-size: var(--modal-size-m);--modal-inline-size: 75%;max-inline-size:var(--modal-max-inline-size);inline-size:var(--modal-inline-size)}.c-modal[size=small]{--modal-max-inline-size: var(--modal-size-s)}.c-modal[size=large]{--modal-max-inline-size: var(--modal-size-l);--modal-inline-size: 100%}@media (min-width: 768px){.c-modal[size=large]{--modal-inline-size: 75%}}.c-modal::backdrop{background:rgba(0,0,0,.5)}.c-modal .c-modal-heading{--modal-header-font-size: calc(var(--dt-font-heading-m-size--wide) * 1px);--modal-header-font-line-height: calc(var(--dt-font-heading-m-line-height--wide) * 1px);--modal-header-font-weight: var(--dt-font-heading-m-weight);--modal-header-padding: var(--dt-spacing-e);--modal-header-padding-block-end: var(--dt-spacing-d);font-size:var(--modal-header-font-size);line-height:var(--modal-header-font-line-height);font-weight:var(--modal-header-font-weight);margin:0;padding-block:var(--modal-header-padding) var(--modal-header-padding-block-end);padding-inline:var(--modal-header-padding)}.c-modal .c-modal-content{--modal-content-font-size: calc(var(--dt-font-size-16) * 1px);--modal-content-font-weight: var(--dt-font-weight-regular);--modal-content-line-height: calc(var(--dt-font-size-16-line-height) * 1px);--modal-content-padding: var(--dt-spacing-e);--modal-content-padding-block-start: var(--dt-spacing-a);font-size:var(--modal-content-font-size);line-height:var(--modal-content-line-height);font-weight:var(--modal-content-font-weight);padding-block:var(--modal-content-padding-block-start) var(--modal-content-padding);padding-inline:var(--modal-content-padding);overflow-y:scroll;max-block-size:300px}.c-modal .c-modal-closeBtn{position:absolute;inset-inline-end:var(--dt-spacing-d);inset-block-start:var(--dt-spacing-d)}
149
- `, W = ["h1", "h2", "h3", "h4", "h5", "h6"], G = ["small", "medium", "large"], w = "pie-modal-close", O = "pie-modal-open";
150
- var J = Object.defineProperty, Q = Object.getOwnPropertyDescriptor, h = (i, e, t, n) => {
151
- for (var o = n > 1 ? void 0 : n ? Q(e, t) : e, d = i.length - 1, a; d >= 0; d--)
152
- (a = i[d]) && (o = (n ? a(e, t, o) : a(o)) || o);
153
- return n && o && J(e, t, o), o;
148
+ const X = `.c-modal{--modal-size-s: 450px;--modal-size-m: 600px;--modal-size-l: 1080px;--modal-border-radius: var(--dt-radius-rounded-d);--modal-font: var(--dt-font-interactive-m-family);--modal-bg-color: var(--dt-color-container-default);--modal-elevation: var(--dt-elevation-04);border-radius:var(--modal-border-radius);border:none;box-shadow:var(--modal-elevation);font-family:var(--modal-font);background-color:var(--modal-bg-color);padding:0;--modal-max-inline-size: var(--modal-size-m);--modal-inline-size: 75%;max-inline-size:var(--modal-max-inline-size);inline-size:var(--modal-inline-size)}.c-modal[size=small]{--modal-max-inline-size: var(--modal-size-s)}@media (max-width: 768px){.c-modal[size=medium][isfullwidthbelowmid]{--modal-inline-size: 100%}}.c-modal[size=large]{--modal-max-inline-size: var(--modal-size-l);--modal-inline-size: 100%}@media (min-width: 768px){.c-modal[size=large]{--modal-inline-size: 75%}}.c-modal::backdrop{background:rgba(0,0,0,.5)}.c-modal .c-modal-heading{--modal-header-font-size: calc(var(--dt-font-heading-m-size--wide) * 1px);--modal-header-font-line-height: calc(var(--dt-font-heading-m-line-height--wide) * 1px);--modal-header-font-weight: var(--dt-font-heading-m-weight);--modal-header-padding: var(--dt-spacing-e);--modal-header-padding-block-end: var(--dt-spacing-d);font-size:var(--modal-header-font-size);line-height:var(--modal-header-font-line-height);font-weight:var(--modal-header-font-weight);margin:0;padding-block:var(--modal-header-padding) var(--modal-header-padding-block-end);padding-inline:var(--modal-header-padding)}.c-modal .c-modal-content{--modal-content-font-size: calc(var(--dt-font-size-16) * 1px);--modal-content-font-weight: var(--dt-font-weight-regular);--modal-content-line-height: calc(var(--dt-font-size-16-line-height) * 1px);--modal-content-padding: var(--dt-spacing-e);--modal-content-padding-block-start: var(--dt-spacing-a);font-size:var(--modal-content-font-size);line-height:var(--modal-content-line-height);font-weight:var(--modal-content-font-weight);padding-block:var(--modal-content-padding-block-start) var(--modal-content-padding);padding-inline:var(--modal-content-padding);overflow-y:scroll;max-block-size:300px}.c-modal .c-modal-closeBtn{position:absolute;inset-inline-end:var(--dt-spacing-d);inset-block-start:var(--dt-spacing-d)}
149
+ `, G = ["h1", "h2", "h3", "h4", "h5", "h6"], J = ["small", "medium", "large"], _ = "pie-modal-close", v = "pie-modal-open";
150
+ var Q = Object.defineProperty, Z = Object.getOwnPropertyDescriptor, s = (n, e, t, o) => {
151
+ for (var i = o > 1 ? void 0 : o ? Z(e, t) : e, d = n.length - 1, l; d >= 0; d--)
152
+ (l = n[d]) && (i = (o ? l(e, t, i) : l(i)) || i);
153
+ return o && i && Q(e, t, i), i;
154
154
  };
155
- const b = "pie-modal";
156
- class s extends R(x) {
155
+ const w = "pie-modal";
156
+ class a extends k(B) {
157
157
  constructor() {
158
- super(), this.isOpen = !1, this.headingLevel = "h2", this.size = "medium", this._triggerCloseModal = () => {
159
- this.isOpen = !1;
158
+ super(), this.headingLevel = "h2", this.isDismissible = !1, this.isFullWidthBelowMid = !1, this.isOpen = !1, this.size = "medium", this._handleDialogCancelEvent = (e) => {
159
+ this.isDismissible || e.preventDefault();
160
160
  }, this._handleDialogLightDismiss = (e) => {
161
- var m;
162
- const t = (m = this._dialog) == null ? void 0 : m.getBoundingClientRect(), {
163
- top: n = 0,
164
- bottom: o = 0,
161
+ var p;
162
+ if (!this.isDismissible)
163
+ return;
164
+ const t = (p = this._dialog) == null ? void 0 : p.getBoundingClientRect(), {
165
+ top: o = 0,
166
+ bottom: i = 0,
165
167
  left: d = 0,
166
- right: a = 0
168
+ right: l = 0
167
169
  } = t || {};
168
- if (n === 0 && o === 0 && d === 0 && a === 0)
170
+ if (o === 0 && i === 0 && d === 0 && l === 0)
169
171
  return;
170
- (e.clientY < n || e.clientY > o || e.clientX < d || e.clientX > a) && (this.isOpen = !1);
171
- }, this._dispatchModalCloseEvent = () => {
172
- const e = new CustomEvent(w, {
173
- bubbles: !0,
174
- composed: !0
175
- });
176
- this.dispatchEvent(e);
177
- }, this._dispatchModalOpenEvent = () => {
178
- const e = new CustomEvent(O, {
172
+ (e.clientY < o || e.clientY > i || e.clientX < d || e.clientX > l) && (this.isOpen = !1);
173
+ }, this._dispatchModalCustomEvent = (e) => {
174
+ const t = new CustomEvent(e, {
179
175
  bubbles: !0,
180
176
  composed: !0
181
177
  });
182
- this.dispatchEvent(e);
183
- }, this.addEventListener("click", (e) => this._handleDialogLightDismiss(e));
178
+ this.dispatchEvent(t);
179
+ }, this.renderCloseButton = () => z`
180
+ <pie-icon-button
181
+ @click="${() => {
182
+ this.isOpen = !1;
183
+ }}"
184
+ variant="ghost-secondary"
185
+ class="c-modal-closeBtn"
186
+ data-test-id="modal-close-button"></pie-icon-button>`, this.addEventListener("click", (e) => this._handleDialogLightDismiss(e));
187
+ }
188
+ connectedCallback() {
189
+ super.connectedCallback(), document.addEventListener(v, this._handleModalOpened.bind(this)), document.addEventListener(_, this._handleModalClosed.bind(this));
190
+ }
191
+ disconnectedCallback() {
192
+ document.removeEventListener(v, this._handleModalOpened.bind(this)), document.removeEventListener(_, this._handleModalClosed.bind(this)), super.disconnectedCallback();
184
193
  }
185
194
  firstUpdated(e) {
186
- this._handleModalOpenStateOnFirstRender(e);
195
+ var t, o;
196
+ (t = this._dialog) == null || t.addEventListener("cancel", (i) => this._handleDialogCancelEvent(i)), this._handleModalOpenStateOnFirstRender(e), (o = this._dialog) == null || o.addEventListener("close", () => {
197
+ this.isOpen = !1;
198
+ });
187
199
  }
188
200
  updated(e) {
189
201
  this._handleModalOpenStateChanged(e);
@@ -192,48 +204,51 @@ class s extends R(x) {
192
204
  * Opens the dialog element and disables page scrolling
193
205
  */
194
206
  _handleModalOpened() {
195
- var e;
196
- Y(this), (e = this._dialog) == null || e.showModal();
207
+ var e, t, o;
208
+ Y(this), !((e = this._dialog) != null && e.hasAttribute("open") || !((t = this._dialog) != null && t.isConnected)) && ((o = this._dialog) == null || o.showModal());
197
209
  }
198
210
  /**
199
211
  * Closes the dialog element and re-enables page scrolling
200
212
  */
201
213
  _handleModalClosed() {
202
214
  var e;
203
- U(this), (e = this._dialog) == null || e.close();
204
- }
205
- connectedCallback() {
206
- super.connectedCallback(), document.addEventListener(O, this._handleModalOpened.bind(this)), document.addEventListener(w, this._handleModalClosed.bind(this));
207
- }
208
- disconnectedCallback() {
209
- document.removeEventListener(O, this._handleModalOpened.bind(this)), document.removeEventListener(w, this._handleModalClosed.bind(this)), super.disconnectedCallback();
215
+ U(this), (e = this._dialog) == null || e.close(), this._returnFocus();
210
216
  }
211
217
  // Handles the value of the isOpen property on first render of the component
212
218
  _handleModalOpenStateOnFirstRender(e) {
213
- e.get("isOpen") === void 0 && this.isOpen && this._dispatchModalOpenEvent();
219
+ e.get("isOpen") === void 0 && this.isOpen && this._dispatchModalCustomEvent(v);
214
220
  }
215
221
  // Handles changes to the modal isOpen property by dispatching any appropriate events
216
222
  _handleModalOpenStateChanged(e) {
217
223
  const t = e.get("isOpen");
218
- t !== void 0 && (t ? this._dispatchModalCloseEvent() : this._dispatchModalOpenEvent());
224
+ t !== void 0 && (t ? this._dispatchModalCustomEvent(_) : this._dispatchModalCustomEvent(v));
225
+ }
226
+ /**
227
+ * Return focus to the specified element, providing the selector is valid
228
+ * and the chosen element can be found.
229
+ * Fails silently.
230
+ */
231
+ _returnFocus() {
232
+ var t, o;
233
+ const e = (t = this.returnFocusAfterCloseSelector) == null ? void 0 : t.trim();
234
+ e && ((o = document.querySelector(e)) == null || o.focus());
219
235
  }
220
236
  render() {
221
237
  const {
222
238
  heading: e,
223
239
  headingLevel: t = "h2",
224
- size: n
225
- } = this, o = E(t);
226
- return L`
240
+ size: o,
241
+ isFullWidthBelowMid: i
242
+ } = this, d = E(t);
243
+ return z`
227
244
  <dialog
228
245
  id="dialog"
229
- size="${n}"
230
- class="c-modal">
246
+ class="c-modal"
247
+ size="${o}"
248
+ ?isFullWidthBelowMid=${i}>
231
249
  <header>
232
- <${o} class="c-modal-heading">${e}</${o}>
233
- <pie-icon-button
234
- @click="${this._triggerCloseModal}"
235
- variant="ghost-secondary"
236
- class="c-modal-closeBtn"></pie-icon-button>
250
+ <${d} class="c-modal-heading">${e}</${d}>
251
+ ${this.isDismissible ? this.renderCloseButton() : x}
237
252
  </header>
238
253
  <article class="c-modal-content">
239
254
  <slot></slot>
@@ -242,28 +257,37 @@ class s extends R(x) {
242
257
  `;
243
258
  }
244
259
  }
245
- s.styles = C(X);
246
- h([
247
- y({ type: Boolean })
248
- ], s.prototype, "isOpen", 2);
249
- h([
250
- y({ type: String }),
251
- A(b)
252
- ], s.prototype, "heading", 2);
253
- h([
254
- y(),
255
- S(b, W, "h2")
256
- ], s.prototype, "headingLevel", 2);
257
- h([
258
- y(),
259
- S(b, G, "medium")
260
- ], s.prototype, "size", 2);
261
- h([
262
- T("dialog")
263
- ], s.prototype, "_dialog", 2);
264
- customElements.define(b, s);
260
+ a.styles = D(X);
261
+ s([
262
+ u({ type: String }),
263
+ R(w)
264
+ ], a.prototype, "heading", 2);
265
+ s([
266
+ u(),
267
+ C(w, G, "h2")
268
+ ], a.prototype, "headingLevel", 2);
269
+ s([
270
+ u({ type: Boolean, reflect: !0 })
271
+ ], a.prototype, "isDismissible", 2);
272
+ s([
273
+ u({ type: Boolean })
274
+ ], a.prototype, "isFullWidthBelowMid", 2);
275
+ s([
276
+ u({ type: Boolean })
277
+ ], a.prototype, "isOpen", 2);
278
+ s([
279
+ u()
280
+ ], a.prototype, "returnFocusAfterCloseSelector", 2);
281
+ s([
282
+ u(),
283
+ C(w, J, "medium")
284
+ ], a.prototype, "size", 2);
285
+ s([
286
+ L("dialog")
287
+ ], a.prototype, "_dialog", 2);
288
+ customElements.define(w, a);
265
289
  export {
266
- s as PieModal,
267
- W as headingLevels,
268
- G as sizes
290
+ a as PieModal,
291
+ G as headingLevels,
292
+ J as sizes
269
293
  };
package/dist/react.js CHANGED
File without changes
File without changes
@@ -13,6 +13,26 @@ export interface ModalProps {
13
13
  * When true, the modal will be open.
14
14
  */
15
15
  isOpen: boolean;
16
+ /**
17
+ * When set to `true`:
18
+ * 1. The close button within the modal will be visible.
19
+ * 2. The user can dismiss the modal via the ESCAPE key, clicking the backdrop
20
+ * or via a close button.
21
+ *
22
+ * When set to `false`:
23
+ * 1. The close button within the modal will be hidden.
24
+ * 2. The user can NOT dismiss the modal via the ESCAPE key or clicking the backdrop.
25
+ *
26
+ */
27
+ isDismissible: boolean;
28
+ /**
29
+ * This controls whether a *medium-sized* modal will cover the full width of the page when below the mid breakpoint.
30
+ */
31
+ isFullWidthBelowMid: boolean;
32
+ /**
33
+ * The selector for the element that you would like focus to be returned to when the modal is closed, e.g., #skipToMain
34
+ */
35
+ returnFocusAfterCloseSelector?: string;
16
36
  /**
17
37
  * The size of the modal; this controls how wide it will appear on the page.
18
38
  */
@@ -1 +1 @@
1
- {"version":3,"file":"defs.d.ts","sourceRoot":"","sources":["../../../src/defs.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,+CAAgD,CAAC;AAC3E,eAAO,MAAM,KAAK,uCAAwC,CAAC;AAE3D,MAAM,WAAW,UAAU;IACvB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,YAAY,EAAE,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC;IAC3C;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,mBAAmB,CAAC"}
1
+ {"version":3,"file":"defs.d.ts","sourceRoot":"","sources":["../../../src/defs.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,+CAAgD,CAAC;AAC3E,eAAO,MAAM,KAAK,uCAAwC,CAAC;AAE3D,MAAM,WAAW,UAAU;IACvB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,YAAY,EAAE,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC;IAE3C;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;;;;;;;;;OAUG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB;;OAEG;IACH,mBAAmB,EAAE,OAAO,CAAC;IAE7B;;OAEG;IACH,6BAA6B,CAAC,EAAE,MAAM,CAAC;IAEvC;;OAEG;IACH,IAAI,EAAE,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,mBAAmB,CAAC"}
@@ -1,4 +1,4 @@
1
- import { LitElement } from 'lit';
1
+ import { LitElement, TemplateResult } from 'lit';
2
2
  import type { DependentMap } from '@justeattakeaway/pie-webc-core';
3
3
  import { ModalProps, headingLevels, sizes } from './defs';
4
4
  export { type ModalProps, headingLevels, sizes };
@@ -8,12 +8,17 @@ declare const PieModal_base: (new (...args: any[]) => {
8
8
  isRTL: boolean;
9
9
  }) & typeof LitElement;
10
10
  export declare class PieModal extends PieModal_base {
11
- isOpen: boolean;
12
11
  heading: string;
13
12
  headingLevel: ModalProps['headingLevel'];
13
+ isDismissible: boolean;
14
+ isFullWidthBelowMid: boolean;
15
+ isOpen: boolean;
16
+ returnFocusAfterCloseSelector?: string;
14
17
  size: ModalProps['size'];
15
18
  _dialog?: HTMLDialogElement;
16
19
  constructor();
20
+ connectedCallback(): void;
21
+ disconnectedCallback(): void;
17
22
  firstUpdated(changedProperties: DependentMap<ModalProps>): void;
18
23
  updated(changedProperties: DependentMap<ModalProps>): void;
19
24
  /**
@@ -25,34 +30,45 @@ export declare class PieModal extends PieModal_base {
25
30
  */
26
31
  private _handleModalClosed;
27
32
  /**
28
- * This is only to be used inside the component template as direct property
29
- * reassignment is not allowed.
33
+ * Prevents the user from dismissing the dialog via the `cancel`
34
+ * event (ESC key) when `isDismissible` is set to false.
35
+ *
36
+ * @param {Event} event - The event object.
30
37
  */
31
- private _triggerCloseModal;
32
- connectedCallback(): void;
33
- disconnectedCallback(): void;
38
+ private _handleDialogCancelEvent;
34
39
  private _handleModalOpenStateOnFirstRender;
35
40
  private _handleModalOpenStateChanged;
36
- render(): import("lit-html").TemplateResult;
37
41
  /**
38
- * Dismisses the modal on backdrop click
39
- *
42
+ * Return focus to the specified element, providing the selector is valid
43
+ * and the chosen element can be found.
44
+ * Fails silently.
45
+ */
46
+ private _returnFocus;
47
+ render(): TemplateResult;
48
+ /**
49
+ * Dismisses the modal on backdrop click if `isDismissible` is `true`.
50
+ * @param {MouseEvent} event - the click event targetting the modal/backdrop
40
51
  */
41
52
  private _handleDialogLightDismiss;
42
53
  /**
43
- * Dispatch `ON_MODAL_CLOSE_EVENT` event.
44
- * To be used whenever we close the modal.
54
+ * Note: We should aim to have a shareable event helper system to allow
55
+ * us to share this across components in-future.
56
+ *
57
+ * Dispatch a custom event.
58
+ *
59
+ * To be used whenever we have behavioural events we want to
60
+ * bubble up through the modal.
45
61
  *
46
- * @event
62
+ * @param {string} eventType
47
63
  */
48
- private _dispatchModalCloseEvent;
64
+ private _dispatchModalCustomEvent;
49
65
  /**
50
- * Dispatch `ON_MODAL_OPEN_EVENT` event.
51
- * To be used whenever we open the modal.
66
+ * Template for the close button element. Called within the
67
+ * main render function.
52
68
  *
53
- * @event
69
+ * @private
54
70
  */
55
- private _dispatchModalOpenEvent;
71
+ private renderCloseButton;
56
72
  static styles: import("lit").CSSResult;
57
73
  }
58
74
  declare global {