@financial-times/custom-code-component 1.0.7 → 1.1.1

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.
@@ -8,52 +8,63 @@ class l extends HTMLElement {
8
8
  "data-asset-type",
9
9
  "shadow-open",
10
10
  "env"
11
- ]);
11
+ ]), this.unmount = () => {
12
+ };
12
13
  }
13
- async connectedCallback() {
14
- var m;
15
- const e = this.getAttribute("path"), c = this.hasAttribute("shadow-open") ? "open" : "closed", a = this.getAttribute("version"), d = (m = this.getAttribute("env")) == null ? void 0 : m.toLowerCase().startsWith("d");
14
+ static get observedAttributes() {
15
+ return ["path", "version", "env", "data-component-props"];
16
+ }
17
+ async mount() {
18
+ var r;
19
+ const e = this.getAttribute("path"), s = this.hasAttribute("shadow-open") ? "open" : "closed", c = this.getAttribute("version"), p = (r = this.getAttribute("env")) == null ? void 0 : r.toLowerCase().startsWith("d");
16
20
  if (!e)
17
21
  throw new Error(
18
22
  "path attribute not specified in <custom-code-component>"
19
23
  );
20
- const [n, i, r] = e.split("/").reverse();
21
- if (!n || !i || !r)
24
+ const [o, a, i] = e.split("/").reverse();
25
+ if (!o || !a || !i)
22
26
  return;
23
- const u = d ? `http://localhost:5173/src/${n}/index.jsx` : `https://www.ft.com/__component/${r}/${i}${a ? `@${a}` : "@latest"}/${n}/${n}.js`;
27
+ const d = p ? `http://localhost:5173/src/${o}/index.jsx` : `https://www.ft.com/__component/${i}/${a}${c ? `@${c}` : "@latest"}/${o}/${o}.js`;
24
28
  try {
25
- const s = await import(
29
+ const n = await import(
26
30
  /* webpackIgnore: true */
27
- u
31
+ d
28
32
  /* @vite-ignore */
29
- ), p = JSON.parse(this.getAttribute("data-component-props")), h = Object.fromEntries(
33
+ ), m = JSON.parse(this.getAttribute("data-component-props")), h = Object.fromEntries(
30
34
  [...this.attributes].filter((t) => !this.RESERVED_ATTRS.has(t.name)).map((t) => [t.name, t.value])
31
35
  );
32
36
  if (this.hasAttribute("iframe")) {
33
37
  const t = document.createElement("iframe");
34
38
  t.addEventListener("load", () => {
35
- const o = s.default(
39
+ const u = n.default(
36
40
  t.contentDocument,
37
- { ...h, data: p },
41
+ { ...h, data: m },
38
42
  ...this.children
39
43
  );
40
- o && (this.disconnectedCallback = o);
41
- }), this.attachShadow({ mode: c }).append(t);
44
+ u && (this.unmount = u);
45
+ }), this.attachShadow({ mode: s }).append(t);
42
46
  } else {
43
- const t = this.attachShadow({ mode: c }), o = await s.default(
44
- t,
45
- { ...h, data: p },
47
+ this.shadowRoot ? this.shadowRoot.innerHTML = "" : this.attachShadow({ mode: s });
48
+ const t = await n.default(
49
+ this.shadowRoot,
50
+ { ...h, data: m },
46
51
  ...this.children
47
52
  );
48
- o && (this.disconnectedCallback = o);
53
+ t && (this.unmount = t);
49
54
  }
50
- } catch (s) {
51
- console.info(`<custom-code-component> uncaught error from ${e}: `, s);
55
+ } catch (n) {
56
+ console.info(`<custom-code-component> uncaught error from ${e}: `, n);
52
57
  }
53
58
  }
54
- async disconnectedCallback() {
59
+ connectedCallback() {
60
+ this.mount();
61
+ }
62
+ attributeChangedCallback() {
63
+ this.mount();
64
+ }
65
+ disconnectedCallback() {
55
66
  const e = this.getAttribute("path");
56
- console.info(`<custom-code-component:${e}> disconnected`);
67
+ console.info(`<custom-code-component:${e}> disconnected`), this.unmount();
57
68
  }
58
69
  }
59
70
  const f = () => customElements.define("custom-code-component", l);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@financial-times/custom-code-component",
3
- "version": "1.0.7",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -16,7 +16,13 @@ class FTCustomCodeComponent extends HTMLElement {
16
16
  "env",
17
17
  ]);
18
18
 
19
- async connectedCallback() {
19
+ static get observedAttributes() {
20
+ return ["path", "version", "env", "data-component-props"];
21
+ }
22
+
23
+ unmount = () => {};
24
+
25
+ async mount() {
20
26
  const path = this.getAttribute("path");
21
27
  const mode = this.hasAttribute("shadow-open") ? "open" : "closed";
22
28
  const componentVersionRange = this.getAttribute("version");
@@ -64,29 +70,39 @@ class FTCustomCodeComponent extends HTMLElement {
64
70
  ...this.children
65
71
  );
66
72
 
67
- if (unmount) this.disconnectedCallback = unmount;
73
+ if (unmount) this.unmount = unmount;
68
74
  });
69
75
 
70
76
  this.attachShadow({ mode }).append(mountPoint);
71
77
  } else {
72
- const shadowRoot = this.attachShadow({ mode });
78
+ if (!this.shadowRoot) this.attachShadow({ mode });
79
+ else this.shadowRoot.innerHTML = "";
73
80
 
74
81
  const unmount = await App.default(
75
- shadowRoot,
82
+ this.shadowRoot,
76
83
  { ...extraProps, data },
77
84
  ...this.children
78
85
  );
79
86
 
80
- if (unmount) this.disconnectedCallback = unmount;
87
+ if (unmount) this.unmount = unmount;
81
88
  }
82
89
  } catch (e) {
83
90
  console.info(`<custom-code-component> uncaught error from ${path}: `, e);
84
91
  }
85
92
  }
86
93
 
87
- async disconnectedCallback() {
94
+ connectedCallback() {
95
+ this.mount();
96
+ }
97
+
98
+ attributeChangedCallback() {
99
+ this.mount();
100
+ }
101
+
102
+ disconnectedCallback() {
88
103
  const path = this.getAttribute("path");
89
104
  console.info(`<custom-code-component:${path}> disconnected`);
105
+ this.unmount();
90
106
  }
91
107
  }
92
108