@justeattakeaway/pie-webc-core 15.0.0 → 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,45 +1,49 @@
|
|
|
1
|
-
import { customElement as
|
|
2
|
-
const
|
|
3
|
-
const o = `#${
|
|
4
|
-
Object.defineProperty(
|
|
1
|
+
import { customElement as d } from "lit/decorators.js";
|
|
2
|
+
const p = (t, s, i) => function(n, r) {
|
|
3
|
+
const o = `#${r}`;
|
|
4
|
+
Object.defineProperty(n, r, {
|
|
5
5
|
get() {
|
|
6
6
|
return this[o];
|
|
7
7
|
},
|
|
8
|
-
set(
|
|
9
|
-
|
|
10
|
-
`<${t}> Invalid value "${
|
|
11
|
-
`Must be one of: ${
|
|
12
|
-
`Falling back to default value: "${
|
|
13
|
-
), this[o] =
|
|
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
|
-
},
|
|
18
|
-
const r = `#${
|
|
19
|
-
Object.defineProperty(
|
|
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[
|
|
21
|
+
return this[n];
|
|
22
22
|
},
|
|
23
|
-
set(
|
|
24
|
-
|
|
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 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
|
+
};
|
|
28
32
|
};
|
|
29
|
-
function
|
|
30
|
-
return (
|
|
33
|
+
function m(t) {
|
|
34
|
+
return (s) => {
|
|
31
35
|
try {
|
|
32
|
-
|
|
33
|
-
} catch (
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
`PIE Web Component: "${t}" was already registered with version: ${
|
|
37
|
-
|
|
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
|
|
38
42
|
);
|
|
39
43
|
}
|
|
40
44
|
};
|
|
41
45
|
}
|
|
42
|
-
function
|
|
46
|
+
function g(t) {
|
|
43
47
|
return new CustomEvent(t.type, {
|
|
44
48
|
detail: {
|
|
45
49
|
sourceEvent: t
|
|
@@ -48,25 +52,28 @@ function b(t) {
|
|
|
48
52
|
cancelable: t.cancelable
|
|
49
53
|
});
|
|
50
54
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
const
|
|
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, {
|
|
54
58
|
bubbles: !0,
|
|
55
59
|
composed: !0,
|
|
56
|
-
detail:
|
|
60
|
+
detail: i
|
|
57
61
|
});
|
|
58
|
-
t.dispatchEvent(
|
|
62
|
+
t.dispatchEvent(e);
|
|
63
|
+
}
|
|
64
|
+
function b() {
|
|
65
|
+
return typeof navigator < "u" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
59
66
|
}
|
|
60
|
-
const
|
|
61
|
-
class
|
|
67
|
+
const L = (t) => {
|
|
68
|
+
class s extends t {
|
|
62
69
|
constructor() {
|
|
63
70
|
super(...arguments), this._isRTL = !1, this.observer = null;
|
|
64
71
|
}
|
|
65
72
|
get isRTL() {
|
|
66
73
|
return this._isRTL;
|
|
67
74
|
}
|
|
68
|
-
set isRTL(
|
|
69
|
-
this._isRTL =
|
|
75
|
+
set isRTL(e) {
|
|
76
|
+
this._isRTL = e, this.requestUpdate();
|
|
70
77
|
}
|
|
71
78
|
connectedCallback() {
|
|
72
79
|
super.connectedCallback(), this.handleWritingDirectionUpdate(), this.observer = new MutationObserver(() => {
|
|
@@ -88,24 +95,24 @@ const m = (t) => {
|
|
|
88
95
|
this.isRTL = this.dir === "rtl" || document.documentElement.getAttribute("dir") === "rtl";
|
|
89
96
|
}
|
|
90
97
|
}
|
|
91
|
-
return
|
|
92
|
-
},
|
|
93
|
-
const
|
|
98
|
+
return s;
|
|
99
|
+
}, M = (t) => {
|
|
100
|
+
const i = class i extends t {
|
|
94
101
|
get form() {
|
|
95
102
|
return this._internals.form;
|
|
96
103
|
}
|
|
97
104
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
-
constructor(...
|
|
99
|
-
super(...
|
|
105
|
+
constructor(...n) {
|
|
106
|
+
super(...n), this._internals = this.attachInternals();
|
|
100
107
|
}
|
|
101
108
|
};
|
|
102
|
-
|
|
103
|
-
let
|
|
104
|
-
return
|
|
105
|
-
},
|
|
106
|
-
const
|
|
109
|
+
i.formAssociated = !0;
|
|
110
|
+
let s = i;
|
|
111
|
+
return s;
|
|
112
|
+
}, T = (t) => {
|
|
113
|
+
const i = class i extends t {
|
|
107
114
|
};
|
|
108
|
-
|
|
115
|
+
i.shadowRootOptions = {
|
|
109
116
|
// The `as any` cast is a safe way to access the static property
|
|
110
117
|
// from the generic superclass constructor. LitElement provides a default.
|
|
111
118
|
// This is unfortunately unavoidable and a common issue with mixins in TS.
|
|
@@ -113,16 +120,45 @@ const m = (t) => {
|
|
|
113
120
|
...t.shadowRootOptions,
|
|
114
121
|
delegatesFocus: !0
|
|
115
122
|
};
|
|
116
|
-
let
|
|
117
|
-
return
|
|
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;
|
|
118
152
|
};
|
|
119
153
|
export {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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,
|
|
161
|
+
m as safeCustomElement,
|
|
162
|
+
p as validPropertyValues,
|
|
163
|
+
g as wrapNativeEvent
|
|
128
164
|
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
|
+
const isMissing = <T>(value: T): boolean => value === undefined || value === null || (typeof value === 'string' && value.trim() === '');
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A decorator for marking a property as required.
|
|
3
5
|
* If the property's value is `undefined`, `null` or empty string, an error is logged.
|
|
6
|
+
*
|
|
7
|
+
* This also checks on `connectedCallback` so a missing required property is still reported
|
|
8
|
+
* even when the corresponding attribute is never set at all (in which case the property
|
|
9
|
+
* setter below is never invoked).
|
|
4
10
|
* @returns {Function} - The decorator function.
|
|
5
11
|
*/
|
|
6
12
|
export const requiredProperty = <T>(componentName: string) => function validateRequiredProperty (target: object, propertyKey: string): void {
|
|
7
13
|
const privatePropertyKey = `#${propertyKey}`;
|
|
14
|
+
const hasBeenSetKey = `#${propertyKey}HasBeenSet`;
|
|
15
|
+
const hasWarnedKey = `#${propertyKey}HasWarned`;
|
|
8
16
|
|
|
9
17
|
Object.defineProperty(target, propertyKey, {
|
|
10
18
|
get (): T {
|
|
11
19
|
return this[privatePropertyKey];
|
|
12
20
|
},
|
|
13
21
|
set (value: T): void {
|
|
14
|
-
|
|
22
|
+
this[hasBeenSetKey] = true;
|
|
23
|
+
|
|
24
|
+
if (isMissing(value)) {
|
|
15
25
|
console.error(`<${componentName}> Missing required attribute "${propertyKey}"`);
|
|
16
26
|
}
|
|
17
27
|
this[privatePropertyKey] = value;
|
|
18
28
|
},
|
|
19
29
|
configurable: true,
|
|
20
30
|
});
|
|
31
|
+
|
|
32
|
+
const elementProto = target as { connectedCallback?: (this: Record<string, unknown>) => void };
|
|
33
|
+
const originalConnectedCallback = elementProto.connectedCallback;
|
|
34
|
+
|
|
35
|
+
elementProto.connectedCallback = function connectedCallback (this: Record<string, unknown>): void {
|
|
36
|
+
originalConnectedCallback?.call(this);
|
|
37
|
+
|
|
38
|
+
// Guard with a dedicated "has warned" flag (rather than reusing hasBeenSetKey) so an
|
|
39
|
+
// element that reconnects repeatedly while still missing the prop (e.g. a virtualised
|
|
40
|
+
// list recycling DOM nodes) only logs once, instead of on every reconnect.
|
|
41
|
+
if (!this[hasBeenSetKey] && !this[hasWarnedKey]) {
|
|
42
|
+
this[hasWarnedKey] = true;
|
|
43
|
+
console.error(`<${componentName}> Missing required attribute "${propertyKey}"`);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
21
46
|
};
|
package/src/functions/index.ts
CHANGED
|
@@ -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
|
+
};
|
package/src/mixins/index.ts
CHANGED