File tree 4 files changed +66
-7
lines changed
boilerplates/rollup-svelte 4 files changed +66
-7
lines changed Original file line number Diff line number Diff line change 11
11
< link rel ="stylesheet " type ="text/css " href ="/app.css ">
12
12
< script type ="module " src ="/main.js "> </ script >
13
13
</ head >
14
- < body > </ body >
14
+ < body >
15
+ < svelte-app foo ="bar "> </ svelte-app >
16
+ </ body >
15
17
</ html >
Original file line number Diff line number Diff line change 1
1
<script >
2
2
import router from " ./web_modules/page.js" ;
3
-
4
3
import Home from " ./routes/Home.svelte" ;
5
4
5
+ export let foo;
6
+
6
7
let page;
7
8
let params;
8
9
13
14
}, () => page = Home);
14
15
15
16
router .start ();
17
+
18
+ console .log (foo);
16
19
</script >
17
20
18
21
<svelte:component this ={page } params ={params }/>
Original file line number Diff line number Diff line change
1
+ import connect from "./utils/connect" ;
1
2
import App from "./App.svelte" ;
2
3
import "./main.css" ;
3
4
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
+ /*
4
15
const app = new App({
5
16
target: document.body,
6
17
props: {},
7
18
});
19
+ */
8
20
9
- if ( "serviceWorker" in navigator ) {
10
- navigator . serviceWorker . register ( "/service-worker.js" ) ;
11
- }
12
-
13
- export default app ;
21
+ // export default app;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments