@brightleaf/elements
Version:
React UI elements styled with Bulma CSS
93 lines (73 loc) • 5.13 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Collapse = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _base = require("../base");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var Collapse = function Collapse(_ref) {
var _animation;
var className = _ref.className,
children = _ref.children,
isShown = _ref.isShown,
animationIn = _ref.animationIn,
animationOut = _ref.animationOut,
props = _objectWithoutProperties(_ref, ["className", "children", "isShown", "animationIn", "animationOut"]);
var container = (0, _react.useRef)();
var animation = (_animation = {
animated: true,
animation: true
}, _defineProperty(_animation, animationIn, isShown), _defineProperty(_animation, animationOut, !isShown), _defineProperty(_animation, 'is-hidden', !isShown), _animation);
(0, _react.useEffect)(function () {
var onEnd = function onEnd(_ref2) {
var target = _ref2.target,
animationName = _ref2.animationName;
if (animationName.indexOf('Out') > -1) {
target.classList.add('is-hidden');
}
};
var onStart = function onStart(_ref3) {
var target = _ref3.target,
animationName = _ref3.animationName;
target.classList.remove('is-hidden');
};
if (container.current) {
container.current.addEventListener('animationend', onEnd);
container.current.addEventListener('animationstart', onStart);
return function () {
container.current.removeEventListener('animationend', onEnd);
container.current.removeEventListener('animationstart', onStart);
};
}
}, [isShown]);
return _react.default.createElement(_base.Base, _extends({
ref: container,
className: (0, _classnames.default)('collapsed', className, animation)
}, props), children);
};
exports.Collapse = Collapse;
var _default = Collapse;
exports.default = _default;
Collapse.propTypes = {
as: _propTypes.default.node,
className: _propTypes.default.string,
isShown: _propTypes.default.bool,
animationIn: _propTypes.default.string,
animationOut: _propTypes.default.string
};
Collapse.defaultProps = {
as: 'div',
isShown: true,
animationIn: 'fadeIn',
animationOut: 'fadeOut'
};