Created
July 15, 2015 20:09
-
-
Save bsansouci/3a8cc7f1dddaebbfabda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*eslint-disable */ | |
import React, {PropTypes} from 'react'; | |
import {TransitionSpring} from '../src/Spring'; | |
const Demo = React.createClass({ | |
getInitialState() { | |
return { open: false }; | |
}, | |
onOpenModal() { | |
this.refs.modal1.open(); | |
}, | |
render() { | |
return ( | |
<div> | |
<button onMouseDown={this.onOpenModal}>Open Modal</button> | |
<Modal ref="modal1" /> | |
</div> | |
); | |
}, | |
}); | |
const Modal = React.createClass({ | |
propTypes: { | |
onClose: PropTypes.func.isRequired, | |
}, | |
getInitialState() { | |
return { open: false }; | |
}, | |
endValue() { | |
if(!this.state.open) return {}; | |
return { | |
key: { | |
opacity: { | |
val: 1 | |
} | |
} | |
}; | |
}, | |
willEnter() { | |
return { | |
opacity: { | |
val: 0 | |
} | |
}; | |
}, | |
willLeave(key, endValue, currentValue) { | |
if (this.state.open === false && currentValue[key].opacity.val === 0) { | |
return null; | |
} | |
return { | |
opacity: { | |
val: 0 | |
} | |
} | |
}, | |
render() { | |
return ( | |
<TransitionSpring endValue={this.endValue} willLeave={this.willLeave} willEnter={this.willEnter}> | |
{config => | |
config.key ? | |
<div className="modal" style={{ opacity: config.key.opacity.val }}> | |
Modal <button onClick={this.onClose}>×</button> | |
</div> | |
: <span /> | |
} | |
</TransitionSpring> | |
); | |
}, | |
onClose() { | |
this.setState({ open: false }); | |
}, | |
open() { | |
this.setState({ open: true }); | |
}, | |
}); | |
export default Demo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code could look a lot nicer if we had inline data structures for
willEnter
andwillLeave
.That said it's just sugar, and it requires use to cover more small edge cases (and meaning we need a default willLeave that checks if the current reached the destination). So I'm not sure if it's worth it.