Skip to content

Commit d26fac8

Browse files
committed
components!
1 parent 68308fa commit d26fac8

File tree

7 files changed

+157
-39
lines changed

7 files changed

+157
-39
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ If you don't want the auto init, remove the `init` attribute and move the script
4141
</script>
4242
```
4343

44-
Or, use the ES modules build:
44+
Or, use the ES module build:
4545

4646
```html
4747
<script type="module">
4848
import { createApp } from 'https://unpkg.com/petite-vue?module'
4949
createApp().mount()
50-
<script>
50+
</script>
5151
```
5252

5353
### Root Scope
5454

55-
The `createApp` function accepts a data object that serves as the root scope for all expressions. This can be used for simple global state management:
55+
The `createApp` function accepts a data object that serves as the root scope for all expressions. This can be used to bootstrap simple, one-off apps:
5656

5757
```html
5858
<script type="module">
@@ -140,6 +140,9 @@ There are no components, but logic can be shared across the app or encapsulated
140140
count: props.initialCount,
141141
inc() {
142142
this.count++
143+
},
144+
mounted() {
145+
console.log(`I'm mounted!`)
143146
}
144147
}
145148
}
@@ -149,7 +152,12 @@ There are no components, but logic can be shared across the app or encapsulated
149152
}).mount()
150153
</script>
151154

152-
<div v-scope="ComponentLike({ initialCount: 10 })">
155+
<div v-scope="ComponentLike({ initialCount: 1 })" @mounted="mounted">
156+
<p>{{ count }}</p>
157+
<button @click="inc">increment</button>
158+
</div>
159+
160+
<div v-scope="ComponentLike({ initialCount: 2 })">
153161
<p>{{ count }}</p>
154162
<button @click="inc">increment</button>
155163
</div>

examples/tree.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<script type="module">
2+
import { createApp } from '../src'
3+
4+
const treeData = {
5+
name: 'My Tree',
6+
children: [
7+
{ name: 'hello' },
8+
{ name: 'wat' },
9+
{
10+
name: 'child folder',
11+
children: [
12+
{
13+
name: 'child folder',
14+
children: [{ name: 'hello' }, { name: 'wat' }]
15+
},
16+
{ name: 'hello' },
17+
{ name: 'wat' },
18+
{
19+
name: 'child folder',
20+
children: [{ name: 'hello' }, { name: 'wat' }]
21+
}
22+
]
23+
}
24+
]
25+
}
26+
27+
function TreeItem(model) {
28+
return {
29+
$template: '#item-template',
30+
model,
31+
open: false,
32+
get isFolder() {
33+
return model.children && model.children.length
34+
},
35+
toggle() {
36+
if (this.isFolder) {
37+
this.open = !this.open
38+
}
39+
},
40+
changeType() {
41+
if (!this.isFolder) {
42+
model.children = []
43+
this.addChild()
44+
this.open = true
45+
}
46+
},
47+
addChild() {
48+
model.children.push({
49+
name: 'new stuff'
50+
})
51+
}
52+
}
53+
}
54+
55+
createApp({
56+
TreeItem,
57+
treeData
58+
}).mount()
59+
</script>
60+
61+
<template id="item-template">
62+
<div :class="{ bold: isFolder }" @click="toggle" @dblclick="changeType">
63+
<span>{{ model.name }}</span>
64+
<span v-if="isFolder">[{{open ? '-' : '+'}}]</span>
65+
</div>
66+
<ul v-show="open" v-if="isFolder">
67+
<li v-for="model in model.children" v-scope="TreeItem(model)"></li>
68+
<li class="add" @click="addChild">+</li>
69+
</ul>
70+
</template>
71+
72+
<p>Double click an item to turn it into a folder.</p>
73+
<ul v-scope>
74+
<li class="item" v-scope="TreeItem(treeData)"></li>
75+
</ul>
76+
77+
<style>
78+
body {
79+
font-family: Menlo, Consolas, monospace;
80+
color: #444;
81+
}
82+
.item {
83+
cursor: pointer;
84+
}
85+
.bold {
86+
font-weight: bold;
87+
}
88+
</style>

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ <h2>Examples</h2>
55
<li><a href="/examples/grid.html">Grid</a></li>
66
<li><a href="/examples/markdown.html">Markdown</a></li>
77
<li><a href="/examples/svg.html">SVG</a></li>
8+
<li><a href="/examples/tree.html">Tree</a></li>
89
</ul>
910

1011
<h2>Tests</h2>
1112
<ul>
12-
<li><a href="/tests/data.html">v-scope</a></li>
13+
<li><a href="/tests/scope.html">v-scope</a></li>
1314
<li><a href="/tests/effect.html">v-effect</a></li>
1415
<li><a href="/tests/bind.html">v-bind</a></li>
1516
<li><a href="/tests/on.html">v-on</a></li>
1617
<li><a href="/tests/if.html">v-if</a></li>
1718
<li><a href="/tests/for.html">v-for</a></li>
1819
<li><a href="/tests/model.html">v-model</a></li>
19-
<li><a href="/tests/reuse.html">Logic Reuse</a></li>
20+
<li><a href="/tests/multi-mount.html">Multi mount</a></li>
2021
</ul>
2122

2223
<style>

tests/component.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script type="module">
2+
import { createApp, reactive } from '../src'
3+
4+
function MyComp() {
5+
return {
6+
$template: '#comp',
7+
count: 0,
8+
get plusOne() {
9+
return this.count + 1
10+
}
11+
}
12+
}
13+
14+
createApp({
15+
MyComp
16+
}).mount()
17+
</script>
18+
19+
<template id="comp">
20+
{{ count }} {{ plusOne }}
21+
<button @click="count++">++</button>
22+
</template>
23+
24+
<div v-scope="MyComp()"></div>

tests/multi-mount.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script type="module">
2+
import { createApp, reactive } from '../src'
3+
4+
const store = reactive({
5+
count: 0,
6+
inc() {
7+
this.count++
8+
}
9+
})
10+
11+
createApp({
12+
store,
13+
count: 1
14+
}).mount('#app1')
15+
16+
createApp({
17+
store,
18+
count: 2
19+
}).mount('#app2')
20+
</script>
21+
22+
<div id="app1">
23+
Global count {{ store.count }}
24+
Local count {{ count }}
25+
</div>
26+
27+
<div id="app2">
28+
Global count {{ store.count }}
29+
Local count {{ count }}
30+
</div>

tests/reuse.html

Lines changed: 0 additions & 33 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)