@cypress-design/react-docmenu 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.umd.js CHANGED
@@ -38,7 +38,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
38
38
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
39
39
  PERFORMANCE OF THIS SOFTWARE.
40
40
  ***************************************************************************** */
41
- /* global Reflect, Promise */
41
+ /* global Reflect, Promise, SuppressedError, Symbol */
42
42
 
43
43
 
44
44
  var __assign$1 = function() {
@@ -72,10 +72,15 @@ function __spreadArray(to, from, pack) {
72
72
  }
73
73
  }
74
74
  return to.concat(ar || Array.prototype.slice.call(from));
75
- }
75
+ }
76
+
77
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
78
+ var e = new Error(message);
79
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
80
+ };
76
81
 
77
82
  var DocLink = function (_a) {
78
- var item = _a.item, collapsible = _a.collapsible, _b = _a.depth, depth = _b === void 0 ? -1 : _b, onActive = _a.onActive;
83
+ var item = _a.item, collapsible = _a.collapsible, _b = _a.depth, depth = _b === void 0 ? -1 : _b, onActive = _a.onActive, _c = _a.LinkComponent, LinkComponent = _c === void 0 ? 'a' : _c;
79
84
  var activeLIRef = React__namespace.useRef(null);
80
85
  // on mount, if the item is active,
81
86
  // send the top position to the parent
@@ -84,11 +89,11 @@ var DocLink = function (_a) {
84
89
  if (item.active) {
85
90
  onActive === null || onActive === void 0 ? void 0 : onActive(((_a = activeLIRef === null || activeLIRef === void 0 ? void 0 : activeLIRef.current) === null || _a === void 0 ? void 0 : _a.offsetTop) || 0);
86
91
  }
87
- }, []);
92
+ }, [onActive, item.active]);
88
93
  return (React__namespace.createElement("li", { ref: activeLIRef, className: clsx('list-none p-0', {
89
94
  'pl-[16px]': depth >= 0,
90
95
  }) },
91
- React__namespace.createElement("a", { className: clsx('group relative inline-block pl-[24px]', {
96
+ React__namespace.createElement(LinkComponent, { className: clsx('group relative inline-block pl-[24px]', {
92
97
  'text-indigo-500': item.active,
93
98
  'py-[8px] text-[16px] leading-[24px]': depth < 0,
94
99
  'py-[12px] leading-[20px] text-[14px]': depth >= 0,
@@ -98,7 +103,7 @@ var DocLink = function (_a) {
98
103
  }), style: {
99
104
  left: "-".concat(18.5 + depth * 8, "px"),
100
105
  } })) : null,
101
- item.text)));
106
+ item.label)));
102
107
  };
103
108
 
104
109
  /******************************************************************************
@@ -115,7 +120,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
115
120
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
116
121
  PERFORMANCE OF THIS SOFTWARE.
117
122
  ***************************************************************************** */
118
- /* global Reflect, Promise */
123
+ /* global Reflect, Promise, SuppressedError, Symbol */
119
124
 
120
125
 
121
126
  var __assign = function() {
@@ -139,7 +144,12 @@ function __rest(s, e) {
139
144
  t[p[i]] = s[p[i]];
140
145
  }
141
146
  return t;
142
- }
147
+ }
148
+
149
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
150
+ var e = new Error(message);
151
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
152
+ };
143
153
 
144
154
  var compileReactIconProperties = function (_a) {
145
155
  var body = _a.body, defs = _a.defs, compiledClasses = _a.compiledClasses, size = _a.size, name = _a.name, attributes = __rest(_a, ["body", "defs", "compiledClasses", "size", "name"]);
@@ -153,6 +163,7 @@ var compileReactIconProperties = function (_a) {
153
163
  }
154
164
  return newAttributes;
155
165
  }, {});
166
+ // eslint-disable-next-line react-hooks/rules-of-hooks
156
167
  React__namespace.useEffect(function () {
157
168
  var iconFileId = "".concat(name, "_").concat(size);
158
169
  if (defs) {
@@ -166,7 +177,7 @@ var compileReactIconProperties = function (_a) {
166
177
  svgElement.innerHTML = defs;
167
178
  document.body.appendChild(svgElement);
168
179
  }
169
- }, [defs]);
180
+ }, [defs, name, size]);
170
181
  var componentProps = __assign({ width: size, height: size, viewBox: "0 0 ".concat(size, " ").concat(size), fill: 'none', dangerouslySetInnerHTML: {
171
182
  __html: body,
172
183
  }, className: undefined }, filteredAttributes);
