@difizen/mana-react
Version:
118 lines • 5.76 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import classnames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import * as React from 'react';
import "./styles/index.less";
import "../menu/styles/index.less";
import { MenubarContext } from "./context";
import { MenubarItem } from "./item";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var defaultMenubarProps = {
prefixCls: 'mana'
};
export var Menubar = function Menubar(props) {
var removeDocEventRef = React.useRef(undefined);
var menubarContentRef = React.useRef(null);
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
active = _React$useState2[0],
setActive = _React$useState2[1];
var _React$useState3 = React.useState(undefined),
_React$useState4 = _slicedToArray(_React$useState3, 2),
activeElementRef = _React$useState4[0],
setActiveElementRef = _React$useState4[1];
var unbindDocEvent = function unbindDocEvent() {
if (removeDocEventRef.current) {
removeDocEventRef.current();
removeDocEventRef.current = undefined;
}
};
React.useEffect(function () {
return function () {
unbindDocEvent();
};
}, []);
var activeMenubar = function activeMenubar() {
setActive(true);
if (!removeDocEventRef.current) {
var onDocumentMouseDown = function onDocumentMouseDown(e) {
var _activeElementRef$cur;
if (e.defaultPrevented) {
return;
}
var nativeEvent = e.nativeEvent;
if (activeElementRef !== null && activeElementRef !== void 0 && (_activeElementRef$cur = activeElementRef.current) !== null && _activeElementRef$cur !== void 0 && _activeElementRef$cur.contains(nativeEvent.target)) {
return;
}
setActive(false);
setActiveElementRef(undefined);
unbindDocEvent();
};
var onDocumentClick = function onDocumentClick(e) {
var _activeElementRef$cur2;
if (e.defaultPrevented) {
return;
}
var nativeEvent = e.nativeEvent;
if (activeElementRef !== null && activeElementRef !== void 0 && (_activeElementRef$cur2 = activeElementRef.current) !== null && _activeElementRef$cur2 !== void 0 && _activeElementRef$cur2.contains(nativeEvent.target)) {
return;
}
setActive(false);
setActiveElementRef(undefined);
unbindDocEvent();
};
var _addEventListener = addEventListener(document.documentElement, 'mousedown', onDocumentMouseDown),
removeMouseDown = _addEventListener.remove;
var _addEventListener2 = addEventListener(document.documentElement, 'click', onDocumentClick),
removeClick = _addEventListener2.remove;
var remove = function remove() {
removeMouseDown();
removeClick();
};
removeDocEventRef.current = remove;
}
};
var inContent = function inContent(element) {
if (menubarContentRef && menubarContentRef.current) {
return menubarContentRef.current.contains(element);
}
return false;
};
var _props$prefixCls = props.prefixCls,
prefixCls = _props$prefixCls === void 0 ? defaultMenubarProps.prefixCls : _props$prefixCls,
className = props.className,
children = props.children,
extra = props.extra;
var baseCls = "".concat(prefixCls, "-menubar");
var contextValue = {
prefixCls: baseCls,
activeMenubar: activeMenubar,
menubarActived: active === true,
inContent: inContent,
activeElementRef: activeElementRef,
setActiveElementRef: setActiveElementRef
};
return /*#__PURE__*/_jsx("div", {
className: classnames(baseCls, className),
children: /*#__PURE__*/_jsxs("div", {
className: "".concat(baseCls, "-content"),
children: [/*#__PURE__*/_jsx("div", {
className: "".concat(baseCls, "-content-inner"),
ref: menubarContentRef,
children: /*#__PURE__*/_jsx(MenubarContext.Provider, {
value: contextValue,
children: children
})
}), extra && /*#__PURE__*/_jsx("div", {
className: "".concat(baseCls, "-content-extras"),
children: extra
})]
})
});
};
Menubar.Item = MenubarItem;