@cypress-design/react-docmenu 0.4.3 → 0.5.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
@@ -175,18 +175,23 @@ var IconChevronDownSmall = function (props) { return React__namespace.createElem
175
175
  /* <wind-keep class="filter icon-light-secondary icon-dark-secondary"> */
176
176
 
177
177
  const classes = {
178
+ expandedIcon: 'absolute left-0 transform transition-transform text-gray-400 dark:text-gray-700',
178
179
  button: 'flex leading-[24px] py-[10px] items-center relative w-full text-start',
179
180
  topButton: 'leading-[24px] text-[16px] pl-[24px] font-medium',
180
- leafButton: 'leading-[20px] text-[14px] pl-[40px]',
181
+ leafButton: 'leading-[20px] text-[14px] pl-[48px]',
182
+ openListBorderLeft: 'absolute left-[8px] top-0 w-[1px] h-full bg-gray-100 dark:bg-gray-900',
181
183
  };
182
184
 
185
+ var MarkerIsMovingContext = React.createContext({
186
+ setMarkerIsMoving: function () { },
187
+ markerIsMoving: false,
188
+ });
189
+
183
190
  var DocLink = React__namespace.forwardRef(function (_a, ref) {
184
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;
185
192
  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() {
193
+ var _d = React__namespace.useContext(MarkerIsMovingContext), markerIsMoving = _d.markerIsMoving, setMarkerIsMoving = _d.setMarkerIsMoving;
194
+ var setActiveMarkerPosition = function () {
190
195
  var _a;
191
196
  if (active) {
192
197
  var box = (_a = activeLIRef === null || activeLIRef === void 0 ? void 0 : activeLIRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
@@ -195,31 +200,51 @@ var DocLink = React__namespace.forwardRef(function (_a, ref) {
195
200
  height: (box === null || box === void 0 ? void 0 : box.height) || 0,
196
201
  });
197
202
  }
198
- }
203
+ };
204
+ // on mount, if the item is active,
205
+ // send the top position to the parent
206
+ React__namespace.useEffect(setActiveMarkerPosition, [
207
+ onActive,
208
+ active,
209
+ setMarkerIsMoving,
210
+ ]);
199
211
  React__namespace.useImperativeHandle(ref, function () { return ({
200
212
  setActiveMarkerPosition: setActiveMarkerPosition,
201
213
  }); });
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]', {
214
+ return (React__namespace.createElement("li", { ref: activeLIRef, className: "list-none p-0" },
215
+ React__namespace.createElement(LinkComponent, { className: clsx('group relative block w-full pl-[24px]', {
206
216
  'text-indigo-500': active,
217
+ 'text-gray-700 dark:text-gray-500': !active,
207
218
  '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', {
219
+ 'leading-[20px] text-[14px] py-[12px]': depth >= 0,
220
+ }), href: item.href, style: {
221
+ paddingLeft: depth >= 0 ? "".concat(depth * 12 + 48, "px") : undefined,
222
+ } },
223
+ depth >= 0 ? (React__namespace.createElement("div", { className: clsx('left-[6.5px] absolute top-[4px] bottom-[4px] w-[4px] z-10 rounded-full', {
224
+ hidden: !markerIsMoving || !active,
211
225
  'group-hover:block bg-gray-300': !active && collapsible,
212
- }), style: {
213
- left: "-".concat(18.5 + depth * 8, "px"),
214
- } })) : null,
226
+ 'bg-indigo-500': active && markerIsMoving,
227
+ }) })) : null,
215
228
  item.label)));
216
229
  });
217
230
 
231
+ var useIsMounted = function () {
232
+ var isMounted = React__namespace.useRef(false);
233
+ React__namespace.useEffect(function () {
234
+ isMounted.current = true;
235
+ return function () {
236
+ isMounted.current = false;
237
+ };
238
+ }, []);
239
+ return isMounted;
240
+ };
218
241
  var DocGroup = React__namespace.forwardRef(function (_a, ref) {
219
242
  var _b;
220
243
  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
244
  var _e = React__namespace.useState(group.collapsed !== true), open = _e[0], setOpen = _e[1];
222
245
  var $groupElements = React__namespace.useRef(null);
246
+ var $listWrapper = React__namespace.useRef(null);
247
+ var didMount = useIsMounted();
223
248
  function toggleMenu(open) {
224
249
  if (!collapsible)
225
250
  return;
@@ -241,29 +266,58 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
241
266
  onActivePosition({ top: top, height: height });
242
267
  }
243
268
  }, [onActivePosition, open]);
