-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdemo.card.js
135 lines (124 loc) · 3.43 KB
/
demo.card.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import devboard from '../';
import React from 'react';
import Color from 'color-js';
import { combine } from '../lib/utils';
var definecard = devboard.ns('4. Thermometer demo');
definecard(`
This demo is of a component which was built iteratively using devboard.
Unfortunately, because you're looking at a static page, you can only see the
end product. I'm planning to re-work this demo to show all of the intermediary
stages at some point, ideally also with an associated screencast.
`);
var style = {
container: { width: 50 },
stickWrapper: { position: 'relative', height: 200 },
stick: {
position: 'absolute',
boxSizing: 'content-box',
top: -1, bottom: 0, left: 15, right: 5,
background: 'white',
borderTop: '1px solid black',
borderLeft: '1px solid black',
borderRight: '1px solid black',
borderTopLeftRadius: 100,
borderTopRightRadius: 100,
overflow: 'hidden'
},
mercury: { width: 28 },
marking: {
position: 'absolute',
fontSize: 6,
boxSizing: 'border-box',
width: 25,
color: '#666',
borderBottom: '1px solid #666',
},
bulb: {
marginTop: -7,
marginLeft: 10,
width: 40, height: 40,
borderRadius: 100,
border: '1px solid black'
},
label: { textAlign: 'center' }
};
var Thermometer = React.createClass({
render() {
var { temp } = this.props;
temp = restrict(temp, -50, 150);
var percentage = calcPercent(temp, -50, 150);
var size = percentage * 2;
var color = colourPoint('#66f', '#900', percentage * 1.5);
return (
<div style={style.container}>
<div style={style.stickWrapper}>
<div style={style.stick}>
<div style={combine(
style.mercury,
{
background: color,
transition: 'height 1s, margin-top 1s',
height: size,
marginTop: 200 - size,
})}
/>
</div>
{range(-40, 140, 10).map(t => (
<span style={combine(
style.marking,
{ bottom: t + 50 }
)}>{t}</span>
))}
</div>
<div style={combine(style.bulb, { background: color })} />
<p style={style.label}>
{temp}
</p>
</div>
);
}
});
definecard(
'Thermometer demo',
`Things are hotting up!`,
({state}) => <div>
<devboard.Row>
<Thermometer temp={-30} />
<Thermometer temp={50} />
<Thermometer temp={state.deref().ticking - 50} />
<div>
<Thermometer temp={state.deref().temp} />
<input
type="text" size="4" value={state.deref().temp}
onChange={({target}) => state.swap(set('temp', target.value))} />
</div>
</devboard.Row>
</div>,
{
state: devboard.atom({ temp: 0, ticking: 0 }),
onTick: ({state}) => state.swap(set('ticking', t => (t + 5) % 200)),
tickInterval: 200, tickAutoplay: false
}
);
function restrict(n, min, max) {
if (n < min) n = min;
if (n > max) n = max;
return n;
}
function calcPercent(n, min, max) {
return ((n - min) / (max - min)) * 100;
}
function colourPoint(a, b, percentage) {
return Color(a).blend(Color(b), percentage / 100).toString();
}
function set(key, val) {
return obj => Object.assign(
{}, obj,
{ [key]: (typeof val === 'function') ? val(obj[key]) : val }
);
}
function range(start, end, step) {
var arr = [];
for (var i = start; i <= end; i += step) arr.push(i);
return arr;
}