@cypress-design/react-docmenu 0.4.0 → 0.4.2

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
@@ -64,48 +64,11 @@ function __rest$1(s, e) {
64
64
  return t;
65
65
  }
66
66
 
67
- function __spreadArray(to, from, pack) {
68
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
69
- if (ar || !(i in from)) {
70
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
71
- ar[i] = from[i];
72
- }
73
- }
74
- return to.concat(ar || Array.prototype.slice.call(from));
75
- }
76
-
77
67
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
78
68
  var e = new Error(message);
79
69
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
80
70
  };
81
71
 
82
- var DocLink = function (_a) {
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;
84
- var activeLIRef = React__namespace.useRef(null);
85
- // on mount, if the item is active,
86
- // send the top position to the parent
87
- React__namespace.useEffect(function () {
88
- var _a;
89
- if (item.active) {
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);
91
- }
92
- }, [onActive, item.active]);
93
- return (React__namespace.createElement("li", { ref: activeLIRef, className: clsx('list-none p-0', {
94
- 'pl-[16px]': depth >= 0,
95
- }) },
96
- React__namespace.createElement(LinkComponent, { className: clsx('group relative inline-block pl-[24px]', {
97
- 'text-indigo-500': item.active,
98
- 'py-[8px] text-[16px] leading-[24px]': depth < 0,
99
- 'py-[12px] leading-[20px] text-[14px]': depth >= 0,
100
- }), href: item.href },
101
- depth >= 0 ? (React__namespace.createElement("div", { className: clsx('absolute w-[4px] z-10 top-[10%] h-[80%] rounded-full hidden', {
102
- 'group-hover:block bg-gray-300': !item.active && collapsible,
103
- }), style: {
104
- left: "-".concat(18.5 + depth * 8, "px"),
105
- } })) : null,
106
- item.label)));
107
- };
108
-
109
72
  /******************************************************************************
110
73
  Copyright (c) Microsoft Corporation.
111
74
 
@@ -212,51 +175,105 @@ var IconChevronDownSmall = function (props) { return React__namespace.createElem
212
175
  /* <wind-keep class="filter icon-light-secondary icon-dark-secondary"> */
213
176
 
214
177
  const classes = {
215
- button: 'flex leading-[24px] py-[10px] items-center relative w-full',
178
+ button: 'flex leading-[24px] py-[10px] items-center relative w-full text-start',
216
179
  topButton: 'leading-[24px] text-[16px] pl-[24px] font-medium',
217
180
  leafButton: 'leading-[20px] text-[14px] pl-[40px]',
218
181
  };
219
182
 
