@mirohq/design-system-dropdown-menu 4.4.9 → 4.4.10-dropdown-on-pointerup.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/module.js CHANGED
@@ -14,7 +14,8 @@ import { useLayoutEffect } from '@mirohq/design-system-use-layout-effect';
14
14
  import { useAriaDisabled as useAriaDisabled$1 } from '@mirohq/design-system-use-aria-disabled';
15
15
  import { ScrollArea } from '@mirohq/design-system-scroll-area';
16
16
  import { styles, Thumb } from '@mirohq/design-system-base-switch';
17
- import { isVirtualClick } from '@react-aria/utils';
17
+ import { mergeProps, isVirtualClick } from '@react-aria/utils';
18
+ import { usePress } from '@mirohq/design-system-use-press';
18
19
  import { styles as styles$1, isIconComponent } from '@mirohq/design-system-base-icon';
19
20
 
20
21
  const ItemDescription = styled(Primitive.div, {
@@ -454,16 +455,25 @@ const ScrollableContent = ({
454
455
  const DropdownContext = createContext({});
455
456
  const DropdownProvider = ({
456
457
  children,
458
+ open: rootOpen,
457
459
  ...restProps
458
- }) => /* @__PURE__ */ jsx(
459
- DropdownContext.Provider,
460
- {
461
- value: {
462
- ...restProps
463
- },
464
- children
465
- }
466
- );
460
+ }) => {
461
+ const [open, setOpen] = useState(rootOpen);
462
+ const triggerRef = useRef(null);
463
+ return /* @__PURE__ */ jsx(
464
+ DropdownContext.Provider,
465
+ {
466
+ value: {
467
+ ...restProps,
468
+ rootOpen,
469
+ open,
470
+ setOpen,
471
+ triggerRef
472
+ },
473
+ children
474
+ }
475
+ );
476
+ };
467
477
  const useDropdownContext = () => useContext(DropdownContext);
468
478
 
469
479
  const Content = React.forwardRef(
@@ -480,10 +490,11 @@ const Content = React.forwardRef(
480
490
  containerSpacing = "medium",
481
491
  overflow = "visible",
482
492
  maxHeight,
493
+ onInteractOutside,
483
494
  children,
484
495
  ...restProps
485
496
  }, forwardRef) => {
486
- const { direction } = useDropdownContext();
497
+ const { direction, triggerRef } = useDropdownContext();
487
498
  return /* @__PURE__ */ jsx(ContentProvider, { containerSpacing, children: /* @__PURE__ */ jsx(
488
499
  StyledContent,
489
500
  {
@@ -498,6 +509,12 @@ const Content = React.forwardRef(
498
509
  collisionPadding,
499
510
  sticky,
500
511
  hideWhenDetached,
512
+ onInteractOutside: (e) => {
513
+ if (e.target === triggerRef.current) {
514
+ e.preventDefault();
515
+ }
516
+ onInteractOutside == null ? void 0 : onInteractOutside(e);
517
+ },
501
518
  children: /* @__PURE__ */ jsx(
502
519
  ScrollableContent,
503
520
  {
@@ -703,8 +720,9 @@ const StyledTrigger = styled(RadixDropdownMenu.Trigger, {
703
720
  }
704
721
  });
705
722
 
706
- const Trigger = React.forwardRef(({ asChild = false, onClick, ...restProps }, forwardRef) => {
723
+ const Trigger = React.forwardRef(({ asChild = false, ...restProps }, forwardRef) => {
707
724
  const ref = useRef(null);
725
+ const { setOpen, rootOpen, triggerRef } = useDropdownContext();
708
726
  const handleVirtualClick = (e) => {
709
727
  var _a;
710
728
  if ((e == null ? void 0 : e.nativeEvent) === void 0) {
@@ -725,15 +743,40 @@ const Trigger = React.forwardRef(({ asChild = false, onClick, ...restProps }, fo
725
743
  (_a = ref.current) == null ? void 0 : _a.dispatchEvent(pointerDownEvent);
726
744
  }
727
745
  };
746
+ const { pressProps } = usePress({
747
+ onPress: () => {
748
+ if (rootOpen === void 0) {
749
+ setOpen((open) => !booleanify(open));
750
+ }
751
+ }
752
+ });
753
+ const elementProps = mergeProps(restProps, pressProps);
728
754
  return /* @__PURE__ */ jsx(
729
755
  StyledTrigger,
730
756
  {
731
- ...restProps,
757
+ ...elementProps,
732
758
  onClick: (e) => {
759
+ var _a, _b;
733
760
  handleVirtualClick(e);
734
- onClick == null ? void 0 : onClick(e);
761
+ if (e instanceof MouseEvent) {
762
+ e.preventDefault();
763
+ (_a = elementProps.onClick) == null ? void 0 : _a.call(elementProps, e);
764
+ } else {
765
+ (_b = restProps.onClick) == null ? void 0 : _b.call(restProps, e);
766
+ }
735
767
  },
736
- ref: mergeRefs([ref, forwardRef]),
768
+ onKeyDown: (e) => {
769
+ var _a, _b;
770
+ if ((e == null ? void 0 : e.nativeEvent) instanceof KeyboardEvent) {
771
+ if (!/^(Arrow|Tab|Escape)/.test(e.key)) {
772
+ e.preventDefault();
773
+ }
774
+ (_a = elementProps.onKeyDown) == null ? void 0 : _a.call(elementProps, e);
775
+ } else {
776
+ (_b = restProps.onKeyDown) == null ? void 0 : _b.call(restProps, e);
777
+ }
778
+ },
779
+ ref: mergeRefs([ref, triggerRef, forwardRef]),
737
780
  unstyled: !asChild,
738
781
  asChild
739
782
  }
@@ -884,39 +927,52 @@ const Root = ({
884
927
  defaultOpen = false,
885
928
  direction,
886
929
  interactOutside = false,
887
- open,
888
930
  onOpen,
889
931
  onClose,
890
932
  ...restProps
891
933
  }) => {
892
934
  const { ignoreNextTooltip } = useBaseTooltipContext();
935
+ const { rootOpen, open, setOpen } = useDropdownContext();
893
936
  const prevOpen = usePrevious(open);
894
937
  useEffect(() => {
895
- if (prevOpen === true && open === false) {
896
- ignoreNextTooltip();
938
+ if (prevOpen !== open) {
939
+ if (open === true) {
940
+ onOpen == null ? void 0 : onOpen();
941
+ } else {
942
+ if (prevOpen === true) {
943
+ ignoreNextTooltip();
944
+ }
945
+ onClose == null ? void 0 : onClose();
946
+ }
897
947
  }
898
- }, [ignoreNextTooltip, open, prevOpen]);
948
+ }, [open, prevOpen, onOpen, onClose, ignoreNextTooltip]);
899
949
  return /* @__PURE__ */ jsx(
900
950
  RadixDropdownMenu.Root,
901
951
  {
902
952
  ...restProps,
903
953
  dir: direction,
904
954
  modal: interactOutside,
905
- open,
955
+ open: (
956
+ // use the root open state if it is defined
957
+ rootOpen != null ? rootOpen : defaultOpen && (prevOpen === void 0 || prevOpen === open) ? (
958
+ // only use defaultOpen in the first render then use the context open state
959
+ // if the open state is the same as the previous one, it is most likely a re-render and we should still use the defaultOpen
960
+ void 0
961
+ ) : (
962
+ // otherwise, use the user open state
963
+ open
964
+ )
965
+ ),
906
966
  defaultOpen,
907
- onOpenChange: (newOpen) => {
908
- if (!newOpen && open === void 0) {
909
- ignoreNextTooltip();
910
- }
911
- newOpen ? onOpen == null ? void 0 : onOpen() : onClose == null ? void 0 : onClose();
912
- }
967
+ onOpenChange: setOpen
913
968
  }
914
969
  );
915
970
  };
916
971
  const DropdownMenu = ({
917
972
  direction = "ltr",
973
+ open,
918
974
  ...restProps
919
- }) => /* @__PURE__ */ jsx(DropdownProvider, { direction, children: /* @__PURE__ */ jsx(BaseTooltipProvider, { children: /* @__PURE__ */ jsx(Root, { direction, ...restProps }) }) });
975
+ }) => /* @__PURE__ */ jsx(DropdownProvider, { direction, open, children: /* @__PURE__ */ jsx(BaseTooltipProvider, { children: /* @__PURE__ */ jsx(Root, { direction, ...restProps }) }) });
920
976
  DropdownMenu.CheckboxItem = CheckboxItem;
921
977
  DropdownMenu.Content = Content;
922
978
  DropdownMenu.Hotkey = Hotkey;