Skip to content

Commit 1088f8c

Browse files
authored
Merge pull request #24 from mreinstein/2.0
2.0 fixes #23
2 parents 9a7a771 + e9753b8 commit 1088f8c

File tree

19 files changed

+2859
-1181
lines changed

19 files changed

+2859
-1181
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ sudo: false
22
language: node_js
33
node_js:
44
- 'node'
5-
- '6'
6-
- '5'
7-
- '4'
5+
- '14'
6+
- '12'

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# changelog
22

3+
## 2.0.0
4+
* require node v12.17+
5+
* update snabbdom from 0.7 to 2.1
6+
* convert to pure es module
7+
* simplify examples
8+
9+
310
## 1.2.0
411

512
* add `thunk` support (fixes #5)

create.js

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,67 @@
1-
var snabbdom = require('snabbdom')
2-
var thunk = require('snabbdom/thunk').default
3-
var h = require('snabbdom/h').default
4-
var hyperx = require('hyperx')
1+
import { init } from 'https://cdn.jsdelivr.net/npm/snabbdom@2.1.0/build/package/init.js';
2+
import { thunk } from 'https://cdn.jsdelivr.net/npm/snabbdom@2.1.0/build/package/thunk.js';
3+
import { h } from 'https://cdn.jsdelivr.net/npm/snabbdom@2.1.0/build/package/h.js'; // helper function for creating vnodes
4+
import hyperx from 'hyperx'
55

66

7-
module.exports = create
7+
export default function create (modules, options={}) {
88

9-
function create (modules, options) {
10-
if (!options) options = {}
11-
12-
// options
13-
var directive = options.directive || '@'
9+
const directive = options.directive || '@'
1410

1511
function createElement (sel, input, content) {
16-
// Adjust content:
1712
if (content && content.length) {
18-
if (content.length === 1) {
13+
if (content.length === 1)
1914
content = content[0]
20-
} else {
21-
// Flatten nested arrays
22-
content = [].concat.apply([], content)
23-
}
15+
else
16+
content = [].concat.apply([], content) // flatten nested arrays
2417
}
2518

26-
// Attribute names, and handling none faster:
27-
var names = Object.keys(input)
28-
if (!names || !names.length) {
19+
// attribute names, and handling none faster:
20+
const names = Object.keys(input)
21+
if (!names || !names.length)
2922
return h(sel, content)
30-
}
3123

32-
// Parse Snabbdom's `data` from attributes:
33-
var data = {}
34-
for (var i = 0, max = names.length; max > i; i++) {
35-
var name = names[i]
36-
if (input[name] === 'false') {
24+
// parse Snabbdom's `data` from attributes:
25+
const data = { }
26+
for (let i = 0, max = names.length; max > i; i++) {
27+
const name = names[i]
28+
if (input[name] === 'false')
3729
input[name] = false
38-
}
3930

40-
// Directive attributes
31+
// directive attributes
4132
if (name.indexOf(directive) === 0) {
42-
var parts = name.slice(1).split(':')
43-
var previous = data
44-
for (var p = 0, pmax = parts.length, last = pmax - 1; p < pmax; p++) {
45-
var part = parts[p]
46-
if (p === last) {
33+
const parts = name.slice(1).split(':')
34+
let previous = data
35+
for (let p = 0, pmax = parts.length, last = pmax - 1; p < pmax; p++) {
36+
const part = parts[p]
37+
if (p === last)
4738
previous[part] = input[name]
48-
} else if (!previous[part]) {
49-
previous = previous[part] = {}
50-
} else {
39+
else if (!previous[part])
40+
previous = previous[part] = { }
41+
else
5142
previous = previous[part]
52-
}
5343
}
5444
}
5545

56-
// Put all other attributes into `data.attrs`
46+
// put all other attributes into `data.attrs`
5747
else {
58-
if (!data.attrs) data.attrs = {}
48+
if (!data.attrs)
49+
data.attrs = { }
5950
data.attrs[name] = input[name]
6051
}
6152
}
6253

63-
// Return vnode:
54+
// return vnode:
6455
return h(sel, data, content)
6556
}
6657

67-
// Create the snabbdom + hyperx functions
68-
var patch = snabbdom.init(modules || [])
58+
// create the snabbdom + hyperx functions
59+
const patch = init(modules || [ ])
6960

70-
// Create snabby function
71-
var snabby = hyperx(createElement, { attrToProp: false })
61+
// create snabby function
62+
const snabby = hyperx(createElement, { attrToProp: false })
7263

73-
// Create yo-yo-like update function
64+
// create yo-yo-like update function
7465
snabby.update = function update (dest, src) {
7566
return patch(dest, src)
7667
}
@@ -79,5 +70,3 @@ function create (modules, options) {
7970

8071
return snabby
8172
}
82-
83-

examples/counter.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Snabby - Counter Example</title>
5+
<meta name=theme-color content=#303F9F><meta name=viewport content="width=device-width,minimum-scale=1">
6+
<style>
7+
body {
8+
font-family: monospace;
9+
background-color: whitesmoke;
10+
padding: 10px;
11+
}
12+
13+
</style>
14+
</head>
15+
<body>
16+
17+
<p>
18+
19+
</p>
20+
21+
<script type="module">
22+
import html from '../snabby.js'
23+
24+
25+
function counter (count) {
26+
const view = html`
27+
<div class="main">
28+
<button @on:click=${sub}>-</button>
29+
<span>${count}</span>
30+
<button @on:click=${add}>+</button>
31+
</div>`
32+
33+
function add () {
34+
html.update(view, counter(count + 1))
35+
}
36+
37+
function sub () {
38+
html.update(view, counter(count - 1))
39+
}
40+
41+
return view
42+
}
43+
44+
45+
html.update(document.body, counter(0))
46+
47+
</script>
48+
49+
</body>
50+
</html>

examples/counter/index.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/counter/package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/text-input/index.js renamed to examples/text-input.html

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1-
const html = require('../../')
2-
const css = require('sheetify')
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Snabby - Text Input Example</title>
5+
<meta name=theme-color content=#303F9F><meta name=viewport content="width=device-width,minimum-scale=1">
6+
<style>
7+
body {
8+
font-family: monospace;
9+
background-color: whitesmoke;
10+
padding: 10px;
11+
}
12+
13+
.app-title {
14+
font-size: 4em
15+
}
16+
17+
.app-desc {
18+
font-size: 1.5em
19+
}
20+
21+
.app-inputs {
22+
margin-top: 20px
23+
}
24+
25+
</style>
26+
</head>
27+
<body>
28+
29+
<p>
30+
31+
</p>
32+
33+
<script type="module">
34+
import html from '../snabby.js'
35+
336

437
function app (state) {
5-
css`
6-
.app-title {
7-
font-size: 4em
8-
}
9-
10-
.app-desc {
11-
font-size: 1.5em
12-
}
13-
14-
.app-inputs {
15-
margin-top: 20px
16-
}
17-
`
1838

1939
var view = html`
2040
<div class='app'>
@@ -48,3 +68,8 @@
4868
title: 'Hello, World!',
4969
desc: 'People greet the world.'
5070
}))
71+
72+
</script>
73+
74+
</body>
75+
</html>

examples/text-input/package.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/thunk.html

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Snabby - Thunk Example</title>
5+
<meta name=theme-color content=#303F9F><meta name=viewport content="width=device-width,minimum-scale=1">
6+
<style>
7+
body {
8+
font-family: monospace;
9+
background-color: whitesmoke;
10+
padding: 10px;
11+
}
12+
13+
</style>
14+
</head>
15+
<body>
16+
17+
<p>
18+
For this example, take a look at the javascript console, and press the random button several times.
19+
You'll notice that when the value that gets chosen matches the previous value, the "ACTUAL UPDATE" console message
20+
doesn't fire. this is because the view hasn't changed, so the thunk doesn't re-evaluate the snabby dom vnode.
21+
</p>
22+
23+
<script type="module">
24+
import html from '../snabby.js'
25+
26+
27+
function randomInt () {
28+
return Math.ceil(Math.random() * 3)
29+
}
30+
31+
32+
function numberView (n) {
33+
console.log('ACTUAL UPDATE:', n)
34+
return html`<span>Number is ${n}</span>`
35+
}
36+
37+
38+
function counter (count) {
39+
console.log('outer call:', count)
40+
const view = html`
41+
<div class="main">
42+
<button @on:click=${sub}>-</button>
43+
<span>${html.thunk('num', numberView, [count])}</span>
44+
<button @on:click=${add}>+</button>
45+
<button @on:click=${rand}>random</button>
46+
</div>
47+
`
48+
49+
function add () {
50+
html.update(view, counter(count + 1))
51+
}
52+
53+
function sub () {
54+
html.update(view, counter(count - 1))
55+
}
56+
57+
function rand () {
58+
html.update(view, counter(randomInt()))
59+
}
60+
61+
return view
62+
}
63+
64+
65+
html.update(document.body, counter(0))
66+
67+
</script>
68+
69+
</body>
70+
</html>

0 commit comments

Comments
 (0)