220
- var DocGroup = function (_a) {
221
- var _b;
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];
225
- React__namespace.useEffect(function () {
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];
235
- React__namespace.useEffect(function () {
236
- var activeItem = group.items.findIndex(function (item) { return 'href' in item && item.active; });
237
- if (activeItem >= 0) {
238
- setActiveTop(itemsHeights.slice(0, activeItem).reduce(function (a, b) { return a + b; }, 1) * 44);
183
+ var DocLink = React__namespace.forwardRef(function (_a, ref) {
184
+ var item = _a.item, active = _a.active, collapsible = _a.collapsible, _b = _a.depth, depth = _b === void 0 ? -1 : _b, onActive = _a.onActive, _c = _a.LinkComponent, LinkComponent = _c === void 0 ? 'a' : _c;
185
+ var activeLIRef = React__namespace.useRef(null);
186
+ // on mount, if the item is active,
187
+ // send the top position to the parent
188
+ React__namespace.useEffect(setActiveMarkerPosition, [onActive, active]);
189
+ function setActiveMarkerPosition() {
190
+ var _a;
191
+ if (active) {
192
+ var box = (_a = activeLIRef === null || activeLIRef === void 0 ? void 0 : activeLIRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
193
+ onActive === null || onActive === void 0 ? void 0 : onActive({
194
+ top: (box === null || box === void 0 ? void 0 : box.top) || 0,
195
+ height: (box === null || box === void 0 ? void 0 : box.height) || 0,
196
+ });
239
197
  }
240
- }, [itemsHeights, group.items]);
198
+ }
199
+ React__namespace.useImperativeHandle(ref, function () { return ({
200
+ setActiveMarkerPosition: setActiveMarkerPosition,
201
+ }); });
202
+ return (React__namespace.createElement("li", { ref: activeLIRef, className: clsx('list-none p-0', {
203
+ 'pl-[16px]': depth >= 0,
204
+ }) },
205
+ React__namespace.createElement(LinkComponent, { className: clsx('group relative inline-block pl-[24px]', {
206
+ 'text-indigo-500': active,
207
+ 'py-[8px] text-[16px] leading-[24px]': depth < 0,
208
+ 'py-[12px] leading-[20px] text-[14px]': depth >= 0,
209
+ }), href: item.href },
210
+ depth >= 0 ? (React__namespace.createElement("div", { className: clsx('absolute w-[4px] z-10 top-[4px] bottom-[4px] rounded-full hidden', {
211
+ 'group-hover:block bg-gray-300': !active && collapsible,
212
+ }), style: {
213
+ left: "-".concat(18.5 + depth * 8, "px"),
214
+ } })) : null,
215
+ item.label)));
216
+ });
217
+
218
+ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
219
+ var _b;
220
+ var group = _a.group, activePath = _a.activePath, collapsible = _a.collapsible, _c = _a.depth, depth = _c === void 0 ? 0 : _c, onActivePosition = _a.onActivePosition, updateMarkerPosition = _a.updateMarkerPosition, _d = _a.LinkComponent, LinkComponent = _d === void 0 ? 'a' : _d, hideMarker = _a.hideMarker;
221
+ var _e = React__namespace.useState(group.collapsed !== true), open = _e[0], setOpen = _e[1];
222
+ var $groupElements = React__namespace.useRef(null);
241
223
  function toggleMenu(open) {
242
224
  if (!collapsible)
243
225
  return;
244
226
  setOpen(open);
245
227
  }
246
- var onSetHeightCallback = React__namespace.useCallback(function (height) {
247
- setItemsHeights(function (prev) {
248
- var newHeights = __spreadArray([], prev, true);
249
- newHeights[index] = height;
250
- return newHeights;
228
+ var hasActiveItemRecursively = React__namespace.useCallback(function (items) {
229
+ if (items === void 0) { items = group.items; }
230
+ return items.some(function (item) {
231
+ if ('items' in item) {
232
+ return hasActiveItemRecursively(item.items);
233
+ }
234
+ return item.href === activePath;
251
235
  });
252
- }, [setItemsHeights, index]);
236
+ }, [group.items, activePath]);
253
237
  var Head = collapsible ? 'button' : group.href ? 'a' : 'div';
238
+ var setActivePosition = React__namespace.useCallback(function (_a) {
239
+ var top = _a.top, height = _a.height;
240
+ if (open) {
241
+ onActivePosition({ top: top, height: height });
242
+ }
243
+ }, [onActivePosition, open]);
244
+ function reTriggerSetActiveGroup() {
245
+ var _a;
246
+ (_a = $groupElements.current) === null || _a === void 0 ? void 0 : _a.reTriggerSetActiveGroup();
247
+ }
248
+ React__namespace.useEffect(function () {
249
+ if (hasActiveItemRecursively()) {
250
+ if (open) {
251
+ reTriggerSetActiveGroup();
252
+ }
253
+ else {
254
+ hideMarker();
255
+ }
256
+ }
257
+ else {
258
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
259
+ }
260
+ }, [open, updateMarkerPosition, hideMarker, hasActiveItemRecursively]);
261
+ React__namespace.useImperativeHandle(ref, function () { return ({
262
+ reTriggerSetActiveGroup: reTriggerSetActiveGroup,
263
+ }); });
264
+ var onUpdateMarkerPosition = React__namespace.useCallback(function () {
265
+ if (hasActiveItemRecursively()) {
266
+ reTriggerSetActiveGroup();
267
+ }
268
+ else {
269
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
270
+ }
271
+ }, [hasActiveItemRecursively, updateMarkerPosition]);
254
272
  return (React__namespace.createElement(React__namespace.Fragment, null,
255
- React__namespace.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {
256
- 'text-indigo-500': group.active
257
- },
273
+ React__namespace.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {},
258
274
  _b[classes.topButton] = depth === 0,
259
275
  _b[classes.leafButton] = depth,
276
+ _b['text-indigo-500'] = activePath === group.href,
260
277
  _b)) },
261
278
  collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx('absolute transform transition-transform left-0', {
262
279
  'rotate-0': open,
@@ -264,31 +281,75 @@ var DocGroup = function (_a) {
264
281
  'ml-[16px]': depth,
265
282
  }) })) : null,
266
283
  group.label),
267
- collapsible &&
268
- depth >= 0 &&
269
- open &&
270
- group.items.some(function (item) { return 'href' in item && item.active; }) ? (React__namespace.createElement("div", { className: "absolute h-[36px] w-[4px] z-10 rounded-full bg-indigo-500 transition-all duration-300 ml-[6px] mt-[4px]", style: {
271
- top: "".concat(activeTop, "px"),
272
- left: "".concat(depth === 0 ? 0.5 : -(depth * 8) - 0.5, "px"),
273
- } })) : null,
274
- React__namespace.createElement("ul", { className: clsx('ml-[8px] list-none p-0', {
275
- 'border-l border-gray-100': depth === 0 && collapsible,
276
- hidden: !open,
277
- }) }, group.items.map(function (item, index) {
278
- return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
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 }));
280
- }))));
281
- };
282
-
283
- var DocMenu = function (_a) {
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"]);
285
- return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', rest.className) }), items.map(function (item, index) {
284
+ React__namespace.createElement(DocGroupElements, { ref: $groupElements, className: !open ? 'hidden' : undefined, items: group.items, activePath: activePath, collapsible: collapsible, depth: depth, onActivePosition: setActivePosition, updateMarkerPosition: onUpdateMarkerPosition, LinkComponent: LinkComponent, hideMarker: hideMarker })));
285
+ });
286
+ var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
287
+ var items = _a.items, activePath = _a.activePath, collapsible = _a.collapsible, _b = _a.depth, depth = _b === void 0 ? 0 : _b, onActivePosition = _a.onActivePosition, updateMarkerPosition = _a.updateMarkerPosition, LinkComponent = _a.LinkComponent, hideMarker = _a.hideMarker, className = _a.className, rest = __rest$1(_a, ["items", "activePath", "collapsible", "depth", "onActivePosition", "updateMarkerPosition", "LinkComponent", "hideMarker", "className"]);
288
+ var $groups = React__namespace.useRef([]);
289
+ var $items = React__namespace.useRef([]);
290
+ var hasActiveItemRecursively = React__namespace.useCallback(function (localItems) {
291
+ if (localItems === void 0) { localItems = items; }
292
+ return localItems.some(function (item) {
293
+ if ('items' in item) {
294
+ return hasActiveItemRecursively(item.items);
295
+ }
296
+ return item.href === activePath;
297
+ });
298
+ }, [items, activePath]);
299
+ function reTriggerSetActiveGroup() {
300
+ $groups.current.forEach(function (group) {
301
+ group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
302
+ });
303
+ $items.current.forEach(function (item) {
304
+ item === null || item === void 0 ? void 0 : item.setActiveMarkerPosition();
305
+ });
306
+ }
307
+ React__namespace.useImperativeHandle(ref, function () { return ({
308
+ reTriggerSetActiveGroup: reTriggerSetActiveGroup,
309
+ }); });
310
+ var onUpdateMarkerPosition = React__namespace.useCallback(function () {
311
+ if (hasActiveItemRecursively()) {
312
+ reTriggerSetActiveGroup();
313
+ }
314
+ else {
315
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
316
+ }
317
+ }, [hasActiveItemRecursively, updateMarkerPosition]);
318
+ return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', className, {
319
+ 'border-l border-gray-100': depth === 0 && collapsible,
320
+ 'ml-[8px]': depth >= 0,
321
+ }) }), items.map(function (item, index) {
286
322
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
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 }));
323
+ React__namespace.createElement(DocGroup, { ref: function (el) {
324
+ $groups.current[index] = el;
325
+ }, group: item, activePath: activePath, depth: depth + 1, collapsible: collapsible, LinkComponent: LinkComponent, hideMarker: hideMarker, onActivePosition: onActivePosition, updateMarkerPosition: onUpdateMarkerPosition }))) : (React__namespace.createElement(DocLink, { ref: function (el) {
326
+ $items.current[index] = el;
327
+ }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: onActivePosition, LinkComponent: LinkComponent }));
288
328
  })));
