react-aria-menubutton 7.0.3 → 8.0.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.
Files changed (43) hide show
  1. package/README.md +140 -122
  2. package/dist/index.d.ts +59 -0
  3. package/dist/react-aria-menubutton.cjs.js +1 -0
  4. package/dist/react-aria-menubutton.es.js +566 -0
  5. package/dist/react-aria-menubutton.umd.js +1 -0
  6. package/package.json +62 -57
  7. package/CHANGELOG.md +0 -176
  8. package/CODE_OF_CONDUCT.md +0 -22
  9. package/dist/Button.js +0 -140
  10. package/dist/ManagerContext.js +0 -7
  11. package/dist/Menu.js +0 -131
  12. package/dist/MenuItem.js +0 -96
  13. package/dist/Wrapper.js +0 -74
  14. package/dist/createManager.js +0 -163
  15. package/dist/externalStateControl.js +0 -32
  16. package/dist/index.js +0 -12
  17. package/dist/propTypes.js +0 -7
  18. package/dist/specialAssign.js +0 -11
  19. package/src/Button.js +0 -129
  20. package/src/ManagerContext.js +0 -5
  21. package/src/Menu.js +0 -118
  22. package/src/MenuItem.js +0 -84
  23. package/src/Wrapper.js +0 -62
  24. package/src/__tests__/Button.test.js +0 -169
  25. package/src/__tests__/Menu.test.js +0 -130
  26. package/src/__tests__/MenuItem.test.js +0 -106
  27. package/src/__tests__/__snapshots__/Button.test.js.snap +0 -41
  28. package/src/__tests__/__snapshots__/Menu.test.js.snap +0 -54
  29. package/src/__tests__/__snapshots__/MenuItem.test.js.snap +0 -37
  30. package/src/__tests__/createManager.test.js +0 -190
  31. package/src/__tests__/helpers/MockWrapper.js +0 -24
  32. package/src/__tests__/helpers/createMockKeyEvent.js +0 -7
  33. package/src/__tests__/helpers/createMockManager.js +0 -22
  34. package/src/__tests__/helpers/jest-setup.js +0 -5
  35. package/src/__tests__/helpers/raf.js +0 -3
  36. package/src/createManager.js +0 -173
  37. package/src/externalStateControl.js +0 -31
  38. package/src/index.js +0 -10
  39. package/src/propTypes.js +0 -8
  40. package/src/specialAssign.js +0 -9
  41. package/umd/ReactAriaMenuButton.js +0 -1
  42. package/webpack-demo.config.js +0 -14
  43. package/webpack-umd.config.js +0 -35
