@justeattakeaway/pie-webc-core 0.18.0 → 0.19.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,7 +1,7 @@
1
1
  vite v4.5.2 building for production...
2
2
  transforming...
3
- ✓ 13 modules transformed.
3
+ ✓ 14 modules transformed.
4
4
  rendering chunks...
5
5
  computing gzip size...
6
- dist/index.js 2.41 kB │ gzip: 1.10 kB
7
- ✓ built in 229ms
6
+ dist/index.js 2.66 kB │ gzip: 1.20 kB
7
+ ✓ built in 198ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.19.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [Added] - dispatchCustomEvent helper function. ([#1272](https://github.com/justeattakeaway/pie/pull/1272)) by [@dandel10n](https://github.com/dandel10n)
8
+
3
9
  ## 0.18.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -1,45 +1,54 @@
1
- import { isServer as l } from "lit";
2
- const a = (e, t, n) => function(o, r) {
3
- const s = `#${r}`;
4
- Object.defineProperty(o, r, {
1
+ import { isServer as c } from "lit";
2
+ const a = (t, e, r) => function(s, n) {
3
+ const o = `#${n}`;
4
+ Object.defineProperty(s, n, {
5
5
  get() {
6
- return this[s];
6
+ return this[o];
7
7
  },
8
- set(c) {
9
- t.includes(c) ? this[s] = c : (console.error(
10
- `<${e}> Invalid value "${c}" provided for property "${r}".`,
11
- `Must be one of: ${t.join(" | ")}.`,
12
- `Falling back to default value: "${n}"`
13
- ), this[s] = n);
8
+ set(u) {
9
+ e.includes(u) ? this[o] = u : (console.error(
10
+ `<${t}> Invalid value "${u}" provided for property "${n}".`,
11
+ `Must be one of: ${e.join(" | ")}.`,
12
+ `Falling back to default value: "${r}"`
13
+ ), this[o] = r);
14
14
  },
15
15
  configurable: !0
16
16
  });
17
- }, d = (e) => function(n, i) {
18
- const o = `#${i}`;
19
- Object.defineProperty(n, i, {
17
+ }, d = (t) => function(r, i) {
18
+ const s = `#${i}`;
19
+ Object.defineProperty(r, i, {
20
20
  get() {
21
- return this[o];
21
+ return this[s];
22
22
  },
23
- set(r) {
24
- (r == null || typeof r == "string" && r.trim() === "") && console.error(`<${e}> Missing required attribute "${i}"`), this[o] = r;
23
+ set(n) {
24
+ (n == null || typeof n == "string" && n.trim() === "") && console.error(`<${t}> Missing required attribute "${i}"`), this[s] = n;
25
25
  },
26
26
  configurable: !0
27
27
  });
28
28
  };
29
- function f(e, t) {
30
- customElements.get(e) ? console.warn(`PIE Web Component: "${e}" has already been defined. Please ensure the component is only being defined once in your application.`) : customElements.define(e, t);
29
+ function f(t, e) {
30
+ customElements.get(t) ? console.warn(`PIE Web Component: "${t}" has already been defined. Please ensure the component is only being defined once in your application.`) : customElements.define(t, e);
31
31
  }
32
- function m(e) {
33
- return new CustomEvent(e.type, {
32
+ function m(t) {
33
+ return new CustomEvent(t.type, {
34
34
  detail: {
35
- sourceEvent: e
35
+ sourceEvent: t
36
36
  },
37
- bubbles: e.bubbles,
38
- cancelable: e.cancelable
37
+ bubbles: t.bubbles,
38
+ cancelable: t.cancelable
39
+ });
40
+ }
41
+ function b(t, e, r) {
42
+ e.startsWith("pie-") || console.warn("A custom event name should start with `pie-`");
43
+ const i = new CustomEvent(e, {
44
+ bubbles: !0,
45
+ composed: !0,
46
+ detail: r
39
47
  });
48
+ t.dispatchEvent(i);
40
49
  }
41
- const b = (e) => {
42
- class t extends e {
50
+ const h = (t) => {
51
+ class e extends t {
43
52
  /**
44
53
  * A getter to determine whether the text direction is right-to-left (RTL).
45
54
  * If the `dir` property is present on the component, it will be used to determine the text direction.
@@ -50,12 +59,12 @@ const b = (e) => {
50
59
  * @returns {boolean} - Returns `true` if the text direction is RTL, otherwise `false`.
51
60
  */
52
61
  get isRTL() {
53
- return this.dir ? this.dir === "rtl" : !l && !this.dir ? document.documentElement.getAttribute("dir") === "rtl" : !1;
62
+ return this.dir ? this.dir === "rtl" : !c && !this.dir ? document.documentElement.getAttribute("dir") === "rtl" : !1;
54
63
  }
55
64
  }
56
- return t;
57
- }, p = (e) => {
58
- class t extends e {
65
+ return e;
66
+ }, p = (t) => {
67
+ class e extends t {
59
68
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
69
  constructor(...i) {
61
70
  super(...i), this._internals = this.attachInternals();
@@ -64,12 +73,13 @@ const b = (e) => {
64
73
  return this._internals.form;
65
74
  }
66
75
  }
67
- return t.formAssociated = !0, t;
76
+ return e.formAssociated = !0, e;
68
77
  };
69
78
  export {
70
79
  p as FormControlMixin,
71
- b as RtlMixin,
80
+ h as RtlMixin,
72
81
  f as defineCustomElement,
82
+ b as dispatchCustomEvent,
73
83
  d as requiredProperty,
74
84
  a as validPropertyValues,
75
85
  m as wrapNativeEvent
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-webc-core",
3
- "version": "0.18.0",
3
+ "version": "0.19.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",
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Dispatch a custom event.
3
+ *
4
+ * To be used whenever we have custom events we want to
5
+ * bubble up through the component.
6
+ * Bubbles and composed event props are set to true.
7
+ * @param {Element} element The element to dispatch the event from.
8
+ * @param {string} eventType
9
+ * @param {any} detail
10
+ */
11
+ export function dispatchCustomEvent (element: Element, eventType: string, detail?: CustomEventInit['detail']) : void {
12
+ if (!eventType.startsWith('pie-')) {
13
+ console.warn('A custom event name should start with `pie-`');
14
+ }
15
+ const event = new CustomEvent(eventType, {
16
+ bubbles: true,
17
+ composed: true,
18
+ detail,
19
+ });
20
+
21
+ element.dispatchEvent(event);
22
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './defineCustomElement';
2
2
  export * from './wrapNativeEvent';
3
+ export * from './dispatchCustomEvent';
@@ -0,0 +1,28 @@
1
+ import { LitElement, html } from 'lit';
2
+ import { property } from 'lit/decorators.js';
3
+ import { defineCustomElement } from '../../../functions/defineCustomElement.js';
4
+ import { dispatchCustomEvent } from '../../../functions/dispatchCustomEvent.js';
5
+
6
+ const componentSelector = 'dispatch-custom-event-mock';
7
+
8
+ export class MockComponent extends LitElement {
9
+ @property({ type: String })
10
+ public eventName = 'pie-mock-event';
11
+
12
+ render () {
13
+ return html`
14
+ <button
15
+ @click="${() => dispatchCustomEvent(this, this.eventName)}"
16
+ data-test-id="dispatch-custom-event-mock">
17
+ Component with the event to dispatch
18
+ </button>`;
19
+ }
20
+ }
21
+
22
+ declare global {
23
+ interface HTMLElementTagNameMap {
24
+ [componentSelector]: MockComponent;
25
+ }
26
+ }
27
+
28
+ defineCustomElement(componentSelector, MockComponent);
@@ -0,0 +1,45 @@
1
+ import { test, expect } from '@sand4rt/experimental-ct-web';
2
+ import { MockComponent } from './MockComponent.ts';
3
+
4
+ test.describe('dispatchCustomEvent function', () => {
5
+ test('should dispatch an event when clicked', async ({ mount, page }) => {
6
+ // Arrange
7
+ const events : Array<Event> = [];
8
+
9
+ await mount(MockComponent, {
10
+ on: {
11
+ 'pie-mock-event': (event: Event) => events.push(event),
12
+ },
13
+ });
14
+
15
+ // Act
16
+ await page.locator('[data-test-id="dispatch-custom-event-mock"]').click();
17
+
18
+ // Assert
19
+ expect(events).toHaveLength(1);
20
+ });
21
+
22
+ test('should call console.warn when event name do not start with "pie-"', async ({ mount, page }) => {
23
+ // Arrange
24
+ let result = '';
25
+ const expected = 'A custom event name should start with `pie-`';
26
+
27
+ await mount(MockComponent, {
28
+ props: {
29
+ eventName: 'mock-event',
30
+ },
31
+ });
32
+
33
+ page.on('console', (msg) => {
34
+ if (msg.type() === 'warning') {
35
+ result = msg.text();
36
+ }
37
+ });
38
+
39
+ // Act
40
+ await page.locator('[data-test-id="dispatch-custom-event-mock"]').click();
41
+
42
+ // Assert
43
+ expect(result).toMatch(expected);
44
+ });
45
+ });
package/vite.config.js CHANGED
@@ -18,7 +18,7 @@ export default defineConfig({
18
18
  globals: true,
19
19
  exclude: [
20
20
  '**/test/{accessibility,component,system,visual}/*.spec.{js,ts}',
21
- '**/test/mixins/**/*.browser.spec.{js,ts}',
21
+ '**/test/**/*.browser.spec.{js,ts}',
22
22
  '**/node_modules/**/*'
23
23
  ],
24
24
  },