bough 0.1.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.mjs ADDED
@@ -0,0 +1,673 @@
1
+ import { jsx as k, Fragment as M, jsxs as ne } from "react/jsx-runtime";
2
+ import { useState as V, useRef as L, useCallback as p, useMemo as z, useEffect as ge, createContext as re, useContext as oe, forwardRef as Ie } from "react";
3
+ function pe({
4
+ defaultExpandedIds: n = [],
5
+ expandedIds: c,
6
+ onExpandedIdsChange: s,
7
+ selectedId: d,
8
+ onSelect: a,
9
+ onExpandChange: t
10
+ } = {}) {
11
+ const [e, f] = V(null), [r, l] = V(null), [h, v] = V(
12
+ () => new Set(n)
13
+ ), g = c !== void 0, T = g ? c : h, C = d ?? r, b = L(/* @__PURE__ */ new Map()), [_, S] = V(null), A = L([]), w = L(null), N = L(""), R = L(), P = p((o) => {
14
+ if (g) {
15
+ const u = o(T);
16
+ s == null || s(u);
17
+ } else
18
+ v(o);
19
+ }, [g, T, s]), O = p((o, u, i) => {
20
+ b.current.set(o, { node: u, parentId: i }), i === null && S((m) => m ?? o);
21
+ }, []), H = p((o) => {
22
+ b.current.delete(o);
23
+ }, []), W = p((o) => {
24
+ var u;
25
+ return ((u = b.current.get(o)) == null ? void 0 : u.parentId) ?? null;
26
+ }, []), Q = p((o) => {
27
+ var u;
28
+ return ((u = b.current.get(o)) == null ? void 0 : u.node) ?? null;
29
+ }, []), y = p((o) => T.has(o), [T]), U = p((o) => C === o, [C]), J = p((o) => e === o, [e]), B = p((o, u) => {
30
+ P((i) => {
31
+ const m = new Set(i), x = !m.has(o);
32
+ return x ? m.add(o) : m.delete(o), t == null || t(u, x), m;
33
+ });
34
+ }, [P, t]), X = p((o) => {
35
+ P((u) => {
36
+ if (u.has(o)) return u;
37
+ const i = new Set(u);
38
+ i.add(o);
39
+ const m = b.current.get(o);
40
+ return m && (t == null || t(m.node, !0)), i;
41
+ });
42
+ }, [P, t]), Y = p((o) => {
43
+ P((u) => {
44
+ if (!u.has(o)) return u;
45
+ const i = new Set(u);
46
+ i.delete(o);
47
+ const m = b.current.get(o);
48
+ return m && (t == null || t(m.node, !1)), i;
49
+ });
50
+ }, [P, t]), Z = p(() => {
51
+ P(() => {
52
+ const o = /* @__PURE__ */ new Set();
53
+ return b.current.forEach((u, i) => {
54
+ u.node.children && u.node.children.length > 0 && o.add(i);
55
+ }), o;
56
+ });
57
+ }, [P]), q = p(() => {
58
+ P(() => /* @__PURE__ */ new Set());
59
+ }, [P]), E = p((o, u) => {
60
+ l(o), a == null || a(u);
61
+ }, [a]), j = p(() => {
62
+ if (!w.current) return [];
63
+ const o = w.current.querySelectorAll('[role="treeitem"]'), u = [];
64
+ return o.forEach((i) => {
65
+ const m = i.getAttribute("data-node-id");
66
+ m && !i.closest('[role="group"][aria-hidden="true"]') && u.push(m);
67
+ }), A.current = u, u;
68
+ }, []), D = p((o) => {
69
+ var i;
70
+ f(o);
71
+ const u = (i = w.current) == null ? void 0 : i.querySelector(`[data-node-id="${o}"]`);
72
+ u instanceof HTMLElement && u.focus();
73
+ }, []), F = p((o, u) => {
74
+ var K, te;
75
+ const i = j();
76
+ if (i.length === 0) return;
77
+ let m = u ?? e;
78
+ !m && document.activeElement && (m = (te = (K = document.activeElement).getAttribute) == null ? void 0 : te.call(K, "data-node-id"));
79
+ let x;
80
+ const $ = m ? i.indexOf(m) : -1;
81
+ switch (o) {
82
+ case "next":
83
+ x = $ < i.length - 1 ? $ + 1 : $;
84
+ break;
85
+ case "prev":
86
+ x = $ > 0 ? $ - 1 : 0;
87
+ break;
88
+ case "first":
89
+ x = 0;
90
+ break;
91
+ case "last":
92
+ x = i.length - 1;
93
+ break;
94
+ }
95
+ const I = i[x];
96
+ I && D(I);
97
+ }, [e, j, D]), ee = p((o) => {
98
+ R.current && clearTimeout(R.current), N.current += o.toLowerCase(), R.current = setTimeout(() => {
99
+ N.current = "";
100
+ }, 500);
101
+ const u = j(), i = e ? u.indexOf(e) : -1;
102
+ for (let m = 0; m < u.length; m++) {
103
+ const x = (i + 1 + m) % u.length, $ = u[x], I = b.current.get($);
104
+ if (I != null && I.node.name.toLowerCase().startsWith(N.current)) {
105
+ D($);
106
+ break;
107
+ }
108
+ }
109
+ }, [e, j, D]), fe = p((o) => {
110
+ var m, x, $;
111
+ const { key: u } = o;
112
+ if (!((m = w.current) != null && m.contains(document.activeElement)))
113
+ return;
114
+ let i = e;
115
+ if (!i && document.activeElement) {
116
+ const I = ($ = (x = document.activeElement).getAttribute) == null ? void 0 : $.call(x, "data-node-id");
117
+ I && (i = I, f(I));
118
+ }
119
+ switch (u) {
120
+ case "ArrowDown":
121
+ o.preventDefault(), F("next", i);
122
+ break;
123
+ case "ArrowUp":
124
+ o.preventDefault(), F("prev", i);
125
+ break;
126
+ case "ArrowRight": {
127
+ if (o.preventDefault(), !i) break;
128
+ const I = b.current.get(i);
129
+ if (!I) break;
130
+ I.node.children && I.node.children.length > 0 && (y(i) ? F("next", i) : B(i, I.node));
131
+ break;
132
+ }
133
+ case "ArrowLeft": {
134
+ if (o.preventDefault(), !i) break;
135
+ const I = b.current.get(i);
136
+ if (!I) break;
137
+ I.node.children && I.node.children.length > 0 && y(i) ? B(i, I.node) : I.parentId && D(I.parentId);
138
+ break;
139
+ }
140
+ case "Home":
141
+ o.preventDefault(), F("first", i);
142
+ break;
143
+ case "End":
144
+ o.preventDefault(), F("last", i);
145
+ break;
146
+ case "Enter":
147
+ case " ": {
148
+ if (o.preventDefault(), !i) break;
149
+ const I = b.current.get(i);
150
+ I && E(i, I.node);
151
+ break;
152
+ }
153
+ default:
154
+ u.length === 1 && !o.ctrlKey && !o.altKey && !o.metaKey && ee(u);
155
+ break;
156
+ }
157
+ }, [e, y, B, F, D, E, ee]), he = z(() => ({
158
+ focusedId: e,
159
+ selectedId: C,
160
+ expandedIds: T,
161
+ firstNodeId: _,
162
+ setFocusedId: f,
163
+ selectNode: E,
164
+ toggleExpanded: B,
165
+ expand: X,
166
+ collapse: Y,
167
+ expandAll: Z,
168
+ collapseAll: q,
169
+ isExpanded: y,
170
+ isSelected: U,
171
+ isFocused: J,
172
+ registerNode: O,
173
+ unregisterNode: H,
174
+ getParentId: W,
175
+ getNode: Q
176
+ }), [
177
+ e,
178
+ C,
179
+ T,
180
+ _,
181
+ E,
182
+ B,
183
+ X,
184
+ Y,
185
+ Z,
186
+ q,
187
+ y,
188
+ U,
189
+ J,
190
+ O,
191
+ H,
192
+ W,
193
+ Q
194
+ ]), me = p((o = {}) => ({
195
+ role: "tree",
196
+ ...o
197
+ }), []);
198
+ return {
199
+ state: he,
200
+ getRootProps: me,
201
+ handleKeyDown: fe,
202
+ treeRef: w
203
+ };
204
+ }
205
+ function be({ node: n, level: c, posinset: s, setsize: d, parentId: a }, t) {
206
+ const e = n.id ?? `${a ?? "root"}-${c}-${s}-${n.name}`, f = `tree-group-${e}`, r = !!(n.children && n.children.length > 0), l = r && t.isExpanded(e), h = t.isSelected(e), v = t.isFocused(e), g = t.focusedId === null && a === null && s === 1, T = v || g ? 0 : -1;
207
+ ge(() => (t.registerNode(e, n, a), () => t.unregisterNode(e)), [e, n, a, t]);
208
+ const C = p(() => {
209
+ r && t.toggleExpanded(e, n);
210
+ }, [r, e, n, t]), b = p(() => {
211
+ t.selectNode(e, n);
212
+ }, [e, n, t]), _ = p((w = {}) => ({
213
+ role: "treeitem",
214
+ "aria-expanded": r ? l : void 0,
215
+ "aria-selected": h,
216
+ "aria-level": c,
217
+ "aria-setsize": d,
218
+ "aria-posinset": s,
219
+ "aria-owns": r ? f : void 0,
220
+ "data-node-id": e,
221
+ "data-expanded": r ? l : void 0,
222
+ "data-selected": h || void 0,
223
+ "data-focused": v || void 0,
224
+ tabIndex: T,
225
+ onFocus: (N) => {
226
+ var R;
227
+ N.stopPropagation(), t.setFocusedId(e), (R = w.onFocus) == null || R.call(w, N);
228
+ },
229
+ ...w
230
+ }), [r, l, h, v, c, d, s, f, e, T, t]), S = p((w = {}) => ({
231
+ onClick: (N) => {
232
+ var R;
233
+ N.stopPropagation(), r && C(), b(), (R = w.onClick) == null || R.call(w, N);
234
+ },
235
+ ...w
236
+ }), [r, C, b]), A = p((w = {}) => ({
237
+ id: f,
238
+ role: "group",
239
+ "aria-hidden": !l,
240
+ "data-state": l ? "open" : "closed",
241
+ ...w
242
+ }), [f, l]);
243
+ return z(() => ({
244
+ nodeId: e,
245
+ isExpanded: l,
246
+ isSelected: h,
247
+ isFocused: v,
248
+ isFirstFocusable: g,
249
+ hasChildren: r,
250
+ tabIndex: T,
251
+ getItemProps: _,
252
+ getContentProps: S,
253
+ getGroupProps: A,
254
+ toggle: C,
255
+ select: b
256
+ }), [
257
+ e,
258
+ l,
259
+ h,
260
+ v,
261
+ g,
262
+ r,
263
+ T,
264
+ _,
265
+ S,
266
+ A,
267
+ C,
268
+ b
269
+ ]);
270
+ }
271
+ const se = re(null);
272
+ function we() {
273
+ const n = oe(se);
274
+ if (!n)
275
+ throw new Error("useTreeContext must be used within a Tree.Root component");
276
+ return n;
277
+ }
278
+ const ce = re(null);
279
+ function G() {
280
+ const n = oe(ce);
281
+ if (!n)
282
+ throw new Error("useTreeItemContext must be used within a Tree.Item component");
283
+ return n;
284
+ }
285
+ function ke({
286
+ children: n,
287
+ className: c,
288
+ style: s,
289
+ "aria-label": d,
290
+ "aria-labelledby": a,
291
+ ...t
292
+ }) {
293
+ const { state: e, getRootProps: f, handleKeyDown: r, treeRef: l } = pe(t), h = z(() => ({ state: e, treeRef: l }), [e, l]);
294
+ return /* @__PURE__ */ k(se.Provider, { value: h, children: /* @__PURE__ */ k(
295
+ "div",
296
+ {
297
+ ref: l,
298
+ ...f(),
299
+ "aria-label": d,
300
+ "aria-labelledby": a,
301
+ className: c,
302
+ style: s,
303
+ onKeyDown: r,
304
+ children: n
305
+ }
306
+ ) });
307
+ }
308
+ function ie({
309
+ node: n,
310
+ level: c,
311
+ posinset: s,
312
+ setsize: d,
313
+ parentId: a,
314
+ children: t,
315
+ className: e,
316
+ style: f
317
+ }) {
318
+ const { state: r } = we(), l = be({ node: n, level: c, posinset: s, setsize: d, parentId: a }, r), h = typeof t == "function" ? t(l) : t;
319
+ return /* @__PURE__ */ k(ce.Provider, { value: l, children: /* @__PURE__ */ k(
320
+ "div",
321
+ {
322
+ ...l.getItemProps(),
323
+ className: e,
324
+ style: f,
325
+ children: h
326
+ }
327
+ ) });
328
+ }
329
+ const le = Ie(
330
+ function({ children: c, className: s, style: d, onClick: a, ...t }, e) {
331
+ const r = G().getContentProps(), l = a ?? r.onClick;
332
+ return /* @__PURE__ */ k(
333
+ "div",
334
+ {
335
+ ref: e,
336
+ ...t,
337
+ className: s,
338
+ style: d,
339
+ onClick: l,
340
+ children: c
341
+ }
342
+ );
343
+ }
344
+ );
345
+ function ve({ open: n }) {
346
+ return /* @__PURE__ */ k(
347
+ "svg",
348
+ {
349
+ viewBox: "0 0 24 24",
350
+ fill: "none",
351
+ stroke: "currentColor",
352
+ strokeWidth: "2",
353
+ strokeLinecap: "round",
354
+ strokeLinejoin: "round",
355
+ "aria-hidden": "true",
356
+ style: {
357
+ width: "0.75rem",
358
+ height: "0.75rem",
359
+ flexShrink: 0,
360
+ transition: "transform 200ms",
361
+ transform: n ? "rotate(180deg)" : "rotate(0deg)"
362
+ },
363
+ children: /* @__PURE__ */ k("polyline", { points: "18 15 12 9 6 15" })
364
+ }
365
+ );
366
+ }
367
+ function ue({
368
+ children: n,
369
+ className: c,
370
+ style: s,
371
+ openClassName: d,
372
+ closedClassName: a
373
+ }) {
374
+ const { isExpanded: t, hasChildren: e } = G();
375
+ if (!e)
376
+ return /* @__PURE__ */ k(
377
+ "span",
378
+ {
379
+ style: { width: "0.75rem", height: "0.75rem", flexShrink: 0, ...s },
380
+ className: c,
381
+ "aria-hidden": "true"
382
+ }
383
+ );
384
+ const r = [c, t ? d : a].filter(Boolean).join(" ") || void 0;
385
+ return n ? /* @__PURE__ */ k("span", { className: r, style: s, "aria-hidden": "true", children: n }) : /* @__PURE__ */ k("span", { className: r, style: s, children: /* @__PURE__ */ k(ve, { open: t }) });
386
+ }
387
+ function de({ children: n, className: c, style: s }) {
388
+ const d = G(), a = d.getGroupProps();
389
+ return d.hasChildren ? /* @__PURE__ */ k(
390
+ "div",
391
+ {
392
+ ...a,
393
+ className: c,
394
+ style: s,
395
+ children: n
396
+ }
397
+ ) : null;
398
+ }
399
+ function Te({
400
+ nodes: n,
401
+ level: c = 1,
402
+ parentId: s = null,
403
+ renderItem: d
404
+ }) {
405
+ return /* @__PURE__ */ k(M, { children: n.map(
406
+ (a, t) => d({
407
+ node: a,
408
+ level: c,
409
+ posinset: t + 1,
410
+ setsize: n.length,
411
+ parentId: s
412
+ })
413
+ ) });
414
+ }
415
+ function Ce({
416
+ nodes: n,
417
+ renderContent: c,
418
+ styled: s = !0,
419
+ rowClassName: d,
420
+ groupClassName: a,
421
+ chevronClassName: t
422
+ }) {
423
+ return /* @__PURE__ */ k(M, { children: n.map((e, f) => /* @__PURE__ */ k(
424
+ ae,
425
+ {
426
+ node: e,
427
+ level: 1,
428
+ posinset: f + 1,
429
+ setsize: n.length,
430
+ parentId: null,
431
+ NodeContent: c,
432
+ styled: s,
433
+ rowClassName: d,
434
+ groupClassName: a,
435
+ chevronClassName: t
436
+ },
437
+ e.id ?? `${e.name}-${f}`
438
+ )) });
439
+ }
440
+ function ae({
441
+ node: n,
442
+ level: c,
443
+ posinset: s,
444
+ setsize: d,
445
+ parentId: a,
446
+ NodeContent: t,
447
+ styled: e,
448
+ rowClassName: f,
449
+ groupClassName: r,
450
+ chevronClassName: l
451
+ }) {
452
+ const h = !!(n.children && n.children.length > 0);
453
+ return /* @__PURE__ */ k(
454
+ ie,
455
+ {
456
+ node: n,
457
+ level: c,
458
+ posinset: s,
459
+ setsize: d,
460
+ parentId: a,
461
+ children: ({ isExpanded: v, isSelected: g, isFocused: T, nodeId: C }) => {
462
+ const b = e ? [
463
+ "tree__row",
464
+ g && "tree__row--selected",
465
+ T && "tree__row--focused",
466
+ f
467
+ ].filter(Boolean).join(" ") : f, _ = e ? [
468
+ "tree__children",
469
+ v && "tree__children--open",
470
+ r
471
+ ].filter(Boolean).join(" ") : r, S = e ? ["tree__chevron", l].filter(Boolean).join(" ") : l;
472
+ return /* @__PURE__ */ ne(M, { children: [
473
+ /* @__PURE__ */ ne(le, { className: b, children: [
474
+ /* @__PURE__ */ k(
475
+ ue,
476
+ {
477
+ className: S,
478
+ openClassName: e ? "tree__chevron--open" : void 0
479
+ }
480
+ ),
481
+ /* @__PURE__ */ k(t, { ...n })
482
+ ] }),
483
+ h && /* @__PURE__ */ k(de, { className: _, children: /* @__PURE__ */ k("div", { className: e ? "tree__children-inner" : void 0, children: n.children.map((w, N) => /* @__PURE__ */ k(
484
+ ae,
485
+ {
486
+ node: w,
487
+ level: c + 1,
488
+ posinset: N + 1,
489
+ setsize: n.children.length,
490
+ parentId: C,
491
+ NodeContent: t,
492
+ styled: e,
493
+ rowClassName: f,
494
+ groupClassName: r,
495
+ chevronClassName: l
496
+ },
497
+ w.id ?? `${w.name}-${N}`
498
+ )) }) })
499
+ ] });
500
+ }
501
+ }
502
+ );
503
+ }
504
+ const _e = {
505
+ Root: ke,
506
+ Item: ie,
507
+ ItemContent: le,
508
+ Chevron: ue,
509
+ Group: de,
510
+ Nodes: Te,
511
+ Recursive: Ce
512
+ };
513
+ function Re(n, c = {}) {
514
+ const {
515
+ startLevel: s = 1,
516
+ filter: d,
517
+ includeCollapsedChildren: a = !0,
518
+ expandedIds: t
519
+ } = c, e = [];
520
+ function f(r, l, h) {
521
+ const v = d ? r.filter(d) : r;
522
+ v.forEach((g, T) => {
523
+ const C = g.id ?? `${h ?? "root"}-${l}-${T + 1}-${g.name}`, b = !!(g.children && g.children.length > 0), _ = {
524
+ ...g,
525
+ nodeId: C,
526
+ level: l,
527
+ posinset: T + 1,
528
+ setsize: v.length,
529
+ parentId: h,
530
+ hasChildren: b
531
+ };
532
+ e.push(_), b && g.children && (a || t && t.has(C)) && f(g.children, l + 1, C);
533
+ });
534
+ }
535
+ return f(n, s, null), e;
536
+ }
537
+ function Se(n, c) {
538
+ const { getId: s, getParentId: d, transform: a } = c, t = /* @__PURE__ */ new Map(), e = [];
539
+ n.forEach((r) => {
540
+ const l = s(r), h = {
541
+ ...a(r),
542
+ children: []
543
+ };
544
+ t.set(l, h);
545
+ }), n.forEach((r) => {
546
+ const l = s(r), h = d(r), v = t.get(l);
547
+ if (h === null)
548
+ e.push(v);
549
+ else {
550
+ const g = t.get(h);
551
+ g ? (g.children = g.children || [], g.children.push(v)) : e.push(v);
552
+ }
553
+ });
554
+ function f(r) {
555
+ r.forEach((l) => {
556
+ l.children && l.children.length === 0 ? delete l.children : l.children && f(l.children);
557
+ });
558
+ }
559
+ return f(e), e;
560
+ }
561
+ function xe(n, c, s = {}) {
562
+ const { keepAncestors: d = !0, keepDescendants: a = !1 } = s;
563
+ function t(e) {
564
+ const f = [];
565
+ return e.forEach((r) => {
566
+ const l = c(r);
567
+ let h = [];
568
+ r.children && r.children.length > 0 && (l && a ? h = r.children : h = t(r.children)), l ? f.push({
569
+ ...r,
570
+ children: h.length > 0 ? h : void 0
571
+ }) : d && h.length > 0 && f.push({
572
+ ...r,
573
+ children: h
574
+ });
575
+ }), f;
576
+ }
577
+ return t(n);
578
+ }
579
+ function De(n, c, s = {}) {
580
+ const { ignoreCase: d = !0, searchFields: a = [], minLength: t = 1 } = s;
581
+ if (!c || c.length < t)
582
+ return n;
583
+ const e = d ? c.toLowerCase() : c;
584
+ return xe(n, (r) => {
585
+ if ((d ? r.name.toLowerCase() : r.name).includes(e))
586
+ return !0;
587
+ if (r.data && a.length > 0) {
588
+ const h = r.data;
589
+ return a.some((v) => {
590
+ const g = h[v];
591
+ return typeof g == "string" ? (d ? g.toLowerCase() : g).includes(e) : !1;
592
+ });
593
+ }
594
+ return !1;
595
+ }, { keepAncestors: !0, keepDescendants: !1 });
596
+ }
597
+ function Ne(n, c) {
598
+ for (const s of n) {
599
+ if (s.id === c)
600
+ return s;
601
+ if (s.children) {
602
+ const d = Ne(s.children, c);
603
+ if (d)
604
+ return d;
605
+ }
606
+ }
607
+ return null;
608
+ }
609
+ function Fe(n, c) {
610
+ function s(d, a) {
611
+ for (const t of d) {
612
+ const e = t.id ?? t.name, f = [...a, e];
613
+ if (e === c)
614
+ return f;
615
+ if (t.children) {
616
+ const r = s(t.children, f);
617
+ if (r)
618
+ return r;
619
+ }
620
+ }
621
+ return null;
622
+ }
623
+ return s(n, []) ?? [];
624
+ }
625
+ function Ae(n) {
626
+ const c = [];
627
+ function s(d, a, t) {
628
+ d.forEach((e, f) => {
629
+ const r = e.id ?? `${a ?? "root"}-${t}-${f + 1}-${e.name}`;
630
+ c.push(r), e.children && s(e.children, r, t + 1);
631
+ });
632
+ }
633
+ return s(n, null, 1), c;
634
+ }
635
+ function ye(n) {
636
+ const c = [];
637
+ function s(d, a, t) {
638
+ d.forEach((e, f) => {
639
+ const r = e.id ?? `${a ?? "root"}-${t}-${f + 1}-${e.name}`;
640
+ e.children && e.children.length > 0 && (c.push(r), s(e.children, r, t + 1));
641
+ });
642
+ }
643
+ return s(n, null, 1), c;
644
+ }
645
+ function Be(...n) {
646
+ return n.filter(Boolean).join(" ");
647
+ }
648
+ export {
649
+ _e as Tree,
650
+ ue as TreeChevron,
651
+ se as TreeContext,
652
+ de as TreeGroup,
653
+ ie as TreeItem,
654
+ le as TreeItemContent,
655
+ ce as TreeItemContext,
656
+ Te as TreeNodes,
657
+ Ce as TreeRecursive,
658
+ ke as TreeRoot,
659
+ Se as buildTree,
660
+ Be as cn,
661
+ xe as filterTree,
662
+ Ne as findNodeById,
663
+ Re as flattenTree,
664
+ Ae as getAllNodeIds,
665
+ ye as getExpandableNodeIds,
666
+ Fe as getNodePath,
667
+ De as searchTree,
668
+ pe as useTree,
669
+ we as useTreeContext,
670
+ be as useTreeItem,
671
+ G as useTreeItemContext
672
+ };
673
+ //# sourceMappingURL=index.mjs.map