@@ -0,0 +1,566 @@
1
+ import { jsx as g } from "react/jsx-runtime";
2
+ import { createContext as D, forwardRef as O, useMemo as A, useEffect as y, createElement as w, useContext as x, useRef as v, useState as L, useCallback as p } from "react";
3
+ const S = /* @__PURE__ */ new Map(), M = "a menu outside a mounted Wrapper with an id, or a menu that does not exist";
4
+ function G(e, r) {
5
+ S.set(e, r);
6
+ }
7
+ function R(e) {
8
+ S.delete(e);
9
+ }
10
+ function ue(e, r) {
11
+ const s = S.get(e);
12
+ if (!s) throw new Error("Cannot open " + M);
13
+ s.openMenu(r);
14
+ }
15
+ function ce(e, r) {
16
+ const s = S.get(e);
17
+ if (!s) throw new Error("Cannot close " + M);
18
+ s.closeMenu(r);
19
+ }
20
+ function F(e) {
21
+ return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
22
+ }
23
+ var b, I;
24
+ function C() {
25
+ if (I) return b;
26
+ I = 1;
27
+ function e(t) {
28
+ t = t || {};
29
+ var n = t.keybindings || {};
30
+ this._settings = {
31
+ keybindings: {
32
+ next: n.next || { keyCode: 40 },
33
+ prev: n.prev || { keyCode: 38 },
34
+ first: n.first,
35
+ last: n.last
36
+ },
37
+ wrap: t.wrap,
38
+ stringSearch: t.stringSearch,
39
+ stringSearchDelay: 800
40
+ }, this._keybindingsLookup = [];
41
+ var o, u;
42
+ for (o in this._settings.keybindings)
43
+ u = this._settings.keybindings[o], u && [].concat(u).forEach((function(f) {
44
+ f.metaKey = f.metaKey || !1, f.ctrlKey = f.ctrlKey || !1, f.altKey = f.altKey || !1, f.shiftKey = f.shiftKey || !1, this._keybindingsLookup.push({
45
+ action: o,
46
+ eventMatcher: f
47
+ });
48
+ }).bind(this));
49
+ this._searchString = "", this._members = [], t.members && this.setMembers(t.members), this._boundHandleKeydownEvent = this._handleKeydownEvent.bind(this);
50
+ }
51
+ e.prototype.activate = function() {
52
+ return document.addEventListener("keydown", this._boundHandleKeydownEvent, !0), this;
53
+ }, e.prototype.deactivate = function() {
54
+ return document.removeEventListener("keydown", this._boundHandleKeydownEvent, !0), this._clearSearchStringRefreshTimer(), this;
55
+ }, e.prototype._handleKeydownEvent = function(t) {
56
+ var n = this._getActiveElementIndex();
57
+ if (n !== -1) {
58
+ var o = !1;
59
+ this._keybindingsLookup.forEach((function(u) {
60
+ if (r(u.eventMatcher, t))
61
+ switch (o = !0, t.preventDefault(), u.action) {
62
+ case "next":
63
+ this.moveFocusForward();
64
+ break;
65
+ case "prev":
66
+ this.moveFocusBack();
67
+ break;
68
+ case "first":
69
+ this.moveFocusToFirst();
70
+ break;
71
+ case "last":
72
+ this.moveFocusToLast();
73
+ break;
74
+ default:
75
+ return;
76
+ }
77
+ }).bind(this)), o || this._handleUnboundKey(t);
78
+ }
79
+ }, e.prototype.moveFocusForward = function() {
80
+ var t = this._getActiveElementIndex(), n;
81
+ return t < this._members.length - 1 ? n = t + 1 : this._settings.wrap ? n = 0 : n = t, this.focusNodeAtIndex(n), n;
82
+ }, e.prototype.moveFocusBack = function() {
83
+ var t = this._getActiveElementIndex(), n;
84
+ return t > 0 ? n = t - 1 : this._settings.wrap ? n = this._members.length - 1 : n = t, this.focusNodeAtIndex(n), n;
85
+ }, e.prototype.moveFocusToFirst = function() {
86
+ this.focusNodeAtIndex(0);
87
+ }, e.prototype.moveFocusToLast = function() {
88
+ this.focusNodeAtIndex(this._members.length - 1);
89
+ }, e.prototype._handleUnboundKey = function(t) {
90
+ if (this._settings.stringSearch) {
91
+ if (this._searchString !== "" && (t.key === " " || t.keyCode === 32))
92
+ return t.preventDefault(), -1;
93
+ if (!s(t.keyCode) || t.ctrlKey || t.metaKey || t.altKey) return -1;
94
+ t.preventDefault(), this._addToSearchString(String.fromCharCode(t.keyCode)), this._runStringSearch();
95
+ }
96
+ }, e.prototype._clearSearchString = function() {
97
+ this._searchString = "";
98
+ }, e.prototype._addToSearchString = function(t) {
99
+ this._searchString += t.toLowerCase();
100
+ }, e.prototype._startSearchStringRefreshTimer = function() {
101
+ var t = this;
102
+ this._clearSearchStringRefreshTimer(), this._stringSearchTimer = setTimeout(function() {
103
+ t._clearSearchString();
104
+ }, this._settings.stringSearchDelay);
105
+ }, e.prototype._clearSearchStringRefreshTimer = function() {
106
+ clearTimeout(this._stringSearchTimer);
107
+ }, e.prototype._runStringSearch = function() {
108
+ this._startSearchStringRefreshTimer(), this.moveFocusByString(this._searchString);
109
+ }, e.prototype.moveFocusByString = function(t) {
110
+ for (var n, o = 0, u = this._members.length; o < u; o++)
111
+ if (n = this._members[o], !!n.text && n.text.indexOf(t) === 0)
112
+ return i(n.node);
113
+ }, e.prototype._findIndexOfNode = function(t) {
114
+ for (var n = 0, o = this._members.length; n < o; n++)
115
+ if (this._members[n].node === t)
116
+ return n;
117
+ return -1;
118
+ }, e.prototype._getActiveElementIndex = function() {
119
+ return this._findIndexOfNode(document.activeElement);
120
+ }, e.prototype.focusNodeAtIndex = function(t) {
121
+ var n = this._members[t];
122
+ return n && i(n.node), this;
123
+ }, e.prototype.addMember = function(t, n) {
124
+ var o = t.node || t, u = t.text || o.getAttribute("data-focus-group-text") || o.textContent || "";
125
+ this._checkNode(o);
126
+ var f = u.replace(/[\W_]/g, "").toLowerCase(), h = {
127
+ node: o,
128
+ text: f
129
+ };
130
+ return n != null ? this._members.splice(n, 0, h) : this._members.push(h), this;
131
+ }, e.prototype.removeMember = function(t) {
132
+ var n = typeof t == "number" ? t : this._findIndexOfNode(t);
133
+ if (n !== -1)
134
+ return this._members.splice(n, 1), this;
135
+ }, e.prototype.clearMembers = function() {
136
+ return this._members = [], this;
137
+ }, e.prototype.setMembers = function(t) {
138
+ this.clearMembers();
139
+ for (var n = 0, o = t.length; n < o; n++)
140
+ this.addMember(t[n]);
141
+ return this;
142
+ }, e.prototype.getMembers = function() {
143
+ return this._members;
144
+ }, e.prototype._checkNode = function(t) {
145
+ if (!t.nodeType || t.nodeType !== window.Node.ELEMENT_NODE)
146
+ throw new Error("focus-group: only DOM nodes allowed");
147
+ return t;
148
+ };
149
+ function r(t, n) {
150
+ for (var o in t)
151
+ if (n[o] !== void 0 && t[o] !== n[o]) return !1;
152
+ return !0;
153
+ }
154
+ function s(t) {
155
+ return t >= 65 && t <= 90;
156
+ }
157
+ function i(t) {
158
+ !t || !t.focus || (t.focus(), t.tagName.toLowerCase() === "input" && t.select());
159
+ }
160
+ return b = function(n) {
161
+ return new e(n);
162
+ }, b;
163
+ }
164
+ var W = C();
165
+ const P = /* @__PURE__ */ F(W), j = {
166
+ wrap: !0,
167
+ stringSearch: !0
168
+ }, q = {
169
+ options: {
170
+ closeOnSelection: !0,
171
+ closeOnBlur: !0
172
+ },
173
+ isOpen: !1,
174
+ button: null,
175
+ menu: null,
176
+ focusGroup: null,
177
+ blurTimer: void 0,
178
+ moveFocusTimer: void 0,
179
+ init(e) {
180
+ this.updateOptions(e), this.handleBlur = H.bind(this), this.handleSelection = U.bind(this), this.handleMenuKey = X.bind(this), this.focusGroup = P(j), this.button = null, this.menu = null, this.isOpen = !1;
181
+ },
182
+ updateOptions(e) {
183
+ const r = this.options;
184
+ this.options = {
185
+ ...e,
186
+ closeOnSelection: e?.closeOnSelection ?? !0,
187
+ closeOnBlur: e?.closeOnBlur ?? !0
188
+ }, this.options.id && G(this.options.id, this), r.id && r.id !== this.options.id && R(r.id);
189
+ },
190
+ focusItem(e) {
191
+ this.focusGroup.focusNodeAtIndex(e);
192
+ },
193
+ addItem(e) {
194
+ this.focusGroup.addMember(e);
195
+ },
196
+ clearItems() {
197
+ this.focusGroup.clearMembers();
198
+ },
199
+ handleButtonNonArrowKey(e) {
200
+ this.focusGroup._handleUnboundKey(e);
201
+ },
202
+ destroy() {
203
+ this.button = null, this.menu = null, this.focusGroup.deactivate(), clearTimeout(this.blurTimer), clearTimeout(this.moveFocusTimer);
204
+ },
205
+ update() {
206
+ this.menu?.setState?.({ isOpen: this.isOpen }), this.button?.setState?.({ menuOpen: this.isOpen }), this.options.onMenuToggle?.({ isOpen: this.isOpen });
207
+ },
208
+ openMenu(e) {
209
+ if (this.isOpen) return;
210
+ const s = (e ?? {}).focusMenu ?? !0;
211
+ this.isOpen = !0, this.update(), this.focusGroup.activate(), s && (this.moveFocusTimer = setTimeout(() => {
212
+ this.focusItem(0);
213
+ }, 0));
214
+ },
215
+ closeMenu(e) {
216
+ if (!this.isOpen) return;
217
+ const r = e ?? {};
218
+ this.isOpen = !1, this.update(), r.focusButton && this.button?.ref.current?.focus();
219
+ },
220
+ toggleMenu(e, r) {
221
+ const s = e ?? {}, i = r ?? {};
222
+ this.isOpen ? this.closeMenu(s) : this.openMenu(i);
223
+ },
224
+ handleBlur() {
225
+ },
226
+ handleSelection(e, r) {
227
+ },
228
+ handleMenuKey(e) {
229
+ }
230
+ };
231
+ function H() {
232
+ this.blurTimer = setTimeout(() => {
233
+ if (!this.button) return;
234
+ const e = this.button.ref.current;
235
+ if (!e) return;
236
+ const r = e.ownerDocument.activeElement;
237
+ if (r === e) return;
238
+ const s = this.menu?.ref.current;
239
+ if (s === r) {
240
+ this.focusItem(0);
241
+ return;
242
+ }
243
+ s?.contains(r) || this.isOpen && this.closeMenu({ focusButton: !1 });
244
+ }, 0);
245
+ }
246
+ function U(e, r) {
247
+ this.options.closeOnSelection && this.closeMenu({ focusButton: !0 }), this.options.onSelection?.(e, r);
248
+ }
249
+ function X(e) {
250
+ if (this.isOpen)
251
+ switch (e.key) {
252
+ // With focus on the drop-down menu, pressing Escape closes
253
+ // the menu and returns focus to the button.
254
+ case "Escape":
255
+ e.preventDefault(), this.closeMenu({ focusButton: !0 });
256
+ break;
257
+ case "Home":
258
+ e.preventDefault(), this.focusGroup.moveFocusToFirst();
259
+ break;
260
+ case "End":
261
+ e.preventDefault(), this.focusGroup.moveFocusToLast();
262
+ break;
263
+ }
264
+ }
265
+ function Y(e) {
266
+ const r = Object.create(q);
267
+ return r.init(e), r;
268
+ }
269
+ const T = D(
270
+ null
271
+ );
272
+ function E(e, r, s) {
273
+ const i = s ?? {};
274
+ for (const t in r)
275
+ Object.prototype.hasOwnProperty.call(r, t) && (i[t] || (e[t] = r[t]));
276
+ }
277
+ const $ = {
278
+ children: !0,
279
+ onMenuToggle: !0,
280
+ onSelection: !0,
281
+ closeOnSelection: !0,
282
+ closeOnBlur: !0,
283
+ tag: !0
284
+ };
285
+ function K(e) {
286
+ return {
287
+ onMenuToggle: e.onMenuToggle,
288
+ onSelection: e.onSelection,
289
+ closeOnSelection: e.closeOnSelection,
290
+ closeOnBlur: e.closeOnBlur,
291
+ id: e.id
292
+ };
293
+ }
294
+ const fe = O(function({
295
+ children: r,
296
+ onMenuToggle: s,
297
+ onSelection: i,
298
+ closeOnSelection: t,
299
+ closeOnBlur: n,
300
+ tag: o = "div",
301
+ ...u
302
+ }, f) {
303
+ const h = A(() => Y(
304
+ K({
305
+ onMenuToggle: s,
306
+ onSelection: i,
307
+ closeOnSelection: t,
308
+ closeOnBlur: n,
309
+ id: u.id
310
+ })
311
+ ), []);
312
+ y(() => {
313
+ h.updateOptions(
314
+ K({
315
+ onMenuToggle: s,
316
+ onSelection: i,
317
+ closeOnSelection: t,
318
+ closeOnBlur: n,
319
+ id: u.id
320
+ })
321
+ );
322
+ }, [
323
+ h,
324
+ s,
325
+ i,
326
+ t,
327
+ n,
328
+ u.id,
329
+ r
330
+ ]);
331
+ const a = {};
332
+ return E(a, u, $), /* @__PURE__ */ g(T.Provider, { value: h, children: w(o, a, r) });
333
+ }), V = [
334
+ "button",
335
+ "fieldset",
336
+ "input",
337
+ "optgroup",
338
+ "option",
339
+ "select",
340
+ "textarea"
341
+ ], z = {
342
+ ambManager: !0,
343
+ children: !0,
344
+ disabled: !0,
345
+ tag: !0
346
+ };
347
+ function J({
348
+ ambManager: e,
349
+ children: r,
350
+ disabled: s = !1,
351
+ tag: i = "span",
352
+ ...t
353
+ }) {
354
+ const n = v(null), [, o] = L(e.isOpen);
355
+ y(() => (e.button = {
356
+ ref: n,
357
+ setState: (c) => {
358
+ o(c.menuOpen);
359
+ }
360
+ }, () => {
361
+ e.destroy();
362
+ }), [e]);
363
+ const u = p(
364
+ (c) => {
365
+ if (!s)
366
+ switch (c.key) {
367
+ case "ArrowDown":
368
+ c.preventDefault(), e.isOpen ? e.focusItem(0) : e.openMenu();
369
+ break;
370
+ case "Enter":
371
+ case " ":
372
+ c.preventDefault(), e.toggleMenu();
373
+ break;
374
+ case "Escape":
375
+ e.handleMenuKey(c);
376
+ break;
377
+ default:
378
+ e.handleButtonNonArrowKey(
379
+ c.nativeEvent
380
+ );
381
+ }
382
+ },
383
+ [e, s]
384
+ ), f = p(
385
+ (c) => {
386
+ s || e.toggleMenu({}, { focusMenu: !1 });
387
+ },
388
+ [e, s]
389
+ ), h = p((c) => {
390
+ n.current = c;
391
+ }, []), a = {
392
+ // "The menu button itself has a role of button."
393
+ role: "button",
394
+ tabIndex: s ? -1 : 0,
395
+ // "The menu button has an aria-haspopup property, set to true."
396
+ "aria-haspopup": !0,
397
+ "aria-expanded": e.isOpen,
398
+ "aria-disabled": s,
399
+ onKeyDown: u,
400
+ onClick: f,
401
+ ref: h
402
+ };
403
+ return V.includes(i) && (a.disabled = s), e.options.closeOnBlur && (a.onBlur = (c) => {
404
+ e.handleBlur();
405
+ }), E(a, t, z), w(i, a, r);
406
+ }
407
+ const ae = O(
408
+ function(r, s) {
409
+ const i = x(T);
410
+ if (!i)
411
+ throw new Error("Button must be used within a Wrapper component");
412
+ return /* @__PURE__ */ g(J, { ...r, ambManager: i });
413
+ }
414
+ );
415
+ var k, B;
416
+ function Q() {
417
+ return B || (B = 1, k = function(r, s, i) {
418
+ var t = 0, n = 0, o = !1, u = !1, f = !1;
419
+ r.addEventListener("click", h, i), r.addEventListener("touchstart", a, i);
420
+ function h(d) {
421
+ f || s(d);
422
+ }
423
+ function a(d) {
424
+ f = !0, !o && (o = !0, r.addEventListener("touchmove", c, i), r.addEventListener("touchend", l, i), r.addEventListener("touchcancel", _, i), u = !1, t = d.touches[0].clientX, n = d.touches[0].clientY);
425
+ }
426
+ function c(d) {
427
+ u || Math.abs(d.touches[0].clientX - t) <= 10 && Math.abs(d.touches[0].clientY - n) <= 10 || (u = !0);
428
+ }
429
+ function l(d) {
430
+ o = !1, m(), u || s(d);
431
+ }
432
+ function _() {
433
+ o = !1, u = !1, t = 0, n = 0;
434
+ }
435
+ function m() {
436
+ r.removeEventListener("touchmove", c, i), r.removeEventListener("touchend", l, i), r.removeEventListener("touchcancel", _, i);
437
+ }
438
+ function N() {
439
+ r.removeEventListener("click", h, i), r.removeEventListener("touchstart", a, i), m();
440
+ }
441
+ return {
442
+ remove: N
443
+ };
444
+ }), k;
445
+ }
446
+ var Z = Q();
447
+ const ee = /* @__PURE__ */ F(Z), te = {
448
+ ambManager: !0,
449
+ children: !0,
450
+ tag: !0
451
+ };
452
+ function ne({
453
+ ambManager: e,
454
+ children: r,
455
+ tag: s = "div",
456
+ ...i
457
+ }) {
458
+ const t = v(null), n = v(null), o = v(
459
+ null
460
+ ), [, u] = L(e.isOpen), f = p(() => {
461
+ const l = t.current;
462
+ if (!l) return;
463
+ const _ = (m) => {
464
+ t.current?.contains(m.target) || e.button?.ref.current?.contains(m.target) || e.closeMenu();
465
+ };
466
+ n.current = ee(
467
+ l.ownerDocument.documentElement,
468
+ _
469
+ );
470
+ }, [e]);
471
+ y(() => (e.menu = {
472
+ ref: t,
473
+ setState: (l) => {
474
+ u(l.isOpen);
475
+ }
476
+ }, () => {
477
+ n.current && n.current.remove(), o.current && clearTimeout(o.current), e.destroy();
478
+ }), [e]), y(() => {
479
+ e.options.closeOnBlur && (e.isOpen && !n.current ? o.current = setTimeout(() => {
480
+ f(), o.current = null;
481
+ }, 0) : !e.isOpen && n.current && (n.current.remove(), n.current = null), e.isOpen || e.clearItems());
482
+ }, [e, e.isOpen, f]);
483
+ const h = p((l) => {
484
+ t.current = l;
485
+ }, []), a = typeof r == "function" ? r({ isOpen: e.isOpen }) : e.isOpen ? r : null;
486
+ if (!a) return null;
487
+ const c = {
488
+ onKeyDown: (l) => {
489
+ e.handleMenuKey(l);
490
+ },
491
+ role: "menu",
492
+ tabIndex: -1,
493
+ ref: h
494
+ };
495
+ return e.options.closeOnBlur && (c.onBlur = (l) => {
496
+ e.handleBlur();
497
+ }), E(c, i, te), w(s, c, a);
498
+ }
499
+ const he = O(function(r, s) {
500
+ const i = x(T);
501
+ if (!i)
502
+ throw new Error("Menu must be used within a Wrapper component");
503
+ return /* @__PURE__ */ g(ne, { ...r, ambManager: i });
504
+ }), re = {
505
+ ambManager: !0,
506
+ children: !0,
507
+ tag: !0,
508
+ text: !0,
509
+ value: !0
510
+ };
511
+ function oe({
512
+ ambManager: e,
513
+ children: r,
514
+ tag: s = "div",
515
+ text: i,
516
+ value: t,
517
+ ...n
518
+ }) {
519
+ const o = v(null);
520
+ y(() => {
521
+ o.current && e.addItem({
522
+ node: o.current,
523
+ text: i
524
+ });
525
+ }, [e, i]);
526
+ const u = p(
527
+ (c) => {
528
+ const l = t !== void 0 ? t : r;
529
+ e.handleSelection(
530
+ l,
531
+ c
532
+ );
533
+ },
534
+ [e, t, r]
535
+ ), f = p(
536
+ (c) => {
537
+ c.key !== "Enter" && c.key !== " " || s === "a" && "href" in n && n.href !== void 0 || (c.preventDefault(), u(c));
538
+ },
539
+ [s, n, u]
540
+ ), h = p((c) => {
541
+ o.current = c;
542
+ }, []), a = {
543
+ onClick: u,
544
+ onKeyDown: f,
545
+ role: "menuitem",
546
+ tabIndex: -1,
547
+ ref: h
548
+ };
549
+ return E(a, n, re), w(s, a, r);
550
+ }
551
+ const le = O(
552
+ function(r, s) {
553
+ const i = x(T);
554
+ if (!i)
555
+ throw new Error("MenuItem must be used within a Wrapper component");
556
+ return /* @__PURE__ */ g(oe, { ...r, ambManager: i });
557
+ }
558
+ );
559
+ export {
560
+ ae as Button,
561
+ he as Menu,
562
+ le as MenuItem,
563
+ fe as Wrapper,
564
+ ce as closeMenu,
565
+ ue as openMenu
566
+ };
@@ -0,0 +1 @@
1
+ (function(p,v){typeof exports=="object"&&typeof module<"u"?v(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],v):(p=typeof globalThis<"u"?globalThis:p||self,v(p.ReactAriaMenuButton={},p.ReactJsxRuntime,p.React))})(this,(function(p,v,u){"use strict";const _=new Map,T="a menu outside a mounted Wrapper with an id, or a menu that does not exist";function B(e,r){_.set(e,r)}function K(e){_.delete(e)}function M(e,r){const s=_.get(e);if(!s)throw new Error("Cannot open "+T);s.openMenu(r)}function L(e,r){const s=_.get(e);if(!s)throw new Error("Cannot close "+T);s.closeMenu(r)}function b(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var S,k;function F(){if(k)return S;k=1;function e(t){t=t||{};var n=t.keybindings||{};this._settings={keybindings:{next:n.next||{keyCode:40},prev:n.prev||{keyCode:38},first:n.first,last:n.last},wrap:t.wrap,stringSearch:t.stringSearch,stringSearchDelay:800},this._keybindingsLookup=[];var o,c;for(o in this._settings.keybindings)c=this._settings.keybindings[o],c&&[].concat(c).forEach((function(l){l.metaKey=l.metaKey||!1,l.ctrlKey=l.ctrlKey||!1,l.altKey=l.altKey||!1,l.shiftKey=l.shiftKey||!1,this._keybindingsLookup.push({action:o,eventMatcher:l})}).bind(this));this._searchString="",this._members=[],t.members&&this.setMembers(t.members),this._boundHandleKeydownEvent=this._handleKeydownEvent.bind(this)}e.prototype.activate=function(){return document.addEventListener("keydown",this._boundHandleKeydownEvent,!0),this},e.prototype.deactivate=function(){return document.removeEventListener("keydown",this._boundHandleKeydownEvent,!0),this._clearSearchStringRefreshTimer(),this},e.prototype._handleKeydownEvent=function(t){var n=this._getActiveElementIndex();if(n!==-1){var o=!1;this._keybindingsLookup.forEach((function(c){if(r(c.eventMatcher,t))switch(o=!0,t.preventDefault(),c.action){case"next":this.moveFocusForward();break;case"prev":this.moveFocusBack();break;case"first":this.moveFocusToFirst();break;case"last":this.moveFocusToLast();break;default:return}}).bind(this)),o||this._handleUnboundKey(t)}},e.prototype.moveFocusForward=function(){var t=this._getActiveElementIndex(),n;return t<this._members.length-1?n=t+1:this._settings.wrap?n=0:n=t,this.focusNodeAtIndex(n),n},e.prototype.moveFocusBack=function(){var t=this._getActiveElementIndex(),n;return t>0?n=t-1:this._settings.wrap?n=this._members.length-1:n=t,this.focusNodeAtIndex(n),n},e.prototype.moveFocusToFirst=function(){this.focusNodeAtIndex(0)},e.prototype.moveFocusToLast=function(){this.focusNodeAtIndex(this._members.length-1)},e.prototype._handleUnboundKey=function(t){if(this._settings.stringSearch){if(this._searchString!==""&&(t.key===" "||t.keyCode===32))return t.preventDefault(),-1;if(!s(t.keyCode)||t.ctrlKey||t.metaKey||t.altKey)return-1;t.preventDefault(),this._addToSearchString(String.fromCharCode(t.keyCode)),this._runStringSearch()}},e.prototype._clearSearchString=function(){this._searchString=""},e.prototype._addToSearchString=function(t){this._searchString+=t.toLowerCase()},e.prototype._startSearchStringRefreshTimer=function(){var t=this;this._clearSearchStringRefreshTimer(),this._stringSearchTimer=setTimeout(function(){t._clearSearchString()},this._settings.stringSearchDelay)},e.prototype._clearSearchStringRefreshTimer=function(){clearTimeout(this._stringSearchTimer)},e.prototype._runStringSearch=function(){this._startSearchStringRefreshTimer(),this.moveFocusByString(this._searchString)},e.prototype.moveFocusByString=function(t){for(var n,o=0,c=this._members.length;o<c;o++)if(n=this._members[o],!!n.text&&n.text.indexOf(t)===0)return i(n.node)},e.prototype._findIndexOfNode=function(t){for(var n=0,o=this._members.length;n<o;n++)if(this._members[n].node===t)return n;return-1},e.prototype._getActiveElementIndex=function(){return this._findIndexOfNode(document.activeElement)},e.prototype.focusNodeAtIndex=function(t){var n=this._members[t];return n&&i(n.node),this},e.prototype.addMember=function(t,n){var o=t.node||t,c=t.text||o.getAttribute("data-focus-group-text")||o.textContent||"";this._checkNode(o);var l=c.replace(/[\W_]/g,"").toLowerCase(),a={node:o,text:l};return n!=null?this._members.splice(n,0,a):this._members.push(a),this},e.prototype.removeMember=function(t){var n=typeof t=="number"?t:this._findIndexOfNode(t);if(n!==-1)return this._members.splice(n,1),this},e.prototype.clearMembers=function(){return this._members=[],this},e.prototype.setMembers=function(t){this.clearMembers();for(var n=0,o=t.length;n<o;n++)this.addMember(t[n]);return this},e.prototype.getMembers=function(){return this._members},e.prototype._checkNode=function(t){if(!t.nodeType||t.nodeType!==window.Node.ELEMENT_NODE)throw new Error("focus-group: only DOM nodes allowed");return t};function r(t,n){for(var o in t)if(n[o]!==void 0&&t[o]!==n[o])return!1;return!0}function s(t){return t>=65&&t<=90}function i(t){!t||!t.focus||(t.focus(),t.tagName.toLowerCase()==="input"&&t.select())}return S=function(n){return new e(n)},S}var C=F();const N=b(C),R={wrap:!0,stringSearch:!0},A={options:{closeOnSelection:!0,closeOnBlur:!0},isOpen:!1,button:null,menu:null,focusGroup:null,blurTimer:void 0,moveFocusTimer:void 0,init(e){this.updateOptions(e),this.handleBlur=D.bind(this),this.handleSelection=G.bind(this),this.handleMenuKey=j.bind(this),this.focusGroup=N(R),this.button=null,this.menu=null,this.isOpen=!1},updateOptions(e){const r=this.options;this.options={...e,closeOnSelection:e?.closeOnSelection??!0,closeOnBlur:e?.closeOnBlur??!0},this.options.id&&B(this.options.id,this),r.id&&r.id!==this.options.id&&K(r.id)},focusItem(e){this.focusGroup.focusNodeAtIndex(e)},addItem(e){this.focusGroup.addMember(e)},clearItems(){this.focusGroup.clearMembers()},handleButtonNonArrowKey(e){this.focusGroup._handleUnboundKey(e)},destroy(){this.button=null,this.menu=null,this.focusGroup.deactivate(),clearTimeout(this.blurTimer),clearTimeout(this.moveFocusTimer)},update(){this.menu?.setState?.({isOpen:this.isOpen}),this.button?.setState?.({menuOpen:this.isOpen}),this.options.onMenuToggle?.({isOpen:this.isOpen})},openMenu(e){if(this.isOpen)return;const s=(e??{}).focusMenu??!0;this.isOpen=!0,this.update(),this.focusGroup.activate(),s&&(this.moveFocusTimer=setTimeout(()=>{this.focusItem(0)},0))},closeMenu(e){if(!this.isOpen)return;const r=e??{};this.isOpen=!1,this.update(),r.focusButton&&this.button?.ref.current?.focus()},toggleMenu(e,r){const s=e??{},i=r??{};this.isOpen?this.closeMenu(s):this.openMenu(i)},handleBlur(){},handleSelection(e,r){},handleMenuKey(e){}};function D(){this.blurTimer=setTimeout(()=>{if(!this.button)return;const e=this.button.ref.current;if(!e)return;const r=e.ownerDocument.activeElement;if(r===e)return;const s=this.menu?.ref.current;if(s===r){this.focusItem(0);return}s?.contains(r)||this.isOpen&&this.closeMenu({focusButton:!1})},0)}function G(e,r){this.options.closeOnSelection&&this.closeMenu({focusButton:!0}),this.options.onSelection?.(e,r)}function j(e){if(this.isOpen)switch(e.key){case"Escape":e.preventDefault(),this.closeMenu({focusButton:!0});break;case"Home":e.preventDefault(),this.focusGroup.moveFocusToFirst();break;case"End":e.preventDefault(),this.focusGroup.moveFocusToLast();break}}function W(e){const r=Object.create(A);return r.init(e),r}const g=u.createContext(null);function O(e,r,s){const i=s??{};for(const t in r)Object.prototype.hasOwnProperty.call(r,t)&&(i[t]||(e[t]=r[t]))}const P={children:!0,onMenuToggle:!0,onSelection:!0,closeOnSelection:!0,closeOnBlur:!0,tag:!0};function x(e){return{onMenuToggle:e.onMenuToggle,onSelection:e.onSelection,closeOnSelection:e.closeOnSelection,closeOnBlur:e.closeOnBlur,id:e.id}}const q=u.forwardRef(function({children:r,onMenuToggle:s,onSelection:i,closeOnSelection:t,closeOnBlur:n,tag:o="div",...c},l){const a=u.useMemo(()=>W(x({onMenuToggle:s,onSelection:i,closeOnSelection:t,closeOnBlur:n,id:c.id})),[]);u.useEffect(()=>{a.updateOptions(x({onMenuToggle:s,onSelection:i,closeOnSelection:t,closeOnBlur:n,id:c.id}))},[a,s,i,t,n,c.id,r]);const h={};return O(h,c,P),v.jsx(g.Provider,{value:a,children:u.createElement(o,h,r)})}),H=["button","fieldset","input","optgroup","option","select","textarea"],U={ambManager:!0,children:!0,disabled:!0,tag:!0};function X({ambManager:e,children:r,disabled:s=!1,tag:i="span",...t}){const n=u.useRef(null),[,o]=u.useState(e.isOpen);u.useEffect(()=>(e.button={ref:n,setState:f=>{o(f.menuOpen)}},()=>{e.destroy()}),[e]);const c=u.useCallback(f=>{if(!s)switch(f.key){case"ArrowDown":f.preventDefault(),e.isOpen?e.focusItem(0):e.openMenu();break;case"Enter":case" ":f.preventDefault(),e.toggleMenu();break;case"Escape":e.handleMenuKey(f);break;default:e.handleButtonNonArrowKey(f.nativeEvent)}},[e,s]),l=u.useCallback(f=>{s||e.toggleMenu({},{focusMenu:!1})},[e,s]),a=u.useCallback(f=>{n.current=f},[]),h={role:"button",tabIndex:s?-1:0,"aria-haspopup":!0,"aria-expanded":e.isOpen,"aria-disabled":s,onKeyDown:c,onClick:l,ref:a};return H.includes(i)&&(h.disabled=s),e.options.closeOnBlur&&(h.onBlur=f=>{e.handleBlur()}),O(h,t,U),u.createElement(i,h,r)}const Y=u.forwardRef(function(r,s){const i=u.useContext(g);if(!i)throw new Error("Button must be used within a Wrapper component");return v.jsx(X,{...r,ambManager:i})});var E,I;function $(){return I||(I=1,E=function(r,s,i){var t=0,n=0,o=!1,c=!1,l=!1;r.addEventListener("click",a,i),r.addEventListener("touchstart",h,i);function a(m){l||s(m)}function h(m){l=!0,!o&&(o=!0,r.addEventListener("touchmove",f,i),r.addEventListener("touchend",d,i),r.addEventListener("touchcancel",w,i),c=!1,t=m.touches[0].clientX,n=m.touches[0].clientY)}function f(m){c||Math.abs(m.touches[0].clientX-t)<=10&&Math.abs(m.touches[0].clientY-n)<=10||(c=!0)}function d(m){o=!1,y(),c||s(m)}function w(){o=!1,c=!1,t=0,n=0}function y(){r.removeEventListener("touchmove",f,i),r.removeEventListener("touchend",d,i),r.removeEventListener("touchcancel",w,i)}function re(){r.removeEventListener("click",a,i),r.removeEventListener("touchstart",h,i),y()}return{remove:re}}),E}var J=$();const V=b(J),z={ambManager:!0,children:!0,tag:!0};function Q({ambManager:e,children:r,tag:s="div",...i}){const t=u.useRef(null),n=u.useRef(null),o=u.useRef(null),[,c]=u.useState(e.isOpen),l=u.useCallback(()=>{const d=t.current;if(!d)return;const w=y=>{t.current?.contains(y.target)||e.button?.ref.current?.contains(y.target)||e.closeMenu()};n.current=V(d.ownerDocument.documentElement,w)},[e]);u.useEffect(()=>(e.menu={ref:t,setState:d=>{c(d.isOpen)}},()=>{n.current&&n.current.remove(),o.current&&clearTimeout(o.current),e.destroy()}),[e]),u.useEffect(()=>{e.options.closeOnBlur&&(e.isOpen&&!n.current?o.current=setTimeout(()=>{l(),o.current=null},0):!e.isOpen&&n.current&&(n.current.remove(),n.current=null),e.isOpen||e.clearItems())},[e,e.isOpen,l]);const a=u.useCallback(d=>{t.current=d},[]),h=typeof r=="function"?r({isOpen:e.isOpen}):e.isOpen?r:null;if(!h)return null;const f={onKeyDown:d=>{e.handleMenuKey(d)},role:"menu",tabIndex:-1,ref:a};return e.options.closeOnBlur&&(f.onBlur=d=>{e.handleBlur()}),O(f,i,z),u.createElement(s,f,h)}const Z=u.forwardRef(function(r,s){const i=u.useContext(g);if(!i)throw new Error("Menu must be used within a Wrapper component");return v.jsx(Q,{...r,ambManager:i})}),ee={ambManager:!0,children:!0,tag:!0,text:!0,value:!0};function te({ambManager:e,children:r,tag:s="div",text:i,value:t,...n}){const o=u.useRef(null);u.useEffect(()=>{o.current&&e.addItem({node:o.current,text:i})},[e,i]);const c=u.useCallback(f=>{const d=t!==void 0?t:r;e.handleSelection(d,f)},[e,t,r]),l=u.useCallback(f=>{f.key!=="Enter"&&f.key!==" "||s==="a"&&"href"in n&&n.href!==void 0||(f.preventDefault(),c(f))},[s,n,c]),a=u.useCallback(f=>{o.current=f},[]),h={onClick:c,onKeyDown:l,role:"menuitem",tabIndex:-1,ref:a};return O(h,n,ee),u.createElement(s,h,r)}const ne=u.forwardRef(function(r,s){const i=u.useContext(g);if(!i)throw new Error("MenuItem must be used within a Wrapper component");return v.jsx(te,{...r,ambManager:i})});p.Button=Y,p.Menu=Z,p.MenuItem=ne,p.Wrapper=q,p.closeMenu=L,p.openMenu=M,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));