@mirohq/design-system-dropdown-menu 4.4.9 → 4.4.10-dropdown-on-pointerup.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/main.js CHANGED
@@ -18,6 +18,7 @@ var designSystemUseAriaDisabled = require('@mirohq/design-system-use-aria-disabl
18
18
  var designSystemScrollArea = require('@mirohq/design-system-scroll-area');
19
19
  var designSystemBaseSwitch = require('@mirohq/design-system-base-switch');
20
20
  var utils = require('@react-aria/utils');
21
+ var designSystemUsePress = require('@mirohq/design-system-use-press');
21
22
  var designSystemBaseIcon = require('@mirohq/design-system-base-icon');
22
23
 
23
24
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -480,16 +481,25 @@ const ScrollableContent = ({
480
481
  const DropdownContext = React.createContext({});
481
482
  const DropdownProvider = ({
482
483
  children,
484
+ open: rootOpen,
483
485
  ...restProps
484
- }) => /* @__PURE__ */ jsxRuntime.jsx(
485
- DropdownContext.Provider,
486
- {
487
- value: {
488
- ...restProps
489
- },
490
- children
491
- }
492
- );
486
+ }) => {
487
+ const [open, setOpen] = React.useState(rootOpen);
488
+ const triggerRef = React.useRef(null);
489
+ return /* @__PURE__ */ jsxRuntime.jsx(
490
+ DropdownContext.Provider,
491
+ {
492
+ value: {
493
+ ...restProps,
494
+ rootOpen,
495
+ open,
496
+ setOpen,
497
+ triggerRef
498
+ },
499
+ children
500
+ }
501
+ );
502
+ };
493
503
  const useDropdownContext = () => React.useContext(DropdownContext);
494
504
 
495
505
  const Content = React__default["default"].forwardRef(
@@ -506,10 +516,11 @@ const Content = React__default["default"].forwardRef(
506
516
  containerSpacing = "medium",
507
517
  overflow = "visible",
508
518
  maxHeight,
519
+ onInteractOutside,
509
520
  children,
510
521
  ...restProps
511
522
  }, forwardRef) => {
512
- const { direction } = useDropdownContext();
523
+ const { direction, triggerRef } = useDropdownContext();
513
524
  return /* @__PURE__ */ jsxRuntime.jsx(ContentProvider, { containerSpacing, children: /* @__PURE__ */ jsxRuntime.jsx(
514
525
  StyledContent,
515
526
  {
@@ -524,6 +535,12 @@ const Content = React__default["default"].forwardRef(
524
535
  collisionPadding,
525
536
  sticky,
526
537
  hideWhenDetached,
538
+ onInteractOutside: (e) => {
539
+ if (e.target === triggerRef.current) {
540
+ e.preventDefault();
541
+ }
542
+ onInteractOutside == null ? void 0 : onInteractOutside(e);
543
+ },
527
544
  children: /* @__PURE__ */ jsxRuntime.jsx(
528
545
  ScrollableContent,
529
546
  {
@@ -729,37 +746,49 @@ const StyledTrigger = designSystemStitches.styled(RadixDropdownMenu__namespace.T
729
746
  }
730
747
  });
731
748
 
732
- const Trigger = React__default["default"].forwardRef(({ asChild = false, onClick, ...restProps }, forwardRef) => {
749
+ const Trigger = React__default["default"].forwardRef(({ asChild = false, ...restProps }, forwardRef) => {
733
750
  const ref = React.useRef(null);
734
- const handleVirtualClick = (e) => {
735
- var _a;
736
- if ((e == null ? void 0 : e.nativeEvent) === void 0) {
737
- console.error(
738
- "DropdownMenu.Trigger onClick expected a MouseEvent but got different one. It usually happens due to a element used asChild with a different onClick interface.\n" + "Event: ".concat(JSON.stringify(e, null, 2))
739
- );
740
- return;
741
- }
742
- const nativeEvent = e.nativeEvent;
743
- if (nativeEvent.mozInputSource === void 0) {
744
- nativeEvent.mozInputSource = null;
745
- }
746
- if (utils.isVirtualClick(e.nativeEvent) && ref.current !== null) {
747
- const pointerDownEvent = new MouseEvent("pointerdown", {
748
- bubbles: true,
749
- cancelable: true
750
- });
751
- (_a = ref.current) == null ? void 0 : _a.dispatchEvent(pointerDownEvent);
751
+ const { setOpen, rootOpen, triggerRef } = useDropdownContext();
752
+ const { pressProps } = designSystemUsePress.usePress({
753
+ onPress: () => {
754
+ if (rootOpen === void 0) {
755
+ setOpen((open) => !designSystemUtils.booleanify(open));
756
+ }
752
757
  }
753
- };
758
+ });
759
+ const elementProps = utils.mergeProps(restProps, pressProps);
754
760
  return /* @__PURE__ */ jsxRuntime.jsx(
755
761
  StyledTrigger,
756
762
  {
757
- ...restProps,
763
+ ...elementProps,
764
+ onPointerDown: (e) => {
765
+ var _a, _b;
766
+ if (process.env.NODE_ENV === "test") {
767
+ e.preventDefault = () => null;
768
+ (_a = restProps.onPointerDown) == null ? void 0 : _a.call(restProps, e);
769
+ return;
770
+ }
771
+ (_b = elementProps.onPointerDown) == null ? void 0 : _b.call(elementProps, e);
772
+ },
758
773
  onClick: (e) => {
759
- handleVirtualClick(e);
760
- onClick == null ? void 0 : onClick(e);
774
+ var _a, _b;
775
+ if ((e == null ? void 0 : e.nativeEvent) === void 0) {
776
+ console.error(
777
+ "DropdownMenu.Trigger onClick expected a MouseEvent but got different one. It usually happens due to a element used asChild with a different onClick interface.\n" + "Event: ".concat(JSON.stringify(e, null, 2))
778
+ );
779
+ (_a = restProps.onClick) == null ? void 0 : _a.call(restProps, e);
780
+ return;
781
+ }
782
+ (_b = elementProps.onClick) == null ? void 0 : _b.call(elementProps, e);
761
783
  },
762
- ref: designSystemUtils.mergeRefs([ref, forwardRef]),
784
+ onKeyDown: (e) => {
785
+ var _a;
786
+ if (!/^(Arrow|Tab|Escape)/.test(e.key)) {
787
+ e.preventDefault();
788
+ }
789
+ (_a = elementProps.onKeyDown) == null ? void 0 : _a.call(elementProps, e);
790
+ },
791
+ ref: designSystemUtils.mergeRefs([ref, triggerRef, forwardRef]),
763
792
  unstyled: !asChild,
764
793
  asChild
765
794
  }
@@ -910,39 +939,52 @@ const Root = ({
910
939
  defaultOpen = false,
911
940
  direction,
912
941
  interactOutside = false,
913
- open,
914
942
  onOpen,
915
943
  onClose,
916
944
  ...restProps
917
945
  }) => {
918
946
  const { ignoreNextTooltip } = designSystemBaseTooltip.useBaseTooltipContext();
947
+ const { rootOpen, open, setOpen } = useDropdownContext();
919
948
  const prevOpen = designSystemUsePrevious.usePrevious(open);
920
949
  React.useEffect(() => {
921
- if (prevOpen === true && open === false) {
922
- ignoreNextTooltip();
950
+ if (prevOpen !== open) {
951
+ if (open === true) {
952
+ onOpen == null ? void 0 : onOpen();
953
+ } else {
954
+ if (prevOpen === true) {
955
+ ignoreNextTooltip();
956
+ }
957
+ onClose == null ? void 0 : onClose();
958
+ }
923
959
  }
924
- }, [ignoreNextTooltip, open, prevOpen]);
960
+ }, [open, prevOpen, onOpen, onClose, ignoreNextTooltip]);
925
961
  return /* @__PURE__ */ jsxRuntime.jsx(
926
962
  RadixDropdownMenu__namespace.Root,
927
963
  {
928
964
  ...restProps,
929
965
  dir: direction,
930
966
  modal: interactOutside,
931
- open,
967
+ open: (
968
+ // use the root open state if it is defined
969
+ rootOpen != null ? rootOpen : defaultOpen && (prevOpen === void 0 || prevOpen === open) ? (
970
+ // only use defaultOpen in the first render then use the context open state
971
+ // 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
972
+ void 0
973
+ ) : (
974
+ // otherwise, use the user open state
975
+ open
976
+ )
977
+ ),
932
978
  defaultOpen,
933
- onOpenChange: (newOpen) => {
934
- if (!newOpen && open === void 0) {
935
- ignoreNextTooltip();
936
- }
937
- newOpen ? onOpen == null ? void 0 : onOpen() : onClose == null ? void 0 : onClose();
938
- }
979
+ onOpenChange: setOpen
939
980
  }
940
981
  );
941
982
  };
942
983
  const DropdownMenu = ({
943
984
  direction = "ltr",
985
+ open,
944
986
  ...restProps
945
- }) => /* @__PURE__ */ jsxRuntime.jsx(DropdownProvider, { direction, children: /* @__PURE__ */ jsxRuntime.jsx(designSystemBaseTooltip.BaseTooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(Root, { direction, ...restProps }) }) });
987
+ }) => /* @__PURE__ */ jsxRuntime.jsx(DropdownProvider, { direction, open, children: /* @__PURE__ */ jsxRuntime.jsx(designSystemBaseTooltip.BaseTooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(Root, { direction, ...restProps }) }) });
946
988
  DropdownMenu.CheckboxItem = CheckboxItem;
947
989
  DropdownMenu.Content = Content;
948
990
  DropdownMenu.Hotkey = Hotkey;