@justeattakeaway/pie-webc-core 15.0.1 → 15.1.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/index.js CHANGED
@@ -1,49 +1,49 @@
1
1
  import { customElement as d } from "lit/decorators.js";
2
- const b = (t, e, s) => function(i, r) {
2
+ const p = (t, s, i) => function(n, r) {
3
3
  const o = `#${r}`;
4
- Object.defineProperty(i, r, {
4
+ Object.defineProperty(n, r, {
5
5
  get() {
6
6
  return this[o];
7
7
  },
8
- set(c) {
9
- e.includes(c) ? this[o] = c : (console.error(
10
- `<${t}> Invalid value "${c}" provided for property "${r}".`,
11
- `Must be one of: ${e.join(" | ")}.`,
12
- `Falling back to default value: "${s}"`
13
- ), this[o] = s);
8
+ set(a) {
9
+ s.includes(a) ? this[o] = a : (console.error(
10
+ `<${t}> Invalid value "${a}" provided for property "${r}".`,
11
+ `Must be one of: ${s.join(" | ")}.`,
12
+ `Falling back to default value: "${i}"`
13
+ ), this[o] = i);
14
14
  },
15
15
  configurable: !0
16
16
  });
17
- }, l = (t) => t == null || typeof t == "string" && t.trim() === "", f = (t) => function(s, n) {
18
- const i = `#${n}`, r = `#${n}HasBeenSet`, o = `#${n}HasWarned`;
19
- Object.defineProperty(s, n, {
17
+ }, u = (t) => t == null || typeof t == "string" && t.trim() === "", v = (t) => function(i, e) {
18
+ const n = `#${e}`, r = `#${e}HasBeenSet`, o = `#${e}HasWarned`;
19
+ Object.defineProperty(i, e, {
20
20
  get() {
21
- return this[i];
21
+ return this[n];
22
22
  },
23
- set(u) {
24
- this[r] = !0, l(u) && console.error(`<${t}> Missing required attribute "${n}"`), this[i] = u;
23
+ set(l) {
24
+ this[r] = !0, u(l) && console.error(`<${t}> Missing required attribute "${e}"`), this[n] = l;
25
25
  },
26
26
  configurable: !0
27
27
  });
28
- const c = s, a = c.connectedCallback;
29
- c.connectedCallback = function() {
30
- a == null || a.call(this), !this[r] && !this[o] && (this[o] = !0, console.error(`<${t}> Missing required attribute "${n}"`));
28
+ const a = i, c = a.connectedCallback;
29
+ a.connectedCallback = function() {
30
+ c == null || c.call(this), !this[r] && !this[o] && (this[o] = !0, console.error(`<${t}> Missing required attribute "${e}"`));
31
31
  };
32
32
  };
33
33
  function m(t) {
34
- return (e) => {
34
+ return (s) => {
35
35
  try {
36
- d(t)(e);
37
- } catch (s) {
38
- const n = customElements.get(t), i = n == null ? void 0 : n.v;
39
- e.v !== i && console.warn(
40
- `PIE Web Component: "${t}" was already registered with version: ${i ?? "No version data found. Icon components do not contain version data. If the component is not an icon, please report the missing version data."}.`,
41
- s
36
+ d(t)(s);
37
+ } catch (i) {
38
+ const e = customElements.get(t), n = e == null ? void 0 : e.v;
39
+ s.v !== n && console.warn(
40
+ `PIE Web Component: "${t}" was already registered with version: ${n ?? "No version data found. Icon components do not contain version data. If the component is not an icon, please report the missing version data."}.`,
41
+ i
42
42
  );
43
43
  }
44
44
  };
45
45
  }
46
- function v(t) {
46
+ function g(t) {
47
47
  return new CustomEvent(t.type, {
48
48
  detail: {
49
49
  sourceEvent: t
@@ -52,25 +52,28 @@ function v(t) {
52
52
  cancelable: t.cancelable
53
53
  });
54
54
  }
55
- function g(t, e, s) {
56
- e.startsWith("pie-") || console.warn("A custom event name should start with `pie-`");
57
- const n = new CustomEvent(e, {
55
+ function x(t, s, i) {
56
+ s.startsWith("pie-") || console.warn("A custom event name should start with `pie-`");
57
+ const e = new CustomEvent(s, {
58
58
  bubbles: !0,
59
59
  composed: !0,
60
- detail: s
60
+ detail: i
61
61
  });
62
- t.dispatchEvent(n);
62
+ t.dispatchEvent(e);
63
63
  }
64
- const p = (t) => {
65
- class e extends t {
64
+ function b() {
65
+ return typeof navigator < "u" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
66
+ }
67
+ const L = (t) => {
68
+ class s extends t {
66
69
  constructor() {
67
70
  super(...arguments), this._isRTL = !1, this.observer = null;
68
71
  }
69
72
  get isRTL() {
70
73
  return this._isRTL;
71
74
  }
72
- set isRTL(n) {
73
- this._isRTL = n, this.requestUpdate();
75
+ set isRTL(e) {
76
+ this._isRTL = e, this.requestUpdate();
74
77
  }
75
78
  connectedCallback() {
76
79
  super.connectedCallback(), this.handleWritingDirectionUpdate(), this.observer = new MutationObserver(() => {
@@ -92,24 +95,24 @@ const p = (t) => {
92
95
  this.isRTL = this.dir === "rtl" || document.documentElement.getAttribute("dir") === "rtl";
93
96
  }
94
97
  }
95
- return e;
96
- }, $ = (t) => {
97
- const s = class s extends t {
98
+ return s;
99
+ }, M = (t) => {
100
+ const i = class i extends t {
98
101
  get form() {
99
102
  return this._internals.form;
100
103
  }
101
104
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
- constructor(...i) {
103
- super(...i), this._internals = this.attachInternals();
105
+ constructor(...n) {
106
+ super(...n), this._internals = this.attachInternals();
104
107
  }
105
108
  };
106
- s.formAssociated = !0;
107
- let e = s;
108
- return e;
109
- }, w = (t) => {
110
- const s = class s extends t {
109
+ i.formAssociated = !0;
110
+ let s = i;
111
+ return s;
112
+ }, T = (t) => {
113
+ const i = class i extends t {
111
114
  };
112
- s.shadowRootOptions = {
115
+ i.shadowRootOptions = {
113
116
  // The `as any` cast is a safe way to access the static property
114
117
  // from the generic superclass constructor. LitElement provides a default.
115
118
  // This is unfortunately unavoidable and a common issue with mixins in TS.
@@ -117,16 +120,45 @@ const p = (t) => {
117
120
  ...t.shadowRootOptions,
118
121
  delegatesFocus: !0
119
122
  };
120
- let e = s;
121
- return e;
123
+ let s = i;
124
+ return s;
125
+ }, h = (t) => Array.from(t).map((i) => {
126
+ var e;
127
+ return ((e = i.textContent) == null ? void 0 : e.replace(/\s+/g, " ").trim()) ?? "";
128
+ }).filter(Boolean).join(" ").trim() || void 0, $ = (t) => {
129
+ class s extends t {
130
+ get associatedLabelText() {
131
+ return this._associatedLabelText;
132
+ }
133
+ firstUpdated(e) {
134
+ super.firstUpdated(e), this.observeAssociatedLabels();
135
+ }
136
+ disconnectedCallback() {
137
+ var e;
138
+ super.disconnectedCallback(), (e = this._labelMutationObserver) == null || e.disconnect();
139
+ }
140
+ updateAssociatedLabelText() {
141
+ this._associatedLabelText = h(this._internals.labels), this.requestUpdate();
142
+ }
143
+ observeAssociatedLabels() {
144
+ const { labels: e } = this._internals;
145
+ !b() || !e.length || (this.updateAssociatedLabelText(), this._labelMutationObserver = new MutationObserver(() => this.updateAssociatedLabelText()), e.forEach((n) => {
146
+ var r;
147
+ (r = this._labelMutationObserver) == null || r.observe(n, { childList: !0, characterData: !0, subtree: !0 });
148
+ }));
149
+ }
150
+ }
151
+ return s;
122
152
  };
123
153
  export {
124
- w as DelegatesFocusMixin,
125
- $ as FormControlMixin,
126
- p as RtlMixin,
127
- g as dispatchCustomEvent,
128
- f as requiredProperty,
154
+ $ as AssociatedLabelMixin,
155
+ T as DelegatesFocusMixin,
156
+ M as FormControlMixin,
157
+ L as RtlMixin,
158
+ x as dispatchCustomEvent,
159
+ b as isSafari,
160
+ v as requiredProperty,
129
161
  m as safeCustomElement,
130
- b as validPropertyValues,
131
- v as wrapNativeEvent
162
+ p as validPropertyValues,
163
+ g as wrapNativeEvent
132
164
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-webc-core",
3
- "version": "15.0.1",
3
+ "version": "15.1.0",
4
4
  "description": "PIE design system base classes, mixins and utilities for web components",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,2 +1,3 @@
1
1
  export * from './wrapNativeEvent';
2
2
  export * from './dispatchCustomEvent';
3
+ export * from './isSafari';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Detects whether the current browser is Safari.
3
+ */
4
+ export function isSafari (): boolean {
5
+ return typeof navigator !== 'undefined' && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
6
+ }
@@ -0,0 +1,106 @@
1
+ import { type LitElement, type PropertyValues } from 'lit';
2
+ import type { GenericConstructor } from '../types/GenericConstructor';
3
+ import { isSafari } from '../../functions/isSafari';
4
+
5
+ /**
6
+ * Joins and normalises the text content of a set of `<label>` elements into a single string.
7
+ */
8
+ const getLabelText = (labels: NodeList): string | undefined => {
9
+ const labelText = Array.from(labels)
10
+ .map((label) => label.textContent?.replace(/\s+/g, ' ').trim() ?? '')
11
+ .filter(Boolean)
12
+ .join(' ')
13
+ .trim();
14
+
15
+ return labelText || undefined;
16
+ };
17
+
18
+ /**
19
+ * An interface representing the structure of a class that has been extended
20
+ * with the AssociatedLabelMixin.
21
+ * @interface
22
+ */
23
+ export interface AssociatedLabelInterface {
24
+ /** The text content of the element's associated `<label>`(s) */
25
+ readonly associatedLabelText?: string;
26
+ }
27
+
28
+ /**
29
+ * A mixin that mirrors the text content of a form-associated custom element's associated
30
+ * `<label>`(s) onto an `associatedLabelText` property.
31
+ *
32
+ * Safari doesn't compute the accessible name of a form-associated
33
+ * custom element from its associated `<label>` the way others do, so we need to use
34
+ * `associatedLabelText` as a fallback `aria-label`.
35
+ * https://bugs.webkit.org/show_bug.cgi?id=259124
36
+ *
37
+ *
38
+ * The class this mixin is applied to must already provide `_internals: ElementInternals`
39
+ * (for example via `FormControlMixin`), and must call `super.firstUpdated()` if it overrides
40
+ * `firstUpdated`.
41
+ *
42
+ * @param superClass - The LitElement class (with `_internals`) to extend.
43
+ * @returns A new class extending both the provided LitElement and AssociatedLabelInterface.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { html } from 'lit';
48
+ * import { FormControlMixin, AssociatedLabelMixin } from '@justeattakeaway/pie-webc-core';
49
+ *
50
+ * class MyFormElement extends AssociatedLabelMixin(FormControlMixin(LitElement)) {
51
+ * render () {
52
+ * return html`<input aria-label="${this.associatedLabelText}">`;
53
+ * }
54
+ * }
55
+ * ```
56
+ */
57
+ export const AssociatedLabelMixin =
58
+ <T extends GenericConstructor<LitElement & { _internals: ElementInternals }>>(superClass: T) => {
59
+ /**
60
+ * Class representing a LitElement with associated label handling.
61
+ * @extends {LitElement}
62
+ * @implements {AssociatedLabelInterface}
63
+ */
64
+ class AssociatedLabelElement extends superClass implements AssociatedLabelInterface {
65
+ private _associatedLabelText?: string;
66
+
67
+ get associatedLabelText () {
68
+ return this._associatedLabelText;
69
+ }
70
+
71
+ private _labelMutationObserver?: MutationObserver;
72
+
73
+ protected firstUpdated (changedProperties: PropertyValues) {
74
+ super.firstUpdated(changedProperties);
75
+ this.observeAssociatedLabels();
76
+ }
77
+
78
+ disconnectedCallback () {
79
+ super.disconnectedCallback();
80
+ this._labelMutationObserver?.disconnect();
81
+ }
82
+
83
+ private updateAssociatedLabelText () : void {
84
+ this._associatedLabelText = getLabelText(this._internals.labels);
85
+ this.requestUpdate();
86
+ }
87
+
88
+ private observeAssociatedLabels () : void {
89
+ const { labels } = this._internals;
90
+
91
+ if (!isSafari() || !labels.length) {
92
+ return;
93
+ }
94
+
95
+ this.updateAssociatedLabelText();
96
+
97
+ this._labelMutationObserver = new MutationObserver(() => this.updateAssociatedLabelText());
98
+
99
+ labels.forEach((label) => {
100
+ this._labelMutationObserver?.observe(label, { childList: true, characterData: true, subtree: true });
101
+ });
102
+ }
103
+ }
104
+
105
+ return AssociatedLabelElement as GenericConstructor<AssociatedLabelInterface> & T;
106
+ };
@@ -1,5 +1,6 @@
1
1
  export * from './rtl/rtlMixin';
2
2
  export * from './formControl/formControlMixin';
3
3
  export * from './delegatesFocus/delegatesFocusMixin';
4
+ export * from './associatedLabel/associatedLabelMixin';
4
5
 
5
6
  export * from './types/GenericConstructor';