Skip to content

Commit 1e1742d

Browse files
committed
add connect util
1 parent d5b8167 commit 1e1742d

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

boilerplates/rollup-svelte/public/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
<link rel="stylesheet" type="text/css" href="/app.css">
1212
<script type="module" src="/main.js"></script>
1313
</head>
14-
<body></body>
14+
<body>
15+
<svelte-app foo="bar"></svelte-app>
16+
</body>
1517
</html>

boilerplates/rollup-svelte/src/App.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
22
import router from "./web_modules/page.js";
3-
43
import Home from "./routes/Home.svelte";
54
5+
export let foo;
6+
67
let page;
78
let params;
89
@@ -13,6 +14,8 @@
1314
}, () => page = Home);
1415
1516
router.start();
17+
18+
console.log(foo);
1619
</script>
1720

1821
<svelte:component this={page} params={params}/>
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import connect from "./utils/connect";
12
import App from "./App.svelte";
23
import "./main.css";
34

5+
if ("serviceWorker" in navigator) {
6+
navigator.serviceWorker.register("/service-worker.js");
7+
}
8+
9+
// --- With Web Components ---
10+
// You can connect as much Web Components as you want.
11+
connect("svelte-app", App, ["foo"]);
12+
13+
// --- Single Page App ---
14+
/*
415
const app = new App({
516
target: document.body,
617
props: {},
718
});
19+
*/
820

9-
if ("serviceWorker" in navigator) {
10-
navigator.serviceWorker.register("/service-worker.js");
11-
}
12-
13-
export default app;
21+
// export default app;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Connect Web Component attributes to Svelte Component properties
3+
* @param {string} name Name of the Web Component
4+
* @param {*} Component Svelte Component
5+
* @param {string[]} attributes Which attributes will be passed as properties
6+
*/
7+
export default function connect(name, Component, attributes = []) {
8+
return customElements.define(name, class extends HTMLElement {
9+
constructor() {
10+
super();
11+
this.component = undefined;
12+
}
13+
14+
static get observedAttributes() {
15+
return attributes;
16+
}
17+
18+
attributeChangedCallback(name, oldValue, newValue) {
19+
if (this.component && oldValue !== newValue) {
20+
this.component.$set({ [name]: newValue });
21+
}
22+
}
23+
24+
connectedCallback() {
25+
let props = {};
26+
27+
for (let attr of attributes) {
28+
props[attr] = this.getAttribute(attr) || undefined;
29+
}
30+
31+
// empty element
32+
while (this.firstChild) {
33+
this.removeChild(this.lastChild);
34+
}
35+
36+
this.component = new Component({
37+
target: this,
38+
props,
39+
});
40+
}
41+
42+
disconnectedCallback() {
43+
this.component.$destroy();
44+
}
45+
});
46+
}

0 commit comments

Comments
 (0)