244
- function reTriggerSetActiveGroup() {
269
+ var setMarkerIsMoving = React__namespace.useContext(MarkerIsMovingContext).setMarkerIsMoving;
270
+ function reTriggerSetActiveGroupParent() {
245
271
  var _a;
246
272
  (_a = $groupElements.current) === null || _a === void 0 ? void 0 : _a.reTriggerSetActiveGroup();
247
273
  }
248
274
  React__namespace.useEffect(function () {
249
- if (hasActiveItemRecursively()) {
250
- if (open) {
251
- reTriggerSetActiveGroup();
275
+ var _a, _b;
276
+ if (didMount.current) {
277
+ (_a = $listWrapper.current) === null || _a === void 0 ? void 0 : _a.addEventListener('transitionstart', function () {
278
+ setMarkerIsMoving(true);
279
+ }, { once: true });
280
+ (_b = $listWrapper.current) === null || _b === void 0 ? void 0 : _b.addEventListener('transitionend', function () {
281
+ setMarkerIsMoving(false);
282
+ if (open || !hasActiveItemRecursively()) {
283
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
284
+ }
285
+ }, { once: true });
286
+ }
287
+ }, [
288
+ open,
289
+ setMarkerIsMoving,
290
+ updateMarkerPosition,
291
+ hasActiveItemRecursively,
292
+ didMount,
293
+ ]);
294
+ React__namespace.useEffect(function () {
295
+ if (didMount.current) {
296
+ if (hasActiveItemRecursively()) {
297
+ if (open) {
298
+ reTriggerSetActiveGroupParent();
299
+ }
300
+ else {
301
+ hideMarker();
302
+ }
252
303
  }
253
304
  else {
254
- hideMarker();
305
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
255
306
  }
256
307
  }
257
- else {
258
- updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
259
- }
260
- }, [open, updateMarkerPosition, hideMarker, hasActiveItemRecursively]);
308
+ }, [
309
+ open,
310
+ updateMarkerPosition,
311
+ hideMarker,
312
+ hasActiveItemRecursively,
313
+ didMount,
314
+ ]);
261
315
  React__namespace.useImperativeHandle(ref, function () { return ({
262
- reTriggerSetActiveGroup: reTriggerSetActiveGroup,
316
+ reTriggerSetActiveGroup: reTriggerSetActiveGroupParent,
263
317
  }); });
264
318
  var onUpdateMarkerPosition = React__namespace.useCallback(function () {
265
319
  if (hasActiveItemRecursively()) {
266
- reTriggerSetActiveGroup();
320
+ reTriggerSetActiveGroupParent();
267
321
  }
268
322
  else {
269
323
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
@@ -275,13 +329,18 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
275
329
  _b[classes.leafButton] = depth,
276
330
  _b['text-indigo-500'] = activePath === group.href,
277
331
  _b)) },
278
- collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx('absolute transform transition-transform left-0', {
332
+ collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx(classes.expandedIcon, {
279
333
  'rotate-0': open,
280
334
  '-rotate-90': !open,
281
- 'ml-[16px]': depth,
335
+ 'ml-[28px]': depth,
282
336
  }) })) : null,
283
337
  group.label),
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 })));
338
+ React__namespace.createElement("div", { className: clsx('grid transition-all relative', {
339
+ 'grid-rows-[0fr]': !open && collapsible,
340
+ 'grid-rows-[1fr]': open || !collapsible,
341
+ }), ref: $listWrapper },
342
+ open && collapsible && depth === 0 ? (React__namespace.createElement("div", { className: classes.openListBorderLeft })) : null,
343
+ 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 }))));
285
344
  });
286
345
  var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
287
346
  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"]);
@@ -296,35 +355,42 @@ var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
296
355
  return item.href === activePath;
297
356
  });
298
357
  }, [items, activePath]);
