Skip to content
This repository was archived by the owner on Oct 12, 2018. It is now read-only.

Commit

Permalink
Sapper nested component semantics
Browse files Browse the repository at this point in the history
From sveltejs/sapper#262 (comment)

Doesn't actually support any props yet though, not sure how I want to set those up just yet.
  • Loading branch information
tivac committed Jul 2, 2018
1 parent 17743ad commit 8326883
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 62 deletions.
34 changes: 1 addition & 33 deletions src/app.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,18 @@

<a href="#HOME" use:link>home</a>
<a href="#ONE" use:link>one</a>
<a href="#TWO" use:link>two</a>
<a href="#THREE" use:link>three</a>

<svelte:component this={pages[page]} {children} />
<svelte:component this={page.child} {...page.props} />

<script>
import link from "./link.js";

import state from "./state.js";

import Home from "./home.html";
import One from "./one.html";
import Two from "./two.html";
import Three from "./three.html";

const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1);

export default {
actions : {
link
},

data : () => ({
page : capitalize(state.state.slice(1)),
children : null,
pages : {
Home,
One,
Two,
Three
}
}),

oncreate() {
state.on("enter", ({ curr }) => {
const [ , ...parts ] = curr.state.split("/");

return this.set({
page : capitalize(parts[0]),
children : parts.slice(1)
});
});
},

onstate : console.log.bind(console, "onstate")
};
</script>
61 changes: 59 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
import state from "./state.js";

import App from "./app.html";

new App({
target : document.body,
let app;

state.on("enter", ({ curr }) => {
// TODO: Assumes single-level component at the root
if(!app) {
app = new App({
target : document.body,
data : {
page : {
child : curr.component,
props : {},
},
},
});

return;
}

// Single level component
if(!curr.parent) {
app.set({
page : {
child : curr.component,
props : {},
},
});

return;
}

// Create the list of nested components
const components = [];
let step = curr;

while(step.parent) {
components.push(step);

step = state.get(step.parent);
}

components.push(step);

// Create nested data structure to represent the component tree
const data = {};

components.reverse().reduce((prev, component) => {
prev.page = {
child : component.component,
props : {},
};

return prev.page.props;
}, data);

app.set(data);
});

state.start();
28 changes: 1 addition & 27 deletions src/one.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,14 @@ <h1>ONE</h1>
<a href="#SUBTWO" use:link>subtwo</a>
<a href="#SUBTHREE" use:link>subthree</a>

<svelte:component this={pages[child]} />
<svelte:component this={page.child} {...page.props} />

<script>
import link from "./link.js";

import Subone from "./subone.html";
import Subtwo from "./subtwo.html";
import Subthree from "./subthree.html";

export default {
actions : {
link
},

data : () => ({
children : [],

pages : {
subone : Subone,
subtwo : Subtwo,
subthree : Subthree
}
}),

computed : {
child({ children }) {
return children.length ? children[0] : ""
}
},

oncreate() {
console.log(arguments);
},

onstate : console.log.bind(console, "one onstate")
}
</script>

0 comments on commit 8326883

Please sign in to comment.