@cypress-design/react-docmenu 0.5.2 → 0.5.3

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
@@ -182,20 +182,16 @@ const classes = {
182
182
  openListBorderLeft: 'absolute left-[8px] top-0 w-[1px] h-full bg-gray-100 dark:bg-gray-900',
183
183
  };
184
184
 
185
- var MarkerIsMovingContext = React.createContext({
186
- setMarkerIsMoving: function () { },
187
- markerIsMoving: false,
188
- });
189
-
190
185
  var DocLink = React__namespace.forwardRef(function (_a, ref) {
191
- 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;
186
+ var item = _a.item, depth = _a.depth, markerIsMoving = _a.markerIsMoving, context = _a.context, onActive = _a.onActive, LinkComponent = _a.LinkComponent;
192
187
  var activeLIRef = React__namespace.useRef(null);
193
- var markerIsMoving = React__namespace.useContext(MarkerIsMovingContext).markerIsMoving;
188
+ var collapsible = context.collapsible, activePath = context.activePath;
189
+ var active = item.href === activePath;
194
190
  var setActiveMarkerPosition = function () {
195
191
  var _a;
196
192
  if (active) {
197
193
  var box = (_a = activeLIRef === null || activeLIRef === void 0 ? void 0 : activeLIRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
198
- onActive === null || onActive === void 0 ? void 0 : onActive({
194
+ onActive({
199
195
  top: (box === null || box === void 0 ? void 0 : box.top) || 0,
200
196
  height: (box === null || box === void 0 ? void 0 : box.height) || 0,
201
197
  });
@@ -209,7 +205,7 @@ var DocLink = React__namespace.forwardRef(function (_a, ref) {
209
205
  }); });
210
206
  return (React__namespace.createElement("li", { ref: activeLIRef, className: "list-none p-0" },
211
207
  React__namespace.createElement(LinkComponent, { className: clsx('group relative block w-full pl-[24px]', {
212
- 'text-indigo-500': active,
208
+ 'text-indigo-500 dark:text-indigo-400': active,
213
209
  'text-gray-700 dark:text-gray-500': !active,
214
210
  'py-[8px] text-[16px] leading-[24px]': depth < 0,
215
211
  'leading-[20px] text-[14px] py-[12px]': depth >= 0,
@@ -219,33 +215,42 @@ var DocLink = React__namespace.forwardRef(function (_a, ref) {
219
215
  depth >= 0 ? (React__namespace.createElement("div", { className: clsx('left-[6.5px] absolute top-[4px] bottom-[4px] w-[4px] z-10 rounded-full', {
220
216
  hidden: !markerIsMoving || !active,
221
217
  'group-hover:block bg-gray-300': !active && collapsible,
222
- 'bg-indigo-500': active && markerIsMoving,
218
+ 'bg-indigo-500 dark:bg-indigo-400': active && markerIsMoving,
223
219
  }) })) : null,
224
220
  item.label)));
225
221
  });
226
222
 
223
+ var hasActiveItemRecursively = function (items, localActivePath) {
224
+ return items.some(function (item) {
225
+ if ('items' in item) {
226
+ return hasActiveItemRecursively(item.items, localActivePath);
227
+ }
228
+ return item.href === localActivePath;
229
+ });
230
+ };
227
231
  var DocGroup = React__namespace.forwardRef(function (_a, ref) {
228
232
  var _b;
229
- 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;
230
- var _e = React__namespace.useState(group.collapsed !== true), open = _e[0], setOpen = _e[1];
233
+ var group = _a.group, _c = _a.depth, depth = _c === void 0 ? 0 : _c, onActivePosition = _a.onActivePosition, markerIsMoving = _a.markerIsMoving, context = _a.context, updateMarkerPosition = _a.updateMarkerPosition, _d = _a.LinkComponent, LinkComponent = _d === void 0 ? 'a' : _d;
234
+ var setMarkerIsMoving = context.setMarkerIsMoving, activePath = context.activePath, collapsible = context.collapsible, hideMarker = context.hideMarker;
235
+ var hasActiveItemRecursivelyMemo = React__namespace.useMemo(function () {
236
+ return hasActiveItemRecursively(group.items, activePath);
237
+ }, [group.items, activePath]);
238
+ var _e = React__namespace.useState(!group.collapsed), open = _e[0], setOpen = _e[1];
239
+ var _f = React__namespace.useState(), markerIsMovingTimeout = _f[0], setMarkerIsMovingTimeout = _f[1];
231
240
  var $groupElements = React__namespace.useRef(null);
232
- var $listWrapper = React__namespace.useRef(null);
233
- function toggleMenu(localOpen) {
234
- if (!collapsible)
241
+ var toggleMenu = function (localOpen) {
242
+ if (!collapsible || markerIsMoving)
235
243
  return;
236
- hideShowAbsoluteMarker(localOpen);
244
+ setMarkerIsMoving(true);
245
+ // on browsers than do not support transitionend event
246
+ // we need to set a timeout to remove the markerIsMoving
247
+ var timeout = setTimeout(function () {
248
+ setMarkerIsMoving(false);
249
+ }, 350);
250
+ setMarkerIsMovingTimeout(timeout);
237
251
  setOpen(localOpen);
238
252
  readjustMarkerPosition(localOpen);
239
- }
240
- var hasActiveItemRecursively = React__namespace.useCallback(function (items) {
241
- if (items === void 0) { items = group.items; }
242
- return items.some(function (item) {
243
- if ('items' in item) {
244
- return hasActiveItemRecursively(item.items);
245
- }
246
- return item.href === activePath;
247
- });
248
- }, [group.items, activePath]);
253
+ };
249
254
  var Head = collapsible ? 'button' : group.href ? 'a' : 'div';
250
255
  var setActivePosition = React__namespace.useCallback(function (_a) {
251
256
  var top = _a.top, height = _a.height;
@@ -253,7 +258,6 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
253
258
  onActivePosition({ top: top, height: height });
254
259
  }
255
260
  }, [onActivePosition, open]);
256
- var setMarkerIsMoving = React__namespace.useContext(MarkerIsMovingContext).setMarkerIsMoving;
257
261
  function reTriggerSetActiveGroupParent() {
258
262
  var _a;
259
263
  (_a = $groupElements.current) === null || _a === void 0 ? void 0 : _a.reTriggerSetActiveGroup();
@@ -263,20 +267,17 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
263
267
  * We replace the absolute marker by one in the link. After the transition, the
264
268
  * marker is replaced by the absolute one again.
265
269
  */
266
- function hideShowAbsoluteMarker(localOpen) {
267
- var _a, _b;
268
- (_a = $listWrapper.current) === null || _a === void 0 ? void 0 : _a.addEventListener('transitionstart', function () {
269
- setMarkerIsMoving(true);
270
- }, { once: true });
271
- (_b = $listWrapper.current) === null || _b === void 0 ? void 0 : _b.addEventListener('transitionend', function () {
272
- if (localOpen || !hasActiveItemRecursively()) {
273
- updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
274
- }
275
- setMarkerIsMoving(false);
276
- }, { once: true });
277
- }
270
+ var restoreActiveMarkerAfterTransition = function () {
271
+ if (!markerIsMovingTimeout) {
272
+ clearTimeout(markerIsMovingTimeout);
273
+ }
274
+ if (open || !hasActiveItemRecursivelyMemo) {
275
+ updateMarkerPosition();
276
+ }
277
+ setMarkerIsMoving(false);
278
+ };
278
279
  function readjustMarkerPosition(localOpen) {
279
- if (hasActiveItemRecursively()) {
280
+ if (hasActiveItemRecursivelyMemo) {
280
281
  if (localOpen) {
281
282
  reTriggerSetActiveGroupParent();
282
283
  }
@@ -285,25 +286,27 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
285
286
  }
286
287
  }
287
288
  else {
288
- updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
289
+ updateMarkerPosition();
289
290
  }
290
291
  }
291
292
  React__namespace.useImperativeHandle(ref, function () { return ({
292
293
  reTriggerSetActiveGroup: reTriggerSetActiveGroupParent,
293
294
  }); });
294
295
  var onUpdateMarkerPosition = React__namespace.useCallback(function () {
295
- if (hasActiveItemRecursively()) {
296
+ if (hasActiveItemRecursivelyMemo) {
296
297
  reTriggerSetActiveGroupParent();
297
298
  }
298
299
  else {
299
300
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
300
301
  }
301
- }, [hasActiveItemRecursively, updateMarkerPosition]);
302
+ }, [hasActiveItemRecursivelyMemo, updateMarkerPosition]);
302
303
  return (React__namespace.createElement(React__namespace.Fragment, null,
303
304
  React__namespace.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {},
304
305
  _b[classes.topButton] = depth === 0,
305
306
  _b[classes.leafButton] = depth,
306
- _b['text-indigo-500'] = activePath === group.href,
307
+ _b['text-indigo-500 dark:text-indigo-400'] = activePath === group.href,
308
+ _b['text-gray-900 dark:text-gray-100'] = hasActiveItemRecursivelyMemo && activePath !== group.href,
309
+ _b['text-gray-800 dark:text-gray-200'] = !hasActiveItemRecursivelyMemo && activePath !== group.href,
307
310
  _b)) },
308
311
  collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx(classes.expandedIcon, {
309
312
  'rotate-0': open,
@@ -311,26 +314,20 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
311
314
  'ml-[28px]': depth,
312
315
  }) })) : null,
313
316
  group.label),
314
- React__namespace.createElement("div", { className: clsx('grid transition-all relative', {
315
- 'grid-rows-[0fr]': !open && collapsible,
316
- 'grid-rows-[1fr]': open || !collapsible,
317
- }), ref: $listWrapper },
317
+ React__namespace.createElement("div", { className: clsx('relative', collapsible
318
+ ? {
319
+ 'transition-all grid': true,
320
+ 'grid-rows-[0fr]': !open,
321
+ 'grid-rows-[1fr]': open,
322
+ }
323
+ : null), onTransitionEnd: restoreActiveMarkerAfterTransition },
318
324
  open && collapsible && depth === 0 ? (React__namespace.createElement("div", { className: classes.openListBorderLeft })) : null,
319
- React__namespace.createElement(DocGroupElements, { ref: $groupElements, className: "overflow-hidden", items: group.items, activePath: activePath, collapsible: collapsible, depth: depth, onActivePosition: setActivePosition, updateMarkerPosition: onUpdateMarkerPosition, LinkComponent: LinkComponent, hideMarker: hideMarker }))));
325
+ React__namespace.createElement(DocGroupElements, { ref: $groupElements, className: "overflow-hidden", items: group.items, depth: depth, context: context, markerIsMoving: markerIsMoving, onActivePosition: setActivePosition, updateMarkerPosition: onUpdateMarkerPosition, LinkComponent: LinkComponent }))));
320
326
  });