299
- function reTriggerSetActiveGroup() {
300
- $groups.current.forEach(function (group) {
301
- group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
302
- });
358
+ var reTriggerSetActiveGroup = React__namespace.useCallback(function (index) {
359
+ if (index === void 0) { index = 0; }
303
360
  $items.current.forEach(function (item) {
304
361
  item === null || item === void 0 ? void 0 : item.setActiveMarkerPosition();
305
362
  });
306
- }
363
+ // calculate the index of the calling group in the list of groups
364
+ var indexGroup = index -
365
+ // remove all the items that are not groups from the array and count them
366
+ items.slice(0, index).filter(function (item) { return !('items' in item); }).length;
367
+ // only update groups that come after the toggled one
368
+ // others will not need to update the marker
369
+ // since they are "before" in the rendering tree
370
+ $groups.current.slice(indexGroup + 1).forEach(function (group) {
371
+ group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
372
+ });
373
+ }, [items]);
307
374
  React__namespace.useImperativeHandle(ref, function () { return ({
308
375
  reTriggerSetActiveGroup: reTriggerSetActiveGroup,
309
376
  }); });
310
- var onUpdateMarkerPosition = React__namespace.useCallback(function () {
377
+ var onUpdateMarkerPosition = React__namespace.useCallback(function (index) {
311
378
  if (hasActiveItemRecursively()) {
312
- reTriggerSetActiveGroup();
379
+ reTriggerSetActiveGroup(index);
313
380
  }
314
381
  else {
315
382
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
316
383
  }
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) {
384
+ }, [hasActiveItemRecursively, updateMarkerPosition, reTriggerSetActiveGroup]);
385
+ return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', className) }), items.map(function (item, index) {
322
386
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
323
387
  React__namespace.createElement(DocGroup, { ref: function (el) {
324
388
  $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) {
389
+ }, 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) {
326
390
  $items.current[index] = el;
327
- }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: onActivePosition, LinkComponent: LinkComponent }));
391
+ }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: function (opts) {
392
+ return depth < 0 ? hideMarker() : onActivePosition(opts);
393
+ }, LinkComponent: LinkComponent }));
328
394
  })));
329
395
  });
330
396
 
@@ -333,6 +399,7 @@ var DocMenu = function (_a) {
333
399
  var _e = React__namespace.useState(0), activeTop = _e[0], setActiveTop = _e[1];
334
400
  var _f = React__namespace.useState(36), activeHeight = _f[0], setActiveHeight = _f[1];
335
401
  var _g = React__namespace.useState(false), showMarker = _g[0], setShowMarker = _g[1];
402
+ var _h = React__namespace.useState(false), markerIsMoving = _h[0], setMarkerIsMoving = _h[1];
336
403
  var container = React__namespace.useRef(null);
337
404
  var setActivePosition = React__namespace.useCallback(function (opts) {
338
405
  var _a;
@@ -341,15 +408,17 @@ var DocMenu = function (_a) {
341
408
  setActiveTop(opts.top - containerTop);
342
409
  setActiveHeight(opts.height);
343
410
  }, []);
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, { items: items, activePath: activePath, collapsible: collapsible, depth: -1, onActivePosition: setActivePosition, LinkComponent: LinkComponent, hideMarker: function () { return setShowMarker(false); } }))));
411
+ var setShowMarkerFalse = React__namespace.useCallback(function () { return setShowMarker(false); }, []);
412
+ return (React__namespace.createElement(MarkerIsMovingContext.Provider, { value: { markerIsMoving: markerIsMoving, setMarkerIsMoving: setMarkerIsMoving } },
413
+ React__namespace.createElement("div", { ref: container, className: "relative" },
414
+ showMarker && !markerIsMoving && 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: {
415
+ top: "".concat(activeTop, "px"),
416
+ height: "".concat(activeHeight - 8, "px"),
417
+ } })) : null,
418
+ React__namespace.createElement(DocGroupElements, __assign$1({}, rest, { items: items, activePath: activePath, collapsible: collapsible, depth: -1, onActivePosition: setActivePosition, LinkComponent: LinkComponent, hideMarker: setShowMarkerFalse })))));
350
419
  };
351
420
 
352
421
  module.exports = DocMenu;
353
422
  //# sourceMappingURL=index.umd.js.map
354
423
 
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] text-indigo-500 transform transition-transform left-0 rotate-0 -rotate-90 ml-[16px] hidden 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]"> */
424
+ /* <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] text-indigo-500 rotate-0 -rotate-90 ml-[28px] 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-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"> */