@pantheon-systems/design-toolkit-react
Version:
Pantheon's React Design Toolkit
74 lines (69 loc) • 3.22 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import PropTypes from 'prop-types';
import { useSpringRef, useSpring, useChain, animated } from 'react-spring';
import useMeasure from '../hooks/useMeasure.js';
import { jsx } from 'react/jsx-runtime';
var _excluded = ["children", "isShown", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var ExpandAnimation = function ExpandAnimation(_ref) {
var children = _ref.children,
isShown = _ref.isShown,
className = _ref.className,
rest = _objectWithoutProperties(_ref, _excluded);
// We will use this to animate whatever height the alert actually is at any time
var _useMeasure = useMeasure(),
_useMeasure2 = _slicedToArray(_useMeasure, 2),
bind = _useMeasure2[0],
height = _useMeasure2[1].height;
/*
- This is a two stage animation that needs to reverse itself, hence the use of useChain.
When showing, the height goes, then the opacity. When hiding it's the opposite.
- Note that you cannot change value types, eg. cannot go from 0 -> "1rem".
It must be "0rem" -> "1rem". Witholding a value type will assume "px".
*/
var heightRef = useSpringRef();
var heightProps = useSpring({
ref: heightRef,
from: {
height: 0
},
to: {
height: isShown ? height : 0
}
});
var opacityRef = useSpringRef();
var opacityProps = useSpring({
ref: opacityRef,
from: {
opacity: 0
},
to: {
opacity: isShown ? 1 : 0
}
});
useChain(isShown ? [heightRef, opacityRef] : [opacityRef, heightRef], [0, 0.3]);
return /*#__PURE__*/jsx(animated.div, _objectSpread(_objectSpread({
className: className,
style: _objectSpread(_objectSpread({}, heightProps), opacityProps)
}, rest), {}, {
children: /*#__PURE__*/jsx("div", _objectSpread(_objectSpread({
className: "margin-collapse"
}, bind), {}, {
children: children
}))
}));
};
ExpandAnimation.defaultProps = {
className: ''
};
ExpandAnimation.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
isShown: PropTypes.bool.isRequired
};
var ExpandAnimation$1 = ExpandAnimation;
export { ExpandAnimation$1 as default };
//# sourceMappingURL=ExpandAnimation.js.map