UNPKG

material-ui

Version:

Material Design UI components built with React

120 lines (97 loc) 3.13 kB
const React = require('react'); const ReactDOM = require('react-dom'); const StylePropable = require('../mixins/style-propable'); const AutoPrefix = require('../styles/auto-prefix'); const Transitions = require('../styles/transitions'); const DefaultRawTheme = require('../styles/raw-themes/light-raw-theme'); const ThemeManager = require('../styles/theme-manager'); const SlideInChild = React.createClass({ mixins: [StylePropable], contextTypes: { muiTheme: React.PropTypes.object, }, //for passing default theme context to children childContextTypes: { muiTheme: React.PropTypes.object, }, getChildContext () { return { muiTheme: this.state.muiTheme, }; }, getInitialState () { return { muiTheme: this.context.muiTheme ? this.context.muiTheme : ThemeManager.getMuiTheme(DefaultRawTheme), }; }, //to update theme inside state whenever a new theme is passed down //from the parent / owner using context componentWillReceiveProps (nextProps, nextContext) { let newMuiTheme = nextContext.muiTheme ? nextContext.muiTheme : this.state.muiTheme; this.setState({muiTheme: newMuiTheme}); }, propTypes: { enterDelay: React.PropTypes.number, //This callback is needed bacause //the direction could change when leaving the dom getLeaveDirection: React.PropTypes.func.isRequired, }, getDefaultProps: function() { return { enterDelay: 0, }; }, componentWillEnter(callback) { let style = ReactDOM.findDOMNode(this).style; let x = this.props.direction === 'left' ? '100%' : this.props.direction === 'right' ? '-100%' : '0'; let y = this.props.direction === 'up' ? '100%' : this.props.direction === 'down' ? '-100%' : '0'; style.opacity = '0'; AutoPrefix.set(style, 'transform', 'translate3d(' + x + ',' + y + ',0)'); setTimeout(() => { if (this.isMounted()) callback(); }, this.props.enterDelay); }, componentDidEnter() { let style = ReactDOM.findDOMNode(this).style; style.opacity = '1'; AutoPrefix.set(style, 'transform', 'translate3d(0,0,0)'); }, componentWillLeave(callback) { let style = ReactDOM.findDOMNode(this).style; let direction = this.props.getLeaveDirection(); let x = direction === 'left' ? '-100%' : direction === 'right' ? '100%' : '0'; let y = direction === 'up' ? '-100%' : direction === 'down' ? '100%' : '0'; style.opacity = '0'; AutoPrefix.set(style, 'transform', 'translate3d(' + x + ',' + y + ',0)'); setTimeout(() => { if (this.isMounted()) callback(); }, 450); }, render() { let { children, enterDelay, getLeaveDirection, style, ...other, } = this.props; let mergedRootStyles = this.prepareStyles({ position: 'absolute', height: '100%', width: '100%', top: 0, left: 0, transition: Transitions.easeOut(null, ['transform', 'opacity']), }, style); return ( <div {...other} style={mergedRootStyles}> {children} </div> ); }, }); module.exports = SlideInChild;