@mirohq/design-system-dropdown-menu 5.3.1 → 5.3.2-dropdown-on-pointerup.4

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
@@ -15,6 +15,8 @@ var designSystemUseLayoutEffect = require('@mirohq/design-system-use-layout-effe
15
15
  var designSystemUseAriaDisabled = require('@mirohq/design-system-use-aria-disabled');
16
16
  var designSystemScrollArea = require('@mirohq/design-system-scroll-area');
17
17
  var designSystemBaseSwitch = require('@mirohq/design-system-base-switch');
18
+ var utils = require('@react-aria/utils');
19
+ var designSystemUsePress = require('@mirohq/design-system-use-press');
18
20
  var designSystemBaseIcon = require('@mirohq/design-system-base-icon');
19
21
 
20
22
  function _interopNamespaceDefault(e) {
@@ -479,16 +481,25 @@ const ScrollableContent = ({
479
481
  const DropdownContext = React.createContext({});
480
482
  const DropdownProvider = ({
481
483
  children,
484
+ open: rootOpen,
482
485
  ...restProps
483
- }) => /* @__PURE__ */ jsxRuntime.jsx(
484
- DropdownContext.Provider,
485
- {
486
- value: {
487
- ...restProps
488
- },
489
- children
490
- }
491
- );
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
+ };
492
503
  const useDropdownContext = () => React.useContext(DropdownContext);
493
504
 
494
505
  const Content = React.forwardRef(
@@ -505,10 +516,11 @@ const Content = React.forwardRef(
505
516
  containerSpacing = "medium",
506
517
  overflow = "visible",
507
518
  maxHeight,
519
+ onInteractOutside,
508
520
  children,
509
521
  ...restProps
510
522
  }, forwardRef) => {
511
- const { direction } = useDropdownContext();
523
+ const { direction, triggerRef } = useDropdownContext();
512
524
  const preventScrollOnFocus = (ref) => {
513
525
  if (ref === null || "_autoScrollPrevented" in ref.focus) {
514
526
  return;
@@ -541,6 +553,12 @@ const Content = React.forwardRef(
541
553
  collisionPadding,
542
554
  sticky,
543
555
  hideWhenDetached,
556
+ onInteractOutside: (e) => {
557
+ if (e.target === triggerRef.current) {
558
+ e.preventDefault();
559
+ }
560
+ onInteractOutside == null ? void 0 : onInteractOutside(e);
561
+ },
544
562
  children: /* @__PURE__ */ jsxRuntime.jsx(
545
563
  ScrollableContent,
546
564
  {
@@ -746,17 +764,52 @@ const StyledTrigger = designSystemStitches.styled(RadixDropdownMenu__namespace.T
746
764
  }
747
765
  });
748
766
 
749
- const Trigger = React.forwardRef(({ asChild = false, onClick, ...restProps }, forwardRef) => {
767
+ const Trigger = React.forwardRef(({ asChild = false, ...restProps }, forwardRef) => {
750
768
  const ref = React.useRef(null);
769
+ const { setOpen, rootOpen, triggerRef } = useDropdownContext();
770
+ const { pressProps } = designSystemUsePress.usePress({
771
+ onPress: () => {
772
+ if (rootOpen === void 0) {
773
+ setOpen((open) => !designSystemUtils.booleanify(open));
774
+ }
775
+ }
776
+ });
777
+ const elementProps = utils.mergeProps(restProps, pressProps);
778
+ const onClickHandler = React.useCallback(
779
+ (e) => {
780
+ var _a, _b;
781
+ if ((e == null ? void 0 : e.nativeEvent) === void 0) {
782
+ console.error(
783
+ "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))
784
+ );
785
+ (_a = restProps.onClick) == null ? void 0 : _a.call(restProps, e);
786
+ return;
787
+ }
788
+ designSystemUtils.handleVirtualClick(e, ref);
789
+ (_b = elementProps.onClick) == null ? void 0 : _b.call(elementProps, e);
790
+ },
791
+ [restProps, elementProps]
792
+ );
751
793
  return /* @__PURE__ */ jsxRuntime.jsx(
752
794
  StyledTrigger,
753
795
  {
754
- ...restProps,
755
- onClick: (e) => {
756
- designSystemUtils.handleVirtualClick(e, ref);
757
- onClick == null ? void 0 : onClick(e);
796
+ ...elementProps,
797
+ onPointerDown: (e) => {
798
+ var _a;
799
+ if (process.env.NODE_ENV === "test") {
800
+ onClickHandler(e);
801
+ }
802
+ (_a = elementProps.onPointerDown) == null ? void 0 : _a.call(elementProps, e);
803
+ },
804
+ onClick: onClickHandler,
805
+ onKeyDown: (e) => {
806
+ var _a;
807
+ if (!/^(Arrow|Tab|Escape)/.test(e.key)) {
808
+ e.preventDefault();
809
+ }
810
+ (_a = elementProps.onKeyDown) == null ? void 0 : _a.call(elementProps, e);
758
811
  },
759
- ref: designSystemUtils.mergeRefs([ref, forwardRef]),
812
+ ref: designSystemUtils.mergeRefs([ref, triggerRef, forwardRef]),
760
813
  unstyled: !asChild,
761
814
  asChild
762
815
  }
@@ -911,36 +964,48 @@ const Root = ({
911
964
  defaultOpen = false,
912
965
  direction,
913
966
  interactOutside = false,
914
- open,
915
967
  onOpen,
916
968
  onClose,
917
969
  ...restProps
918
970
  }) => {
919
971
  const { ignoreNextTooltip } = designSystemBaseTooltip.useBaseTooltipContext();
972
+ const { rootOpen, open, setOpen } = useDropdownContext();
920
973
  const prevOpen = designSystemUsePrevious.usePrevious(open);
921
974
  React.useEffect(() => {
922
- if (prevOpen === true && open === false) {
923
- ignoreNextTooltip();
975
+ if (prevOpen !== open) {
976
+ if (open === true) {
977
+ onOpen == null ? void 0 : onOpen();
978
+ } else {
979
+ if (prevOpen === true) {
980
+ ignoreNextTooltip();
981
+ }
982
+ onClose == null ? void 0 : onClose();
983
+ }
924
984
  }
925
- }, [ignoreNextTooltip, open, prevOpen]);
985
+ }, [open, prevOpen, onOpen, onClose, ignoreNextTooltip]);
926
986
  return /* @__PURE__ */ jsxRuntime.jsx(
927
987
  RadixDropdownMenu__namespace.Root,
928
988
  {
929
989
  ...restProps,
930
990
  dir: direction,
931
991
  modal: interactOutside,
932
- open,
992
+ open: (
993
+ // use the root open state if it is defined
994
+ rootOpen != null ? rootOpen : defaultOpen && (prevOpen === void 0 || prevOpen === open) ? (
995
+ // only use defaultOpen in the first render then use the context open state
996
+ // 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
997
+ void 0
998
+ ) : (
999
+ // otherwise, use the user open state
1000
+ open
1001
+ )
1002
+ ),
933
1003
  defaultOpen,
934
- onOpenChange: (newOpen) => {
935
- if (!newOpen && open === void 0) {
936
- ignoreNextTooltip();
937
- }
938
- newOpen ? onOpen == null ? void 0 : onOpen() : onClose == null ? void 0 : onClose();
939
- }
1004
+ onOpenChange: setOpen
940
1005
  }
941
1006
  );
942
1007
  };
943
- const DropdownMenu = ({ direction = "ltr", ...restProps }) => /* @__PURE__ */ jsxRuntime.jsx(DropdownProvider, { direction, children: /* @__PURE__ */ jsxRuntime.jsx(designSystemBaseTooltip.BaseTooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(Root, { direction, ...restProps }) }) });
1008
+ const DropdownMenu = ({ direction = "ltr", open, ...restProps }) => /* @__PURE__ */ jsxRuntime.jsx(DropdownProvider, { direction, open, children: /* @__PURE__ */ jsxRuntime.jsx(designSystemBaseTooltip.BaseTooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(Root, { direction, ...restProps }) }) });
944
1009
  DropdownMenu.CheckboxItem = CheckboxItem;
945
1010
  DropdownMenu.Content = Content;
946
1011
  DropdownMenu.Hotkey = Hotkey;