Skip to content

Instantly share code, notes, and snippets.

@theKashey
Forked from ryanflorence/foo.js
Last active August 27, 2018 16:04
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 theKashey/002dad1b867c77f21b63d59d56c35662 to your computer and use it in GitHub Desktop.
Save theKashey/002dad1b867c77f21b63d59d56c35662 to your computer and use it in GitHub Desktop.
Schrodinger Logic to show a modal
import {Phased} from 'recondition';
// Adds a lovely fade in of the modal
// and a gentle slide-down of the modal content
class Demo extends React.Component {
state = { showDialog: false };
render() {
return (
<div>
<button onClick={() => this.setState({ showDialog: true })}>
Show Dialog
</button>
<Phased value={this.state.showDialog} timeouts={[300]}>
{({value, nextValue, phasing}) =>
// display modal if it IS open or WILL be open.
// ie when it is
// - open
// - closed, but gonna be opened
// - open, but gonna be closing
(value || nextValue) && (
<DialogOverlay
className={cx(
'modal-dialog',
// on open value is false, nextValue is true, phasing is false
// then value is false, nextValue is true, phasing is true
// then value is true, nextValue is true, phasing is true
// then value is true, nextValue is true, phasing is false
phasing
? (nextValue ? 'opened' : 'closed')
: (value ? 'opened' : 'closed')
)}
>
<DialogContent>
<button onClick={() => this.setState({ showDialog: false })}>
Close Dialog
</button>
<p>React Spring makes it too easy!</p>
</DialogContent>
</DialogOverlay>
))}
</Phased>
</div>
);
}
}
<style>
.modal-dialog {
transition-property: transform, opacity;
transition-duration: 300ms;
opacity: 0;
transform: translateY(10px);
}
.opened {
opacity: 1;
transform: translateY(0px);
}
.opened {
opacity: 0;
transform: translateY(10px);
}
</style>
PS: to be honest this is FAAAAARRRrrr more complex that Ryan example :P
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment