UNPKG

7.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = exports.NestedMenuContext = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
11
12function _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; }
13
14function _typeof(obj) { "@babel/helpers - typeof"; 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); }
15
16function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
17
18function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
19
20function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
21
22function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
23
24function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
26function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
27
28function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
29
30function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
31
32function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
33
34function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
35
36function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
37
38function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
39
40function _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; }
41
42var NestedMenuContext = React.createContext({
43 addMenuToNesting: function addMenuToNesting() {},
44 removeMenuFromNesting: function removeMenuFromNesting() {},
45 getParentMenu: function getParentMenu() {},
46 getChildMenu: function getChildMenu() {},
47 mountRef: {
48 current: null
49 }
50});
51exports.NestedMenuContext = NestedMenuContext;
52
53function isSame(a, b) {
54 if (!a || !b) {
55 return false;
56 }
57
58 return a.isSameNode(b);
59}
60
61var NestedMenus =
62/*#__PURE__*/
63function (_React$Component) {
64 _inherits(NestedMenus, _React$Component);
65
66 function NestedMenus() {
67 var _getPrototypeOf2;
68
69 var _this;
70
71 _classCallCheck(this, NestedMenus);
72
73 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
74 args[_key] = arguments[_key];
75 }
76
77 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(NestedMenus)).call.apply(_getPrototypeOf2, [this].concat(args)));
78
79 _defineProperty(_assertThisInitialized(_this), "state", {
80 menus: []
81 });
82
83 _defineProperty(_assertThisInitialized(_this), "mountRef", React.createRef());
84
85 _defineProperty(_assertThisInitialized(_this), "addMenuToNesting", function (ref) {
86 // check offsetHeight to determine if component is visible in the dom (0 means hidden)
87 // we need to do this so that when we renderAll, the hidden seo-only child-menus don't
88 // register themselves which would break the nesting logic
89 if (ref.current && ref.current.offsetHeight) {
90 _this.setState(function (state) {
91 return {
92 menus: [].concat(_toConsumableArray(state.menus), [ref])
93 };
94 });
95 }
96 });
97
98 _defineProperty(_assertThisInitialized(_this), "removeMenuFromNesting", function (ref) {
99 _this.setState(function (state) {
100 var nextMenus = state.menus.filter(function (r) {
101 return r.current;
102 }).filter(function (r) {
103 return !isSame(r.current, ref.current);
104 });
105 return {
106 menus: nextMenus
107 };
108 });
109 });
110
111 _defineProperty(_assertThisInitialized(_this), "findMenuIndexByRef", function (ref) {
112 return _this.state.menus.findIndex(function (r) {
113 return isSame(r.current, ref.current);
114 });
115 });
116
117 _defineProperty(_assertThisInitialized(_this), "getParentMenu", function (ref) {
118 var index = _this.findMenuIndexByRef(ref) - 1;
119 return _this.state.menus[index];
120 });
121
122 _defineProperty(_assertThisInitialized(_this), "getChildMenu", function (ref) {
123 var index = _this.findMenuIndexByRef(ref) + 1;
124 return _this.state.menus[index];
125 });
126
127 return _this;
128 }
129
130 _createClass(NestedMenus, [{
131 key: "render",
132 value: function render() {
133 return React.createElement(NestedMenuContext.Provider, {
134 value: {
135 addMenuToNesting: this.addMenuToNesting,
136 removeMenuFromNesting: this.removeMenuFromNesting,
137 getParentMenu: this.getParentMenu,
138 getChildMenu: this.getChildMenu,
139 mountRef: this.mountRef
140 }
141 }, React.createElement(React.Fragment, null, this.props.children, React.createElement("span", {
142 ref: this.mountRef
143 })));
144 }
145 }]);
146
147 return NestedMenus;
148}(React.Component);
149
150exports.default = NestedMenus;
\No newline at end of file