@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/module.js CHANGED
@@ -14,6 +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 { mergeProps } from '@react-aria/utils';
18
+ import { usePress } from '@mirohq/design-system-use-press';
17
19
  import { styles as styles$1, isIconComponent } from '@mirohq/design-system-base-icon';
18
20
 
19
21
  const Context$1 = createContext({
@@ -459,16 +461,25 @@ const ScrollableContent = ({
459
461
  const DropdownContext = createContext({});
460
462
  const DropdownProvider = ({
461
463
  children,
464
+ open: rootOpen,
462
465
  ...restProps
463
- }) => /* @__PURE__ */ jsx(
464
- DropdownContext.Provider,
465
- {
466
- value: {
467
- ...restProps
468
- },
469
- children
470
- }
471
- );
466
+ }) => {
467
+ const [open, setOpen] = useState(rootOpen);
468
+ const triggerRef = useRef(null);
469
+ return /* @__PURE__ */ jsx(
470
+ DropdownContext.Provider,
471
+ {
472
+ value: {
473
+ ...restProps,
474
+ rootOpen,
475
+ open,
476
+ setOpen,
477
+ triggerRef
478
+ },
479
+ children
480
+ }
481
+ );
482
+ };
472
483
  const useDropdownContext = () => useContext(DropdownContext);
473
484
 
474
485
  const Content = React.forwardRef(
@@ -485,10 +496,11 @@ const Content = React.forwardRef(
485
496
  containerSpacing = "medium",
486
497
  overflow = "visible",
487
498
  maxHeight,
499
+ onInteractOutside,
488
500
  children,
489
501
  ...restProps
490
502
  }, forwardRef) => {
491
- const { direction } = useDropdownContext();
503
+ const { direction, triggerRef } = useDropdownContext();
492
504
  const preventScrollOnFocus = (ref) => {
493
505
  if (ref === null || "_autoScrollPrevented" in ref.focus) {
494
506
  return;
@@ -521,6 +533,12 @@ const Content = React.forwardRef(
521
533
  collisionPadding,
522
534
  sticky,
523
535
  hideWhenDetached,
536
+ onInteractOutside: (e) => {
537
+ if (e.target === triggerRef.current) {
538
+ e.preventDefault();
539
+ }
540
+ onInteractOutside == null ? void 0 : onInteractOutside(e);
541
+ },
524
542
  children: /* @__PURE__ */ jsx(
525
543
  ScrollableContent,
526
544
  {
@@ -726,17 +744,52 @@ const StyledTrigger = styled(RadixDropdownMenu.Trigger, {
726
744
  }
727
745
  });
728
746
 
729
- const Trigger = React.forwardRef(({ asChild = false, onClick, ...restProps }, forwardRef) => {
747
+ const Trigger = React.forwardRef(({ asChild = false, ...restProps }, forwardRef) => {
730
748
  const ref = useRef(null);
749
+ const { setOpen, rootOpen, triggerRef } = useDropdownContext();
750
+ const { pressProps } = usePress({
751
+ onPress: () => {
752
+ if (rootOpen === void 0) {
753
+ setOpen((open) => !booleanify(open));
754
+ }
755
+ }
756
+ });
757
+ const elementProps = mergeProps(restProps, pressProps);
758
+ const onClickHandler = useCallback(
759
+ (e) => {
760
+ var _a, _b;
761
+ if ((e == null ? void 0 : e.nativeEvent) === void 0) {
762
+ console.error(
763
+ "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))
764
+ );
765
+ (_a = restProps.onClick) == null ? void 0 : _a.call(restProps, e);
766
+ return;
767
+ }
768
+ handleVirtualClick(e, ref);
769
+ (_b = elementProps.onClick) == null ? void 0 : _b.call(elementProps, e);
770
+ },
771
+ [restProps, elementProps]
772
+ );
731
773
  return /* @__PURE__ */ jsx(
732
774
  StyledTrigger,
733
775
  {
734
- ...restProps,
735
- onClick: (e) => {
736
- handleVirtualClick(e, ref);
737
- onClick == null ? void 0 : onClick(e);
776
+ ...elementProps,
777
+ onPointerDown: (e) => {
778
+ var _a;
779
+ if (process.env.NODE_ENV === "test") {
780
+ onClickHandler(e);
781
+ }
782
+ (_a = elementProps.onPointerDown) == null ? void 0 : _a.call(elementProps, e);
783
+ },
784
+ onClick: onClickHandler,
785
+ onKeyDown: (e) => {
786
+ var _a;
787
+ if (!/^(Arrow|Tab|Escape)/.test(e.key)) {
788
+ e.preventDefault();
789
+ }
790
+ (_a = elementProps.onKeyDown) == null ? void 0 : _a.call(elementProps, e);
738
791
  },
739
- ref: mergeRefs([ref, forwardRef]),
792
+ ref: mergeRefs([ref, triggerRef, forwardRef]),
740
793
  unstyled: !asChild,
741
794
  asChild
742
795
  }
@@ -891,36 +944,48 @@ const Root = ({
891
944
  defaultOpen = false,
892
945
  direction,
893
946
  interactOutside = false,
894
- open,
895
947
  onOpen,
896
948
  onClose,
897
949
  ...restProps
898
950
  }) => {
899
951
  const { ignoreNextTooltip } = useBaseTooltipContext();
952
+ const { rootOpen, open, setOpen } = useDropdownContext();
900
953
  const prevOpen = usePrevious(open);
901
954
  useEffect(() => {
902
- if (prevOpen === true && open === false) {
903
- ignoreNextTooltip();
955
+ if (prevOpen !== open) {
956
+ if (open === true) {
957
+ onOpen == null ? void 0 : onOpen();
958
+ } else {
959
+ if (prevOpen === true) {
960
+ ignoreNextTooltip();
961
+ }
962
+ onClose == null ? void 0 : onClose();
963
+ }
904
964
  }
905
- }, [ignoreNextTooltip, open, prevOpen]);
965
+ }, [open, prevOpen, onOpen, onClose, ignoreNextTooltip]);
906
966
  return /* @__PURE__ */ jsx(
907
967
  RadixDropdownMenu.Root,
908
968
  {
909
969
  ...restProps,
910
970
  dir: direction,
911
971
  modal: interactOutside,
912
- open,
972
+ open: (
973
+ // use the root open state if it is defined
974
+ rootOpen != null ? rootOpen : defaultOpen && (prevOpen === void 0 || prevOpen === open) ? (
975
+ // only use defaultOpen in the first render then use the context open state
976
+ // 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
977
+ void 0
978
+ ) : (
979
+ // otherwise, use the user open state
980
+ open
981
+ )
982
+ ),
913
983
  defaultOpen,
914
- onOpenChange: (newOpen) => {
915
- if (!newOpen && open === void 0) {
916
- ignoreNextTooltip();
917
- }
918
- newOpen ? onOpen == null ? void 0 : onOpen() : onClose == null ? void 0 : onClose();
919
- }
984
+ onOpenChange: setOpen
920
985
  }
921
986
  );
922
987
  };
923
- const DropdownMenu = ({ direction = "ltr", ...restProps }) => /* @__PURE__ */ jsx(DropdownProvider, { direction, children: /* @__PURE__ */ jsx(BaseTooltipProvider, { children: /* @__PURE__ */ jsx(Root, { direction, ...restProps }) }) });
988
+ const DropdownMenu = ({ direction = "ltr", open, ...restProps }) => /* @__PURE__ */ jsx(DropdownProvider, { direction, open, children: /* @__PURE__ */ jsx(BaseTooltipProvider, { children: /* @__PURE__ */ jsx(Root, { direction, ...restProps }) }) });
924
989
  DropdownMenu.CheckboxItem = CheckboxItem;
925
990
  DropdownMenu.Content = Content;
926
991
  DropdownMenu.Hotkey = Hotkey;