UNPKG

@massds/mayflower-react

Version:

React versions of Mayflower design system UI components

92 lines (74 loc) 2.52 kB
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } import React from "react"; import PropTypes from "prop-types"; import { Portal } from "react-portal/es"; let TabBody = /*#__PURE__*/function (_React$Component) { _inheritsLoose(TabBody, _React$Component); function TabBody(props) { var _this; _this = _React$Component.call(this, props) || this; _this.state = { nodeList: [] }; if (global.MutationObserver) { _this.observer = new MutationObserver(mutations => { const tabContainer = document.getElementById(_this.props.tabContainerBodyId); mutations.forEach(mutation => { if (!mutation.target) { return; } if (_this.props.active && mutation.target.contains(tabContainer)) { _this.setState({ nodeList: [tabContainer] }); } else if (!_this.props.active && mutation.target.contains(tabContainer)) { _this.setState({ nodeList: [] }); } }); }); } return _this; } var _proto = TabBody.prototype; _proto.componentDidMount = function componentDidMount() { const nodeList = document.getElementById(this.props.tabContainerBodyId); if (nodeList) { // eslint-disable-next-line react/no-did-mount-set-state this.setState({ nodeList: [nodeList] }); } if (global.MutationObserver) { this.observer.observe(document, { attributes: true, childList: true, subtree: true }); } }; _proto.componentWillUnmount = function componentWillUnmount() { if (global.MutationObserver) { this.observer.disconnect(); } }; _proto.render = function render() { if (this.state.nodeList.length > 0 && this.props.active) { return /*#__PURE__*/React.createElement(Portal, { node: this.state.nodeList[0] }, this.props.children); } return null; }; return TabBody; }(React.Component); TabBody.propTypes = process.env.NODE_ENV !== "production" ? { /** The tab container body to render Tab children into. */ tabContainerBodyId: PropTypes.string.isRequired, /** Sets if the tab is the currently active tab or not. */ active: PropTypes.bool, /** Children passed to tab body (tab content) */ children: PropTypes.node } : {}; export default TabBody;