@versini/ui-menu 7.6.3 → 7.6.5

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 (2) hide show
  1. package/dist/index.js +94 -13
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- @versini/ui-menu v7.6.3
2
+ @versini/ui-menu v7.6.5
3
3
  © 2026 gizmette.com
4
4
  */
5
5
 
@@ -15,6 +15,51 @@ import * as __rspack_external_react from "react";
15
15
 
16
16
 
17
17
 
18
+ /**
19
+ * Returning focus to the trigger without asking for a focus ring.
20
+ *
21
+ * Closing a menu hands focus back to its trigger. Whether such a script-moved
22
+ * focus should be RINGED is a heuristic the spec leaves to the UA, and the
23
+ * engines disagree: measured in Safari 27 / WebKit 605.1.15, `button.focus()`
24
+ * matches `:focus-visible` even from a cold start with nothing focused
25
+ * beforehand, while Chromium declines to. So in WebKit, dismissing a menu with
26
+ * a tap painted a keyboard focus ring on the trigger.
27
+ *
28
+ * SCOPED TO THE TRIGGER on purpose. A menu focuses items constantly -- on
29
+ * hover, on open, on arrow keys -- and none of those needs this: every item
30
+ * carries `outline-hidden` from `getMenuItemClasses`, an author declaration
31
+ * that outranks the UA's `:focus-visible` outline, so an item cannot ring
32
+ * however it is focused. The trigger is the exception because it is supplied by
33
+ * the consumer, typically a `ui-button` carrying `focus-visible:outline-*`.
34
+ *
35
+ * ONLY `false` IS EVER PASSED, and that asymmetry is the design. A
36
+ * pointer-driven dismissal knows the ring is wrong and says so. A
37
+ * keyboard-driven one says NOTHING and calls `focus()` bare, leaving the
38
+ * decision to the UA, because that is the case the heuristic already gets right
39
+ * -- passing `focusVisible: true` would override it rather than agree with it.
40
+ *
41
+ */ /**
42
+ * Focus `element`, suppressing the ring when we know a pointer caused it.
43
+ *
44
+ * `element` is optional so call sites can pass a ref's `current` straight in
45
+ * without repeating the null check at seven sites.
46
+ *
47
+ */ const restoreFocus = (element, { viaPointer } = {})=>{
48
+ element?.focus(viaPointer ? {
49
+ focusVisible: false
50
+ } : undefined);
51
+ };
52
+ /**
53
+ * Whether a click came from a pointer rather than from a keypress.
54
+ *
55
+ * `useMenuKeyboard` activates Enter and Space by calling `element.click()`, so
56
+ * a keyboard activation and a real tap arrive at the SAME handler and the call
57
+ * site alone cannot tell them apart. `detail` can: a synthetic click reports 0
58
+ * and a pointer click reports 1 or more. Measured in WebKit rather than
59
+ * assumed.
60
+ *
61
+ */ const isPointerClick = (event)=>event.detail > 0;
62
+
18
63
 
19
64
 
20
65
  const MenuGroupContext = createContext(false);
@@ -459,6 +504,7 @@ function useMenuPosition({ triggerRef, menuRef, placement, sideOffset, isOpen })
459
504
 
460
505
 
461
506
 
