@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
75 lines • 2.64 kB
JavaScript
function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
import React from "react";
import PropTypes from "prop-types";
import { Portal } from "react-portal/es";
let TabBody = /*#__PURE__*/function (_React$Component) {
function TabBody(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_this.state = {
nodeList: []
};
if (globalThis.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;
}
_inheritsLoose(TabBody, _React$Component);
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 (globalThis.MutationObserver) {
this.observer.observe(document, {
attributes: true,
childList: true,
subtree: true
});
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (globalThis.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;