@cypress-design/react-docmenu 0.4.3 → 0.5.1

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 markerIsMoving = React__namespace.useContext(MarkerIsMovingContext).markerIsMoving;
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,23 +200,27 @@ 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, [onActive, active]);
199
207
  React__namespace.useImperativeHandle(ref, function () { return ({
200
208
  setActiveMarkerPosition: setActiveMarkerPosition,
201
209
  }); });
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]', {
210
+ return (React__namespace.createElement("li", { ref: activeLIRef, className: "list-none p-0" },
211
+ React__namespace.createElement(LinkComponent, { className: clsx('group relative block w-full pl-[24px]', {
206
212
  'text-indigo-500': active,
213
+ 'text-gray-700 dark:text-gray-500': !active,
207
214
  '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', {
215
+ 'leading-[20px] text-[14px] py-[12px]': depth >= 0,
216
+ }), href: item.href, style: {
217
+ paddingLeft: depth >= 0 ? "".concat(depth * 12 + 48, "px") : undefined,
218
+ } },
219
+ depth >= 0 ? (React__namespace.createElement("div", { className: clsx('left-[6.5px] absolute top-[4px] bottom-[4px] w-[4px] z-10 rounded-full', {
220
+ hidden: !markerIsMoving || !active,
211
221
  'group-hover:block bg-gray-300': !active && collapsible,
212
- }), style: {
213
- left: "-".concat(18.5 + depth * 8, "px"),
214
- } })) : null,
222
+ 'bg-indigo-500': active && markerIsMoving,
223
+ }) })) : null,
215
224
  item.label)));
216
225
  });
217
226
 
@@ -220,10 +229,13 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
220
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;
221
230
  var _e = React__namespace.useState(group.collapsed !== true), open = _e[0], setOpen = _e[1];
222
231
  var $groupElements = React__namespace.useRef(null);
232
+ var $listWrapper = React__namespace.useRef(null);
223
233
  function toggleMenu(open) {
224
234
  if (!collapsible)
225
235
  return;
226
236
  setOpen(open);
237
+ hideShowAbsoluteMarker();
238
+ readjustMarkerPosition();
227
239
  }
228
240
  var hasActiveItemRecursively = React__namespace.useCallback(function (items) {
229
241
  if (items === void 0) { items = group.items; }
@@ -241,14 +253,32 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
241
253
  onActivePosition({ top: top, height: height });
242
254
  }
243
255
  }, [onActivePosition, open]);
244
- function reTriggerSetActiveGroup() {
256
+ var setMarkerIsMoving = React__namespace.useContext(MarkerIsMovingContext).setMarkerIsMoving;
257
+ function reTriggerSetActiveGroupParent() {
245
258
  var _a;
246
259
  (_a = $groupElements.current) === null || _a === void 0 ? void 0 : _a.reTriggerSetActiveGroup();
247
260
  }
248
- React__namespace.useEffect(function () {
261
+ /**
262
+ * To make the marker follow smooth open/close animation of the menu groups
263
+ * We replace the absolute marker by one in the link. After the transition, the
264
+ * marker is replaced by the absolute one again.
265
+ */
266
+ function hideShowAbsoluteMarker() {
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 (open || !hasActiveItemRecursively()) {
273
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
274
+ }
275
+ setMarkerIsMoving(false);
276
+ }, { once: true });
277
+ }
278
+ function readjustMarkerPosition() {
249
279
  if (hasActiveItemRecursively()) {
250
280
  if (open) {
251
- reTriggerSetActiveGroup();
281
+ reTriggerSetActiveGroupParent();
252
282
  }
253
283
  else {
254
284
  hideMarker();
@@ -257,13 +287,13 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
257
287
  else {
258
288
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
259
289
  }
260
- }, [open, updateMarkerPosition, hideMarker, hasActiveItemRecursively]);
290
+ }
261
291
  React__namespace.useImperativeHandle(ref, function () { return ({
262
- reTriggerSetActiveGroup: reTriggerSetActiveGroup,
292
+ reTriggerSetActiveGroup: reTriggerSetActiveGroupParent,
263
293
  }); });
264
294
  var onUpdateMarkerPosition = React__namespace.useCallback(function () {
265
295
  if (hasActiveItemRecursively()) {
266
- reTriggerSetActiveGroup();
296
+ reTriggerSetActiveGroupParent();
267
297
  }
268
298
  else {
269
299
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
@@ -275,13 +305,18 @@ var DocGroup = React__namespace.forwardRef(function (_a, ref) {
275
305
  _b[classes.leafButton] = depth,
276
306
  _b['text-indigo-500'] = activePath === group.href,
277
307
  _b)) },
278
- collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx('absolute transform transition-transform left-0', {
308
+ collapsible ? (React__namespace.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx(classes.expandedIcon, {
279
309
  'rotate-0': open,
280
310
  '-rotate-90': !open,
281
- 'ml-[16px]': depth,
311
+ 'ml-[28px]': depth,
282
312
  }) })) : null,
283
313
  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 })));
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 },
318
+ 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 }))));
285
320
  });