321
327
  var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
322
- 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"]);
328
+ var items = _a.items, _b = _a.depth, depth = _b === void 0 ? 0 : _b, onActivePosition = _a.onActivePosition, updateMarkerPosition = _a.updateMarkerPosition, markerIsMoving = _a.markerIsMoving, LinkComponent = _a.LinkComponent, className = _a.className, context = _a.context, rest = __rest$1(_a, ["items", "depth", "onActivePosition", "updateMarkerPosition", "markerIsMoving", "LinkComponent", "className", "context"]);
323
329
  var $groups = React__namespace.useRef([]);
324
330
  var $items = React__namespace.useRef([]);
325
- var hasActiveItemRecursively = React__namespace.useCallback(function (localItems) {
326
- if (localItems === void 0) { localItems = items; }
327
- return localItems.some(function (item) {
328
- if ('items' in item) {
329
- return hasActiveItemRecursively(item.items);
330
- }
331
- return item.href === activePath;
332
- });
333
- }, [items, activePath]);
334
331
  var reTriggerSetActiveGroup = React__namespace.useCallback(function (index) {
335
332
  if (index === void 0) { index = 0; }
336
333
  $items.current.forEach(function (item) {
@@ -351,21 +348,22 @@ var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
351
348
  React__namespace.useImperativeHandle(ref, function () { return ({
352
349
  reTriggerSetActiveGroup: reTriggerSetActiveGroup,
353
350
  }); });
351
+ var activePath = context.activePath, hideMarker = context.hideMarker;
354
352
  var onUpdateMarkerPosition = React__namespace.useCallback(function (index) {
355
- if (hasActiveItemRecursively()) {
353
+ if (hasActiveItemRecursively(items, activePath)) {
356
354
  reTriggerSetActiveGroup(index);
357
355
  }
358
356
  else {
359
357
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
360
358
  }
361
- }, [hasActiveItemRecursively, updateMarkerPosition, reTriggerSetActiveGroup]);
359
+ }, [updateMarkerPosition, reTriggerSetActiveGroup, items, activePath]);
362
360
  return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', className) }), items.map(function (item, index) {
363
361
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
364
362
  React__namespace.createElement(DocGroup, { ref: function (el) {
365
363
  $groups.current[index] = el;
366
- }, group: item, activePath: activePath, depth: depth + 1, collapsible: collapsible, LinkComponent: LinkComponent, hideMarker: hideMarker, onActivePosition: onActivePosition, updateMarkerPosition: function () { return onUpdateMarkerPosition(index); } }))) : (React__namespace.createElement(DocLink, { ref: function (el) {
364
+ }, group: item, depth: depth + 1, LinkComponent: LinkComponent, markerIsMoving: markerIsMoving, context: context, onActivePosition: onActivePosition, updateMarkerPosition: function () { return onUpdateMarkerPosition(index); } }))) : (React__namespace.createElement(DocLink, { ref: function (el) {
367
365
  $items.current[index] = el;
368
- }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: function (opts) {
366
+ }, key: index, item: item, depth: depth, markerIsMoving: markerIsMoving, context: context, onActive: function (opts) {
369
367
  return depth < 0 ? hideMarker() : onActivePosition(opts);
370
368
  }, LinkComponent: LinkComponent }));
