|
| 1 | +/*eslint-disable react/prop-types */ |
| 2 | +'use strict'; |
| 3 | +import React from 'react'; |
| 4 | +import Transition from './Transition'; |
| 5 | +import domUtils from './utils/domUtils'; |
| 6 | +import createChainedFunction from './utils/createChainedFunction'; |
| 7 | + |
| 8 | +let capitalize = str => str[0].toUpperCase() + str.substr(1); |
| 9 | + |
| 10 | +// reading a dimension prop will cause the browser to recalculate, |
| 11 | +// which will let our animations work |
| 12 | +let triggerBrowserReflow = node => node.offsetHeight; //eslint-disable-line no-unused-expressions |
| 13 | + |
| 14 | +const MARGINS = { |
| 15 | + height: ['marginTop', 'marginBottom'], |
| 16 | + width: ['marginLeft', 'marginRight'] |
| 17 | +}; |
| 18 | + |
| 19 | +function getDimensionValue(dimension, elem){ |
| 20 | + let value = elem[`offset${capitalize(dimension)}`]; |
| 21 | + let computedStyles = domUtils.getComputedStyles(elem); |
| 22 | + let margins = MARGINS[dimension]; |
| 23 | + |
| 24 | + return (value + |
| 25 | + parseInt(computedStyles[margins[0]], 10) + |
| 26 | + parseInt(computedStyles[margins[1]], 10) |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +class Collapse extends React.Component { |
| 31 | + |
| 32 | + constructor(props, context){ |
| 33 | + super(props, context); |
| 34 | + |
| 35 | + this.onEnterListener = this.handleEnter.bind(this); |
| 36 | + this.onEnteringListener = this.handleEntering.bind(this); |
| 37 | + this.onEnteredListener = this.handleEntered.bind(this); |
| 38 | + this.onExitListener = this.handleExit.bind(this); |
| 39 | + this.onExitingListener = this.handleExiting.bind(this); |
| 40 | + } |
| 41 | + |
| 42 | + render() { |
| 43 | + let enter = createChainedFunction(this.onEnterListener, this.props.onEnter); |
| 44 | + let entering = createChainedFunction(this.onEnteringListener, this.props.onEntering); |
| 45 | + let entered = createChainedFunction(this.onEnteredListener, this.props.onEntered); |
| 46 | + let exit = createChainedFunction(this.onExitListener, this.props.onExit); |
| 47 | + let exiting = createChainedFunction(this.onExitingListener, this.props.onExiting); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Transition |
| 51 | + ref='transition' |
| 52 | + {...this.props} |
| 53 | + aria-expanded={this.props.in} |
| 54 | + className={this._dimension() === 'width' ? 'width' : ''} |
| 55 | + exitedClassName='collapse' |
| 56 | + exitingClassName='collapsing' |
| 57 | + enteredClassName='collapse in' |
| 58 | + enteringClassName='collapsing' |
| 59 | + onEnter={enter} |
| 60 | + onEntering={entering} |
| 61 | + onEntered={entered} |
| 62 | + onExit={exit} |
| 63 | + onExiting={exiting} |
| 64 | + onExited={this.props.onExited} |
| 65 | + > |
| 66 | + { this.props.children } |
| 67 | + </Transition> |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /* -- Expanding -- */ |
| 72 | + handleEnter(elem){ |
| 73 | + let dimension = this._dimension(); |
| 74 | + elem.style[dimension] = '0'; |
| 75 | + } |
| 76 | + |
| 77 | + handleEntering(elem){ |
| 78 | + let dimension = this._dimension(); |
| 79 | + |
| 80 | + elem.style[dimension] = this._getScrollDimensionValue(elem, dimension); |
| 81 | + } |
| 82 | + |
| 83 | + handleEntered(elem){ |
| 84 | + let dimension = this._dimension(); |
| 85 | + elem.style[dimension] = null; |
| 86 | + } |
| 87 | + |
| 88 | + /* -- Collapsing -- */ |
| 89 | + handleExit(elem){ |
| 90 | + let dimension = this._dimension(); |
| 91 | + |
| 92 | + elem.style[dimension] = this.props.getDimensionValue(dimension, elem) + 'px'; |
| 93 | + } |
| 94 | + |
| 95 | + handleExiting(elem){ |
| 96 | + let dimension = this._dimension(); |
| 97 | + |
| 98 | + triggerBrowserReflow(elem); |
| 99 | + elem.style[dimension] = '0'; |
| 100 | + } |
| 101 | + |
| 102 | + _dimension(){ |
| 103 | + return typeof this.props.dimension === 'function' |
| 104 | + ? this.props.dimension() |
| 105 | + : this.props.dimension; |
| 106 | + } |
| 107 | + |
| 108 | + //for testing |
| 109 | + _getTransitionInstance(){ |
| 110 | + return this.refs.transition; |
| 111 | + } |
| 112 | + |
| 113 | + _getScrollDimensionValue(elem, dimension){ |
| 114 | + return elem[`scroll${capitalize(dimension)}`] + 'px'; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +Collapse.propTypes = { |
| 119 | + /** |
| 120 | + * Collapse the Component in or out. |
| 121 | + */ |
| 122 | + in: React.PropTypes.bool, |
| 123 | + |
| 124 | + /** |
| 125 | + * Provide the durration of the animation in milliseconds, used to ensure that finishing callbacks are fired even if the |
| 126 | + * original browser transition end events are canceled. |
| 127 | + */ |
| 128 | + duration: React.PropTypes.number, |
| 129 | + |
| 130 | + /** |
| 131 | + * Specifies the dimension used when collapsing. |
| 132 | + * |
| 133 | + * _Note: Bootstrap only partially supports this! |
| 134 | + * You will need to supply your own css animation for the `.width` css class._ |
| 135 | + */ |
| 136 | + dimension: React.PropTypes.oneOfType([ |
| 137 | + React.PropTypes.oneOf(['height', 'width']), |
| 138 | + React.PropTypes.func |
| 139 | + ]), |
| 140 | + |
| 141 | + /** |
| 142 | + * A function that returns the height or width of the animating DOM node. Allows for providing some custom logic how much |
| 143 | + * Collapse component should animation in its specified dimension. |
| 144 | + * |
| 145 | + * `getDimensionValue` is called with the current dimension prop value and the DOM node. |
| 146 | + */ |
| 147 | + getDimensionValue: React.PropTypes.func, |
| 148 | + |
| 149 | + /** |
| 150 | + * A Callback fired before the component starts to expand. |
| 151 | + */ |
| 152 | + onEnter: React.PropTypes.func, |
| 153 | + |
| 154 | + /** |
| 155 | + * A Callback fired immediately after the component starts to expand. |
| 156 | + */ |
| 157 | + onEntering: React.PropTypes.func, |
| 158 | + |
| 159 | + /** |
| 160 | + * A Callback fired after the component has expanded. |
| 161 | + */ |
| 162 | + onEntered: React.PropTypes.func, |
| 163 | + |
| 164 | + /** |
| 165 | + * A Callback fired before the component starts to collapse. |
| 166 | + */ |
| 167 | + onExit: React.PropTypes.func, |
| 168 | + |
| 169 | + /** |
| 170 | + * A Callback fired immediately after the component starts to collapse. |
| 171 | + */ |
| 172 | + onExiting: React.PropTypes.func, |
| 173 | + |
| 174 | + /** |
| 175 | + * A Callback fired after the component has collapsed. |
| 176 | + */ |
| 177 | + onExited: React.PropTypes.func, |
| 178 | + |
| 179 | + /** |
| 180 | + * Specify whether the transitioning component should be unmounted (removed from the DOM) once the exit animation finishes. |
| 181 | + */ |
| 182 | + unmountOnExit: React.PropTypes.bool, |
| 183 | + |
| 184 | + /** |
| 185 | + * Specify whether the component should collapse or expand when it mounts. |
| 186 | + */ |
| 187 | + transitionAppear: React.PropTypes.bool |
| 188 | +}; |
| 189 | + |
| 190 | +Collapse.defaultProps = { |
| 191 | + in: false, |
| 192 | + duration: 300, |
| 193 | + dimension: 'height', |
| 194 | + transitionAppear: false, |
| 195 | + unmountOnExit: false, |
| 196 | + getDimensionValue |
| 197 | +}; |
| 198 | + |
| 199 | +export default Collapse; |
| 200 | + |
0 commit comments