UNPKG

react-widgets-up

Version:

An à la carte set of polished, extensible, and accessible inputs built for React

81 lines 3.27 kB
const _excluded = ["children", "className", "dropUp"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import cn from 'classnames'; import css from 'dom-helpers/css'; import getHeight from 'dom-helpers/height'; import transitionEnd from 'dom-helpers/transitionEnd'; import * as React from 'react'; import { Transition } from './Transition'; const transitionClasses = { "entering": 'rw-slide-transition-entering', "exiting": 'rw-slide-transition-exiting', "exited": 'rw-slide-transition-exited' }; class SlideDownTransition extends React.Component { constructor(...args) { super(...args); this.nodeRef = /*#__PURE__*/React.createRef(); this.setContainerHeight = () => { this.nodeRef.current.style.height = this.getHeight(this.nodeRef.current) + 'px'; }; this.clearContainerHeight = elem => { elem.style.height = ''; }; this.handleEntered = () => { this.clearContainerHeight(this.nodeRef.current); if (this.props.onEntered) this.props.onEntered(); }; this.handleEntering = () => { if (this.props.onEntering) this.props.onEntering(); }; this.handleExit = () => { this.setContainerHeight(); if (this.props.onExit) this.props.onExit(); }; this.handleExited = () => { this.clearContainerHeight(this.nodeRef.current); if (this.props.onExited) this.props.onExited(); }; this.handleTransitionEnd = (_, done) => { transitionEnd(this.nodeRef.current.firstChild, done); }; } getHeight(container) { let content = container.firstChild; let margin = parseInt(css(content, 'margin-top'), 10) + parseInt(css(content, 'margin-bottom'), 10); let old = container.style.display; let height; container.style.display = 'block'; height = (getHeight(content) || 0) + (isNaN(margin) ? 0 : margin); container.style.display = old; return height; } render() { const _this$props = this.props, { children, className, dropUp } = _this$props, props = _objectWithoutPropertiesLoose(_this$props, _excluded); return /*#__PURE__*/React.createElement(Transition, { appear: true, in: this.props.in, nodeRef: this.nodeRef, onEnter: this.setContainerHeight, onEntering: this.handleEntering, onEntered: this.handleEntered, onExit: this.handleExit, onExited: this.handleExited, addEndListener: this.handleTransitionEnd, timeout: undefined /*hack*/ }, status => /*#__PURE__*/React.createElement("div", _extends({}, props, { ref: this.nodeRef, className: cn(className, dropUp && 'rw-dropup', transitionClasses[status]) }), /*#__PURE__*/React.cloneElement(children, { className: cn('rw-slide-transition', children.props.className) }))); } } export default SlideDownTransition;