371
369
  })));
@@ -386,7 +384,7 @@ var DocMenu = function (_a) {
386
384
  setActiveTop(opts.top - containerTop);
387
385
  setActiveHeight(opts.height);
388
386
  }, []);
389
- var setShowMarkerFalse = React__namespace.useCallback(function () { return setShowMarker(false); }, []);
387
+ var hideMarker = React__namespace.useCallback(function () { return setShowMarker(false); }, []);
390
388
  var setMarkerIsMoving = React__namespace.useCallback(function (val) {
391
389
  localSetMarkerIsMoving(val);
392
390
  if (!val)
@@ -398,19 +396,23 @@ var DocMenu = function (_a) {
398
396
  setHasCSSTransition(true);
399
397
  }, 300);
400
398
  }, [localSetMarkerIsMoving]);
401
- return (React__namespace.createElement(MarkerIsMovingContext.Provider, { value: { markerIsMoving: markerIsMoving, setMarkerIsMoving: setMarkerIsMoving } },
402
- React__namespace.createElement("div", { ref: container, className: "relative" },
403
- showMarker && !markerIsMoving && collapsible ? (React__namespace.createElement("div", { className: "absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 ml-[6.5px] mt-[4px]", style: {
404
- top: "".concat(activeTop, "px"),
405
- height: "".concat(activeHeight - 8, "px"),
406
- transition: hasCSSTransition
407
- ? 'height .3s ease-in-out, top .3s ease-in-out'
408
- : 'none',
409
- } })) : null,
410
- React__namespace.createElement(DocGroupElements, __assign$1({}, rest, { items: items, activePath: activePath, collapsible: collapsible, depth: -1, onActivePosition: setActivePosition, LinkComponent: LinkComponent, hideMarker: setShowMarkerFalse })))));
399
+ return (React__namespace.createElement("div", { ref: container, className: "relative" },
400
+ showMarker && !markerIsMoving && collapsible ? (React__namespace.createElement("div", { "data-cy": "doc-menu-active-marker", className: "absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 dark:bg-indigo-400 ml-[6.5px] mt-[4px]", style: {
401
+ top: "".concat(activeTop, "px"),
402
+ height: "".concat(activeHeight - 8, "px"),
403
+ transition: hasCSSTransition
404
+ ? 'height .3s ease-in-out, top .3s ease-in-out'
405
+ : 'none',
406
+ } })) : null,
407
+ React__namespace.createElement(DocGroupElements, __assign$1({}, rest, { items: items, depth: -1, markerIsMoving: markerIsMoving, onActivePosition: setActivePosition, LinkComponent: LinkComponent, context: {
408
+ setMarkerIsMoving: setMarkerIsMoving,
409
+ activePath: activePath,
410
+ collapsible: collapsible,
411
+ hideMarker: hideMarker,
412
+ } }))));
411
413
  };