329
+ });
330
+
331
+ var DocMenu = function (_a) {
332
+ var items = _a.items, _b = _a.activePath, activePath = _b === void 0 ? '<unknown>' : _b, _c = _a.collapsible, collapsible = _c === void 0 ? true : _c, _d = _a.LinkComponent, LinkComponent = _d === void 0 ? 'a' : _d, rest = __rest$1(_a, ["items", "activePath", "collapsible", "LinkComponent"]);
333
+ var _e = React__namespace.useState(0), activeTop = _e[0], setActiveTop = _e[1];
334
+ var _f = React__namespace.useState(36), activeHeight = _f[0], setActiveHeight = _f[1];
335
+ var _g = React__namespace.useState(false), showMarker = _g[0], setShowMarker = _g[1];
336
+ var container = React__namespace.useRef(null);
337
+ var setActivePosition = React__namespace.useCallback(function (opts) {
338
+ var _a;
339
+ var containerTop = ((_a = container.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().top) || 0;
340
+ setShowMarker(true);
341
+ setActiveTop(opts.top - containerTop);
342
+ setActiveHeight(opts.height);
343
+ }, []);
344
+ return (React__namespace.createElement("div", { ref: container, className: "relative" },
345
+ showMarker && collapsible ? (React__namespace.createElement("div", { className: "absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 transition-all duration-300 ml-[6.5px] mt-[4px]", style: {
346
+ top: "".concat(activeTop + 4, "px"),
347
+ height: "".concat(activeHeight - 8, "px"),
348
+ } })) : null,
349
+ React__namespace.createElement(DocGroupElements, __assign$1({}, rest, { className: !open ? 'hidden' : undefined, items: items, activePath: activePath, collapsible: collapsible, depth: -1, onActivePosition: setActivePosition, LinkComponent: LinkComponent, hideMarker: function () { return setShowMarker(false); } }))));
289
350
  };
290
351
 
291
352
  module.exports = DocMenu;
292
353
  //# sourceMappingURL=index.umd.js.map
293
354
 
294
- /* <wind-keep class="list-none p-0 relative pl-[16px] inline-block pl-[24px] text-indigo-500 py-[8px] text-[16px] leading-[24px] py-[12px] leading-[20px] text-[14px] absolute w-[4px] z-10 top-[10%] h-[80%] rounded-full hidden group-hover:block bg-gray-300 transform transition-transform left-0 rotate-0 -rotate-90 ml-[16px] h-[36px] bg-indigo-500 transition-all duration-300 ml-[6px] mt-[4px] ml-[8px] border-l border-gray-100 flex py-[10px] items-center w-full font-medium pl-[40px]"> */
355
+ /* <wind-keep class="container relative absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 transition-all duration-300 ml-[6.5px] mt-[4px] hidden text-indigo-500 transform transition-transform left-0 rotate-0 -rotate-90 ml-[16px] list-none p-0 border-l border-gray-100 ml-[8px] pl-[16px] inline-block pl-[24px] py-[8px] text-[16px] leading-[24px] py-[12px] leading-[20px] text-[14px] z-10 top-[4px] bottom-[4px] group-hover:block bg-gray-300 flex py-[10px] items-center w-full text-start font-medium pl-[40px]"> */