@financial-times/custom-code-component 1.1.0 → 1.1.2

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