412
414
 
413
415
  module.exports = DocMenu;
414
416
  //# sourceMappingURL=index.umd.js.map
415
417
 
416
- /* <wind-keep class="container relative absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 ml-[6.5px] mt-[4px] transition ease-in-out text-indigo-500 rotate-0 -rotate-90 ml-[28px] grid transition-all grid-rows-[0fr] grid-rows-[1fr] overflow-hidden filter list-none p-0 block w-full pl-[24px] text-gray-700 dark:text-gray-500 py-[8px] text-[16px] leading-[24px] leading-[20px] text-[14px] py-[12px] left-[6.5px] top-[4px] bottom-[4px] z-10 hidden group-hover:block bg-gray-300 text-gray-500 left-0 transform transition-transform text-gray-400 dark:text-gray-700 flex py-[10px] items-center text-start font-medium pl-[48px] left-[8px] top-0 w-[1px] h-full bg-gray-100 dark:bg-gray-900"> */
418
+ /* <wind-keep class="container relative absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 dark:bg-indigo-400 ml-[6.5px] mt-[4px] transition ease-in-out text-indigo-500 dark:text-indigo-400 text-gray-900 dark:text-gray-100 text-gray-800 dark:text-gray-200 rotate-0 -rotate-90 ml-[28px] transition-all grid grid-rows-[0fr] grid-rows-[1fr] overflow-hidden filter list-none p-0 block w-full pl-[24px] text-gray-700 dark:text-gray-500 py-[8px] text-[16px] leading-[24px] leading-[20px] text-[14px] py-[12px] left-[6.5px] top-[4px] bottom-[4px] z-10 hidden group-hover:block bg-gray-300 text-indigo-400 text-gray-500 bg-indigo-400 left-0 transform transition-transform text-gray-400 dark:text-gray-700 flex py-[10px] items-center text-start font-medium pl-[48px] left-[8px] top-0 w-[1px] h-full bg-gray-100 dark:bg-gray-900"> */