@@ -208,31 +219,37 @@ const classes = {
208
219
 
209
220
  var DocGroup = function (_a) {
210
221
  var _b;
211
- var group = _a.group, collapsible = _a.collapsible, _c = _a.depth, depth = _c === void 0 ? 0 : _c, setHeight = _a.setHeight;
212
- var _d = React__namespace.useState(depth === 0), open = _d[0], setOpen = _d[1];
213
- var _e = React__namespace.useState(Array(group.items.length).fill(1)), itemsHeights = _e[0], setItemsHeights = _e[1];
222
+ var group = _a.group, collapsible = _a.collapsible, _c = _a.depth, depth = _c === void 0 ? 0 : _c, setHeight = _a.setHeight, index = _a.index, _d = _a.LinkComponent, LinkComponent = _d === void 0 ? 'a' : _d;
223
+ var _e = React__namespace.useState(depth === 0), open = _e[0], setOpen = _e[1];
224
+ var _f = React__namespace.useState(Array(group.items.length).fill(1)), itemsHeights = _f[0], setItemsHeights = _f[1];
214
225
  React__namespace.useEffect(function () {
215
- setHeight === null || setHeight === void 0 ? void 0 : setHeight(open ? itemsHeights.reduce(function (a, b) { return a + b; }, 1) : 1);
216
- }, [itemsHeights, open]);
217
- var _f = React__namespace.useState(0), activeTop = _f[0], setActiveTop = _f[1];
226
+ // if this group contains other groups, ignore this effect
227
+ // it will be triggered by a leaf and trickle up
228
+ if (group.items.some(function (item) { return 'items' in item; })) {
229
+ return;
230
+ }
231
+ var newHeight = open ? itemsHeights.reduce(function (a, b) { return a + b; }, 1) : 1;
232
+ setHeight === null || setHeight === void 0 ? void 0 : setHeight(newHeight);
233
+ }, [itemsHeights, open, setHeight, group.items, group.label]);
234
+ var _g = React__namespace.useState(0), activeTop = _g[0], setActiveTop = _g[1];
218
235
  React__namespace.useEffect(function () {
219
236
  var activeItem = group.items.findIndex(function (item) { return 'href' in item && item.active; });
220
237
  if (activeItem >= 0) {
221
238
  setActiveTop(itemsHeights.slice(0, activeItem).reduce(function (a, b) { return a + b; }, 1) * 44);
222
239
  }
223
- }, [itemsHeights]);
240
+ }, [itemsHeights, group.items]);
224
241
  function toggleMenu(open) {
225
242
  if (!collapsible)
226
243
  return;
227
244
  setOpen(open);
228
245
  }
229
- function onSetHeight(index, height) {
246
+ var onSetHeightCallback = React__namespace.useCallback(function (height) {
230
247
  setItemsHeights(function (prev) {
231
248
  var newHeights = __spreadArray([], prev, true);
232
249
  newHeights[index] = height;
233
250
  return newHeights;
234
251
  });
235
- }
252
+ }, [setItemsHeights, index]);
236
253
  var Head = collapsible ? 'button' : group.href ? 'a' : 'div';
237
254
  return (React__namespace.createElement(React__namespace.Fragment, null,
238
255
  React__namespace.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {
@@ -246,7 +263,7 @@ var DocGroup = function (_a) {
246
263
  '-rotate-90': !open,
247
264
  'ml-[16px]': depth,
248
265
  }) })) : null,
249
- group.text),
266
+ group.label),
250
267
  collapsible &&
251
268
  depth >= 0 &&
252
269
  open &&
@@ -259,15 +276,15 @@ var DocGroup = function (_a) {
259
276
  hidden: !open,
260
277
  }) }, group.items.map(function (item, index) {
261
278
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
262
- React__namespace.createElement(DocGroup, { group: item, depth: depth + 1, setHeight: function (height) { return onSetHeight(index, height); }, collapsible: collapsible }))) : (React__namespace.createElement(DocLink, { key: index, item: item, collapsible: collapsible, depth: depth, onActive: function (top) { return setActiveTop(top); } }));
279
+ React__namespace.createElement(DocGroup, { group: item, depth: depth + 1, setHeight: onSetHeightCallback, index: index, collapsible: collapsible, LinkComponent: LinkComponent }))) : (React__namespace.createElement(DocLink, { key: index, item: item, collapsible: collapsible, depth: depth, onActive: function (top) { return setActiveTop(top); }, LinkComponent: LinkComponent }));
263
280
  }))));
264
281
  };
265
282
 
266
283
  var DocMenu = function (_a) {
267
- var items = _a.items, _b = _a.collapsible, collapsible = _b === void 0 ? true : _b, rest = __rest$1(_a, ["items", "collapsible"]);
284
+ var items = _a.items, _b = _a.collapsible, collapsible = _b === void 0 ? true : _b, _c = _a.LinkComponent, LinkComponent = _c === void 0 ? 'a' : _c, rest = __rest$1(_a, ["items", "collapsible", "LinkComponent"]);
268
285
  return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', rest.className) }), items.map(function (item, index) {
269
286
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
270
- React__namespace.createElement(DocGroup, { group: item, collapsible: collapsible }))) : (React__namespace.createElement(DocLink, { key: index, item: item, collapsible: collapsible }));
287
+ React__namespace.createElement(DocGroup, { index: index, group: item, collapsible: collapsible, LinkComponent: LinkComponent }))) : (React__namespace.createElement(DocLink, { key: index, item: item, collapsible: collapsible, LinkComponent: LinkComponent }));
271
288
  })));
272
289
  };
273
290