507
+
462
508
  const Menu = ({ trigger, children, label = "Open menu", defaultPlacement = "bottom-start", onOpenChange, mode = "system", sideOffset = 10, noBackdrop = false })=>{
463
509
  const contentClass = getMenuClasses({
464
510
  isSub: false,
@@ -491,7 +537,13 @@ const Menu = ({ trigger, children, label = "Open menu", defaultPlacement = "bott
491
537
  }, [
492
538
  isOpen
493
539
  ]);
494
- /* v8 ignore stop */ const handleClose = (0,__rspack_external_react.useCallback)(()=>{
540
+ /* v8 ignore stop */ /**
541
+ * `options` carries whether a POINTER closed the menu, because focus returns
542
+ * to the trigger here and the three ways in disagree: `useMenuKeyboard`
543
+ * reaches this on Escape and Tab, `handleToggle` on a click of the trigger
544
+ * itself, and `MenuItem` on an activation that may be either. Each passes what
545
+ * it knows; see `focusRestore.ts` for why only the pointer case speaks up.
546
+ */ const handleClose = (0,__rspack_external_react.useCallback)((options)=>{
495
547
  /* v8 ignore start - guard against double-close */ if (!isOpen) {
496
548
  return;
497
549
  }
@@ -506,7 +558,7 @@ const Menu = ({ trigger, children, label = "Open menu", defaultPlacement = "bott
506
558
  // Popover might already be hidden.
507
559
  }
508
560
  }
509
- /* v8 ignore stop */ triggerRef.current?.focus();
561
+ /* v8 ignore stop */ restoreFocus(triggerRef.current, options);
510
562
  }, [
511
563
  isOpen,
512
564
  onOpenChange
@@ -523,7 +575,10 @@ const Menu = ({ trigger, children, label = "Open menu", defaultPlacement = "bott
523
575
  setActiveIndex(-1);
524
576
  setOpenSubMenuId(null);
525
577
  onOpenChange?.(false);
526
- triggerRef.current?.focus();
578
+ // A mousedown outside is unambiguously a pointer.
579
+ restoreFocus(triggerRef.current, {
580
+ viaPointer: true
581
+ });
527
582
  }
528
583
  };
529
584
  // Use a timeout to avoid catching the opening click itself.
@@ -592,20 +647,41 @@ const Menu = ({ trigger, children, label = "Open menu", defaultPlacement = "bott
592
647
  getItems,
593
648
  focusItem
594
649
  ]);
595
- const handleToggle = (0,__rspack_external_react.useCallback)(()=>{
650
+ /**
651
+ * Only the CLOSE branch classifies. Closing hands focus back to the trigger,
652
+ * which is the one element around a menu that can show a ring; opening focuses
653
+ * a menu item, and items carry `outline-hidden`, so nothing there rings
654
+ * whatever it is focused with.
655
+ *
656
+ * `isPointerClick` answers "pointer" every time TODAY, and that is not an
657
+ * oversight. `handleTriggerKeyDown` below intercepts Enter, Space and
658
+ * ArrowDown, calls `preventDefault` so no click is synthesized, and opens the
659
+ * menu itself -- so keyboard activation never arrives here at all. Measured,
660
+ * not read: forcing this to classify every toggle as a pointer leaves the
661
+ * keyboard tests green, which is only possible if the keyboard never reaches
662
+ * this code.
663
+ *
664
+ * Kept as a classifier rather than hardcoded to `true` because it stays
665
+ * correct if that interception is ever removed or narrowed, where a hardcoded
666
+ * value would silently start lying about a keyboard press.
667
+ *
668
+ */ const handleToggle = (0,__rspack_external_react.useCallback)((event)=>{
596
669
  /* v8 ignore start - re-entrant guard for synthetic click dispatch */ if (isTogglingRef.current) {
597
670
  return;
598
671
  }
599
672
  /* v8 ignore stop */ isTogglingRef.current = true;
600
- /* v8 ignore start - toggle close when clicking trigger while menu is open */ if (isOpen) {
601
- handleClose();
673
+ /* v8 ignore start - toggle close when clicking trigger while menu is open */ const viaPointer = isPointerClick(event);
674
+ if (isOpen) {
675
+ handleClose({
676
+ viaPointer
677
+ });
602
678
  } else {
603
679
  /* v8 ignore stop */ handleOpen();
604
680
  /**
605
- * Dispatch a click event to parent elements. This ensures that parent
606
- * components like Tooltip can detect the interaction and respond
607
- * appropriately (e.g., disable tooltip display).
608
- */ /* v8 ignore start */ if (triggerRef.current) {
681
+ * Dispatch a click event to parent elements. This ensures that parent
682
+ * components like Tooltip can detect the interaction and respond
683
+ * appropriately (e.g., disable tooltip display).
684
+ */ /* v8 ignore start */ if (triggerRef.current) {
609
685
  const clickEvent = new MouseEvent("click", {
610
686
  bubbles: true,
611
687
  cancelable: true,
@@ -722,6 +798,7 @@ MenuGroup.displayName = "MenuGroup";
722
798
 
723
799
 
724
800
 
801
+
725
802
  const ITEM_CLASS = getMenuItemClasses({
726
803
  isSub: false
727
804
  });
@@ -766,7 +843,9 @@ const MenuItem = ({ label, disabled, icon, raw = false, children, ignoreClick =
766
843
  onSelect?.(syntheticEvent);
767
844
  /* v8 ignore start - optional onClick may not be provided */ onClick?.(event);
768
845
  /* v8 ignore stop */ if (!ignoreClick) {
769
- closeAll();
846
+ closeAll({
847
+ viaPointer: isPointerClick(event)
848
+ });
770
849
  }
771
850
  };
772
851
  const handleMouseEnter = (event)=>{
@@ -805,7 +884,9 @@ const MenuItem = ({ label, disabled, icon, raw = false, children, ignoreClick =
805
884
  onSelect?.(syntheticEvent);
806
885
  /* v8 ignore start - optional onClick may not be provided */ onClick?.(event);
807
886
  /* v8 ignore stop */ if (!ignoreClick) {
808
- closeAll();
887
+ closeAll({
888
+ viaPointer: isPointerClick(event)
889
+ });
809
890
  }
810
891
  },
811
892
  onMouseEnter: handleMouseEnter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-menu",
3
- "version": "7.6.3",
3
+ "version": "7.6.5",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -22,10 +22,10 @@
22
22
  "@testing-library/jest-dom": "7.0.1"
23
23
  },
24
24
  "dependencies": {
25
- "@versini/ui-icons": "4.29.2",
25
+ "@versini/ui-hooks": "6.2.2",
26
+ "@versini/ui-icons": "4.29.3",
26
27
  "clsx": "2.1.1",
27
- "tailwindcss": "4.3.3",
28
- "@versini/ui-hooks": "6.2.2"
28
+ "tailwindcss": "4.3.3"
29
29
  },
30
30
  "sideEffects": [
31
31
  "**/*.css"