-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.jsx
154 lines (123 loc) · 3.29 KB
/
app.jsx
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/// <reference path="./types.d.ts" />
import React from "react";
import {TransitionSpring,Spring,utils as RMutils} from "react-motion";
const {reorderKeys} = RMutils;
const update = React.addons;
let Demo = React.createClass({
getInitialState() {
return {
// Apparently Object.keys return keys in insertion order (except for 1,2,3).
letters: {
"#A": true,
"#N": true,
"#T": true,
},
};
},
componentDidMount() {
window.addEventListener("keyup",this.handleKeyup)
},
handleKeyup(e) {
const {keyCode} = e;
if(!isAlphaNumeric(keyCode)) {
return;
}
const letter = `#${String.fromCharCode(e.keyCode)}`;
const {letters} = this.state;
const hasLetter = letters[letter];
console.log(letter);
let newLetters = {...letters};
if(hasLetter) {
delete newLetters[letter]
} else {
newLetters[letter] = true;
}
newLetters = reorderKeys(newLetters,keys => keys.sort());
this.setState({letters: newLetters});
},
toggle(letter) {
const selected = this.state.letters[letter];
this.setState({
letters: {
...this.state.letters,
[letter]: !selected,
},
});
},
// TransitionSpring Methods
getEndValue() {
let values = {};
Object.keys(this.state.letters).forEach(key => {
values[key] = {
width: {val: 50},
scale: {val: 1},
margin: {val: 5},
}
});
console.log(values);
return values;
},
willEnter(key) {
return {
width: {val: 0},
margin: {val: 0},
scale: {val: 0},
}
},
willLeave(key) {
return {
width: {val: 0},
margin: {val: 0},
scale: {val: 0},
}
},
render() {
const {letters} = this.state;
return (
<TransitionSpring
endValue={this.getEndValue()}
willEnter={this.willEnter}
willLeave={this.willLeave}>
{values =>
<div className="letters">
{Object.keys(values).map(letter => {
const {scale,width,margin} = values[letter];
const widthValue = Math.ceil(width.val-0.5);
const marginValue = Math.ceil(margin.val-0.5);
let styles = {
transform: `scale(${scale.val})`,
height: widthValue,
width: widthValue,
margin: marginValue,
borderRadius: widthValue,
};
return (
<span
key={letter}
className="letter"
style={styles}
onClick={this.toggle.bind(this,letter)}>
{letter.substring(1)}
</span>
)
})}
</div>
}
</TransitionSpring>
);
}
});
function isAlphaNumeric(keyCode) {
return (48 <= keyCode && keyCode <= 57) || (65 <= keyCode && keyCode <= 90);
}
const app = (
<div className="container">
<Demo/>
<h3 className="help">press any key</h3>
<h3 className="colophon">
made with <a href="https://github.com/chenglou/react-motion">react motion.</a>
<a href="https://github.com/hayeah/react-motion-TransitionSpring-demo/blob/master/app.jsx">source.</a>
</h3>
</div>
);
React.render(app, document.querySelector('#content'));