286
321
  var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
287
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"]);
@@ -296,35 +331,42 @@ var DocGroupElements = React__namespace.forwardRef(function (_a, ref) {
296
331
  return item.href === activePath;
297
332
  });
298
333
  }, [items, activePath]);
299
- function reTriggerSetActiveGroup() {
300
- $groups.current.forEach(function (group) {
301
- group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
302
- });
334
+ var reTriggerSetActiveGroup = React__namespace.useCallback(function (index) {
335
+ if (index === void 0) { index = 0; }
303
336
  $items.current.forEach(function (item) {
304
337
  item === null || item === void 0 ? void 0 : item.setActiveMarkerPosition();
305
338
  });
306
- }
339
+ // calculate the index of the calling group in the list of groups
340
+ var indexGroup = index -
341
+ // remove all the items that are not groups from the array and count them
342
+ items.slice(0, index).filter(function (item) { return !('items' in item); }).length;
343
+ // only update groups that come after the toggled one
344
+ // others will not need to update the marker
345
+ // since they are "before" in the rendering tree
346
+ $groups.current.slice(indexGroup + 1).forEach(function (group) {
347
+ group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
348
+ });
349
+ }, [items]);
307
350
  React__namespace.useImperativeHandle(ref, function () { return ({
308
351
  reTriggerSetActiveGroup: reTriggerSetActiveGroup,
309
352
  }); });
310
- var onUpdateMarkerPosition = React__namespace.useCallback(function () {
353
+ var onUpdateMarkerPosition = React__namespace.useCallback(function (index) {
311
354
  if (hasActiveItemRecursively()) {
312
- reTriggerSetActiveGroup();
355
+ reTriggerSetActiveGroup(index);
313
356
  }
314
357
  else {
315
358
  updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
316
359
  }
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) {
360
+ }, [hasActiveItemRecursively, updateMarkerPosition, reTriggerSetActiveGroup]);
361
+ return (React__namespace.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', className) }), items.map(function (item, index) {
322
362
  return 'items' in item ? (React__namespace.createElement("li", { key: index, className: "relative list-none p-0" },
323
363
  React__namespace.createElement(DocGroup, { ref: function (el) {
324
364
  $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) {
365
+ }, 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
366
  $items.current[index] = el;
327
- }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: onActivePosition, LinkComponent: LinkComponent }));
367
+ }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: function (opts) {
368
+ return depth < 0 ? hideMarker() : onActivePosition(opts);
369
+ }, LinkComponent: LinkComponent }));
328
370
  })));
329
371
  });
330
372
 
@@ -333,6 +375,7 @@ var DocMenu = function (_a) {
333
375
  var _e = React__namespace.useState(0), activeTop = _e[0], setActiveTop = _e[1];
334
376
  var _f = React__namespace.useState(36), activeHeight = _f[0], setActiveHeight = _f[1];
335
377
  var _g = React__namespace.useState(false), showMarker = _g[0], setShowMarker = _g[1];
378
+ var _h = React__namespace.useState(false), markerIsMoving = _h[0], setMarkerIsMoving = _h[1];
336
379
  var container = React__namespace.useRef(null);
337
380
  var setActivePosition = React__namespace.useCallback(function (opts) {
338
381
  var _a;
@@ -341,15 +384,17 @@ var DocMenu = function (_a) {
341
384
  setActiveTop(opts.top - containerTop);
342
385
  setActiveHeight(opts.height);
343
386
  }, []);
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); } }))));
387
+ var setShowMarkerFalse = React__namespace.useCallback(function () { return setShowMarker(false); }, []);
388
+ return (React__namespace.createElement(MarkerIsMovingContext.Provider, { value: { markerIsMoving: markerIsMoving, setMarkerIsMoving: setMarkerIsMoving } },
389
+ React__namespace.createElement("div", { ref: container, className: "relative" },
390
+ 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: {
391
+ top: "".concat(activeTop, "px"),
392
+ height: "".concat(activeHeight - 8, "px"),
393
+ } })) : null,
394
+ React__namespace.createElement(DocGroupElements, __assign$1({}, rest, { items: items, activePath: activePath, collapsible: collapsible, depth: -1, onActivePosition: setActivePosition, LinkComponent: LinkComponent, hideMarker: setShowMarkerFalse })))));
350
395
  };
351
396
 
352
397
  module.exports = DocMenu;
353
398
  //# sourceMappingURL=index.umd.js.map
354
399
 
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]"> */
400
+ /* <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] transition 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"> */