@cypress-design/react-docmenu 0.3.1 → 0.4.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.es.mjs CHANGED
@@ -43,50 +43,11 @@ function __rest$1(s, e) {
43
43
  return t;
44
44
  }
45
45
 
46
- function __spreadArray(to, from, pack) {
47
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
48
- if (ar || !(i in from)) {
49
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
50
- ar[i] = from[i];
51
- }
52
- }
53
- return to.concat(ar || Array.prototype.slice.call(from));
54
- }
55
-
56
46
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
57
47
  var e = new Error(message);
58
48
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
59
49
  };
60
50
 
61
- var DocLink = function (_a) {
62
- var item = _a.item, collapsible = _a.collapsible, _b = _a.depth, depth = _b === void 0 ? -1 : _b, onActive = _a.onActive;
63
- var activeLIRef = React.useRef(null);
64
- // on mount, if the item is active,
65
- // send the top position to the parent
66
- React.useEffect(function () {
67
- var _a;
68
- if (item.active) {
69
- 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);
70
- }
71
- // supposed to happen only on mount
72
- // eslint-disable-next-line react-hooks/exhaustive-deps
73
- }, []);
74
- return (React.createElement("li", { ref: activeLIRef, className: clsx('list-none p-0', {
75
- 'pl-[16px]': depth >= 0,
76
- }) },
77
- React.createElement("a", { className: clsx('group relative inline-block pl-[24px]', {
78
- 'text-indigo-500': item.active,
79
- 'py-[8px] text-[16px] leading-[24px]': depth < 0,
80
- 'py-[12px] leading-[20px] text-[14px]': depth >= 0,
81
- }), href: item.href },
82
- depth >= 0 ? (React.createElement("div", { className: clsx('absolute w-[4px] z-10 top-[10%] h-[80%] rounded-full hidden', {
83
- 'group-hover:block bg-gray-300': !item.active && collapsible,
84
- }), style: {
85
- left: "-".concat(18.5 + depth * 8, "px"),
86
- } })) : null,
87
- item.text)));
88
- };
89
-
90
51
  /******************************************************************************
91
52
  Copyright (c) Microsoft Corporation.
92
53
 
@@ -198,72 +159,176 @@ const classes = {
198
159
  leafButton: 'leading-[20px] text-[14px] pl-[40px]',
199
160
  };
200
161
 
201
- var DocGroup = function (_a) {
202
- var _b;
203
- var group = _a.group, collapsible = _a.collapsible, _c = _a.depth, depth = _c === void 0 ? 0 : _c, setHeight = _a.setHeight;
204
- var _d = React.useState(depth === 0), open = _d[0], setOpen = _d[1];
205
- var _e = React.useState(Array(group.items.length).fill(1)), itemsHeights = _e[0], setItemsHeights = _e[1];
206
- React.useEffect(function () {
207
- setHeight === null || setHeight === void 0 ? void 0 : setHeight(open ? itemsHeights.reduce(function (a, b) { return a + b; }, 1) : 1);
208
- }, [itemsHeights, open, setHeight]);
209
- var _f = React.useState(0), activeTop = _f[0], setActiveTop = _f[1];
210
- React.useEffect(function () {
211
- var activeItem = group.items.findIndex(function (item) { return 'href' in item && item.active; });
212
- if (activeItem >= 0) {
213
- setActiveTop(itemsHeights.slice(0, activeItem).reduce(function (a, b) { return a + b; }, 1) * 44);
162
+ var DocLink = React.forwardRef(function (_a, ref) {
163
+ 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;
164
+ var activeLIRef = React.useRef(null);
165
+ // on mount, if the item is active,
166
+ // send the top position to the parent
167
+ React.useEffect(setActiveMarkerPosition, [onActive, active]);
168
+ function setActiveMarkerPosition() {
169
+ var _a;
170
+ if (active) {
171
+ var box = (_a = activeLIRef === null || activeLIRef === void 0 ? void 0 : activeLIRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
172
+ onActive === null || onActive === void 0 ? void 0 : onActive({
173
+ top: (box === null || box === void 0 ? void 0 : box.top) || 0,
174
+ height: (box === null || box === void 0 ? void 0 : box.height) || 0,
175
+ });
214
176
  }
215
- }, [itemsHeights, group.items]);
177
+ }
178
+ React.useImperativeHandle(ref, function () { return ({
179
+ setActiveMarkerPosition: setActiveMarkerPosition,
180
+ }); });
181
+ return (React.createElement("li", { ref: activeLIRef, className: clsx('list-none p-0', {
182
+ 'pl-[16px]': depth >= 0,
183
+ }) },
184
+ React.createElement(LinkComponent, { className: clsx('group relative inline-block pl-[24px]', {
185
+ 'text-indigo-500': active,
186
+ 'py-[8px] text-[16px] leading-[24px]': depth < 0,
187
+ 'py-[12px] leading-[20px] text-[14px]': depth >= 0,
188
+ }), href: item.href },
189
+ depth >= 0 ? (React.createElement("div", { className: clsx('absolute w-[4px] z-10 top-[4px] bottom-[4px] rounded-full hidden', {
190
+ 'group-hover:block bg-gray-300': !active && collapsible,
191
+ }), style: {
192
+ left: "-".concat(18.5 + depth * 8, "px"),
193
+ } })) : null,
194
+ item.label)));
195
+ });
196
+
197
+ var DocGroup = React.forwardRef(function (_a, ref) {
198
+ var _b;
199
+ 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;
200
+ var _e = React.useState(group.collapsed !== true), open = _e[0], setOpen = _e[1];
201
+ var $groupElements = React.useRef(null);
216
202
  function toggleMenu(open) {
217
203
  if (!collapsible)
218
204
  return;
219
205
  setOpen(open);
220
206
  }
221
- function onSetHeight(index, height) {
222
- setItemsHeights(function (prev) {
223
- var newHeights = __spreadArray([], prev, true);
224
- newHeights[index] = height;
225
- return newHeights;
207
+ var hasActiveItemRecursively = React.useCallback(function (items) {
208
+ if (items === void 0) { items = group.items; }
209
+ return items.some(function (item) {
210
+ if ('items' in item) {
211
+ return hasActiveItemRecursively(item.items);
212
+ }
213
+ return item.href === activePath;
226
214
  });
227
- }
215
+ }, [group.items, activePath]);
228
216
  var Head = collapsible ? 'button' : group.href ? 'a' : 'div';
217
+ var setActivePosition = React.useCallback(function (_a) {
218
+ var top = _a.top, height = _a.height;
219
+ if (open) {
220
+ onActivePosition({ top: top, height: height });
221
+ }
222
+ }, [onActivePosition, open]);
223
+ function reTriggerSetActiveGroup() {
224
+ var _a;
225
+ (_a = $groupElements.current) === null || _a === void 0 ? void 0 : _a.reTriggerSetActiveGroup();
226
+ }
227
+ React.useEffect(function () {
228
+ if (hasActiveItemRecursively()) {
229
+ if (open) {
230
+ reTriggerSetActiveGroup();
231
+ }
232
+ else {
233
+ hideMarker();
234
+ }
235
+ }
236
+ else {
237
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
238
+ }
239
+ }, [open, updateMarkerPosition, hideMarker, hasActiveItemRecursively]);
240
+ React.useImperativeHandle(ref, function () { return ({
241
+ reTriggerSetActiveGroup: reTriggerSetActiveGroup,
242
+ }); });
243
+ var onUpdateMarkerPosition = React.useCallback(function () {
244
+ if (hasActiveItemRecursively()) {
245
+ reTriggerSetActiveGroup();
246
+ }
247
+ else {
248
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
249
+ }
250
+ }, [hasActiveItemRecursively, updateMarkerPosition]);
229
251
  return (React.createElement(React.Fragment, null,
230
- React.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {
231
- 'text-indigo-500': group.active
232
- },
252
+ React.createElement(Head, { onClick: function () { return toggleMenu(!open); }, href: group.href, className: clsx(classes.button, (_b = {},
233
253
  _b[classes.topButton] = depth === 0,
234
254
  _b[classes.leafButton] = depth,
255
+ _b['text-indigo-500'] = activePath === group.href,
235
256
  _b)) },
236
257
  collapsible ? (React.createElement(IconChevronDownSmall, { "stroke-color": "gray-400", size: depth ? '8' : '16', className: clsx('absolute transform transition-transform left-0', {
237
258
  'rotate-0': open,
238
259
  '-rotate-90': !open,
239
260
  'ml-[16px]': depth,
240
261
  }) })) : null,
241
- group.text),
242
- collapsible &&
243
- depth >= 0 &&
244
- open &&
245
- group.items.some(function (item) { return 'href' in item && item.active; }) ? (React.createElement("div", { className: "absolute h-[36px] w-[4px] z-10 rounded-full bg-indigo-500 transition-all duration-300 ml-[6px] mt-[4px]", style: {
246
- top: "".concat(activeTop, "px"),
247
- left: "".concat(depth === 0 ? 0.5 : -(depth * 8) - 0.5, "px"),
248
- } })) : null,
249
- React.createElement("ul", { className: clsx('ml-[8px] list-none p-0', {
250
- 'border-l border-gray-100': depth === 0 && collapsible,
251
- hidden: !open,
252
- }) }, group.items.map(function (item, index) {
253
- return 'items' in item ? (React.createElement("li", { key: index, className: "relative list-none p-0" },
254
- React.createElement(DocGroup, { group: item, depth: depth + 1, setHeight: function (height) { return onSetHeight(index, height); }, collapsible: collapsible }))) : (React.createElement(DocLink, { key: index, item: item, collapsible: collapsible, depth: depth, onActive: function (top) { return setActiveTop(top); } }));
255
- }))));
256
- };
257
-
258
- var DocMenu = function (_a) {
259
- var items = _a.items, _b = _a.collapsible, collapsible = _b === void 0 ? true : _b, rest = __rest$1(_a, ["items", "collapsible"]);
260
- return (React.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', rest.className) }), items.map(function (item, index) {
262
+ group.label),
263
+ React.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 })));
264
+ });
265
+ var DocGroupElements = React.forwardRef(function (_a, ref) {
266
+ 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"]);
267
+ var $groups = React.useRef([]);
268
+ var $items = React.useRef([]);
269
+ var hasActiveItemRecursively = React.useCallback(function (localItems) {
270
+ if (localItems === void 0) { localItems = items; }
271
+ return localItems.some(function (item) {
272
+ if ('items' in item) {
273
+ return hasActiveItemRecursively(item.items);
274
+ }
275
+ return item.href === activePath;
276
+ });
277
+ }, [items, activePath]);
278
+ function reTriggerSetActiveGroup() {
279
+ $groups.current.forEach(function (group) {
280
+ group === null || group === void 0 ? void 0 : group.reTriggerSetActiveGroup();
281
+ });
282
+ $items.current.forEach(function (item) {
283
+ item === null || item === void 0 ? void 0 : item.setActiveMarkerPosition();
284
+ });
285
+ }
286
+ React.useImperativeHandle(ref, function () { return ({
287
+ reTriggerSetActiveGroup: reTriggerSetActiveGroup,
288
+ }); });
289
+ var onUpdateMarkerPosition = React.useCallback(function () {
290
+ if (hasActiveItemRecursively()) {
291
+ reTriggerSetActiveGroup();
292
+ }
293
+ else {
294
+ updateMarkerPosition === null || updateMarkerPosition === void 0 ? void 0 : updateMarkerPosition();
295
+ }
296
+ }, [hasActiveItemRecursively, updateMarkerPosition]);
297
+ return (React.createElement("ul", __assign$1({}, rest, { className: clsx('list-none p-0', className, {
298
+ 'border-l border-gray-100': depth === 0 && collapsible,
299
+ 'ml-[8px]': depth >= 0,
300
+ }) }), items.map(function (item, index) {
261
301
  return 'items' in item ? (React.createElement("li", { key: index, className: "relative list-none p-0" },
262
- React.createElement(DocGroup, { group: item, collapsible: collapsible }))) : (React.createElement(DocLink, { key: index, item: item, collapsible: collapsible }));
302
+ React.createElement(DocGroup, { ref: function (el) {
303
+ $groups.current[index] = el;
304
+ }, group: item, activePath: activePath, depth: depth + 1, collapsible: collapsible, LinkComponent: LinkComponent, hideMarker: hideMarker, onActivePosition: onActivePosition, updateMarkerPosition: onUpdateMarkerPosition }))) : (React.createElement(DocLink, { ref: function (el) {
305
+ $items.current[index] = el;
306
+ }, key: index, item: item, active: item.href === activePath, collapsible: collapsible, depth: depth, onActive: onActivePosition, LinkComponent: LinkComponent }));
263
307
  })));
308
+ });
309
+
310
+ var DocMenu = function (_a) {
311
+ 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"]);
312
+ var _e = React.useState(0), activeTop = _e[0], setActiveTop = _e[1];
313
+ var _f = React.useState(36), activeHeight = _f[0], setActiveHeight = _f[1];
314
+ var _g = React.useState(false), showMarker = _g[0], setShowMarker = _g[1];
315
+ var container = React.useRef(null);
316
+ var setActivePosition = React.useCallback(function (opts) {
317
+ var _a;
318
+ var containerTop = ((_a = container.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().top) || 0;
319
+ setShowMarker(true);
320
+ setActiveTop(opts.top - containerTop);
321
+ setActiveHeight(opts.height);
322
+ }, []);
323
+ return (React.createElement("div", { ref: container, className: "relative" },
324
+ showMarker && collapsible ? (React.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: {
325
+ top: "".concat(activeTop + 4, "px"),
326
+ height: "".concat(activeHeight - 8, "px"),
327
+ } })) : null,
328
+ React.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); } }))));
264
329
  };
265
330
 
266
331
  export { DocMenu as default };
267
332
  //# sourceMappingURL=index.es.mjs.map
268
333
 
269
- /* <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]"> */
334
+ /* <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 font-medium pl-[40px]"> */