Skip to content

Instantly share code, notes, and snippets.

@chenglou
Created July 14, 2015 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenglou/870bba30490d06c3c4a4 to your computer and use it in GitHub Desktop.
Save chenglou/870bba30490d06c3c4a4 to your computer and use it in GitHub Desktop.
react-motion demo
import React from 'react';
import {Spring} from '../src/Spring';
const childVal = 275;
const Demo = React.createClass({
getInitialState() {
return {open: false};
},
handleMouseDown() {
this.setState({open: !this.state.open});
},
getEndValue(currentValue) {
const parentVal = this.state.open ? 400 : 0;
if (currentValue == null) {
return {
parent: {val: parentVal},
children: {val: [0, 0, 0]},
};
}
const {parent, children: {val: [c1, c2]}} = currentValue;
return {
parent: {val: parentVal},
children: {
val: [
// percentage of parent val
parent.val / 400 * childVal,
c1 - 100,
c2 - 100,
],
config: [120, 17],
},
};
},
render() {
return (
<div>
<button onMouseDown={this.handleMouseDown}>Toggle</button>
<Spring endValue={this.getEndValue}>
{({parent, children: {val: [c1, c2, c3]}}) =>
// children is a callback which should accept the current value of
// `endValue`
<div className="demo0" style={{height: parent.val}}>
<div className="demo0-block" style={{
WebkitTransform: `translate3d(0, ${c1}px, 0)`,
transform: `translate3d(0, ${c1}px, 0)`,
}} />
<div className="demo0-block" style={{
WebkitTransform: `translate3d(0, ${c2}px, 0)`,
transform: `translate3d(0, ${c2}px, 0)`,
}} />
<div className="demo0-block" style={{
WebkitTransform: `translate3d(0, ${c3}px, 0)`,
transform: `translate3d(0, ${c3}px, 0)`,
}} />
</div>
}
</Spring>
</div>
);
},
});
export default Demo;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Toggle</title>
<style>
.demo0 {
border-radius: 4px;
background-color: rgb(240, 240, 232);
position: relative;
margin: 5px 3px 10px;
width: 250px;
height: 500px;
overflow: hidden;
}
.demo0-block {
position: absolute;
left: 25px;
width: 200px;
margin: auto;
height: 50px;
border-radius: 4px;
background-color: rgb(130, 181, 198);
}
</style>
</head>
<body>
<div id="content"></div>
<script src="./all.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment