Skip to content

Commit

Permalink
better trigger type
Browse files Browse the repository at this point in the history
  • Loading branch information
aversini committed Apr 27, 2024
1 parent 76c3cee commit 04e346e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
25 changes: 17 additions & 8 deletions packages/ui-components/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ import React, {
import { MenuContext } from "./MenuContext";
import type { MenuProps } from "./MenuTypes";

const getDisplayName = (element: React.ReactNode): string => {
if (typeof element === "string") {
return element;
}
if (typeof element === "object" && element !== null && "type" in element) {
const type = element.type as any;
if (typeof type === "function" || typeof type === "object") {
return type.displayName || type.name || "Component";
}
}
return "Element";
};

export const MenuComponent = forwardRef<
HTMLButtonElement,
MenuProps & React.HTMLProps<HTMLButtonElement>
Expand Down Expand Up @@ -97,19 +110,15 @@ export const MenuComponent = forwardRef<
const { getReferenceProps, getFloatingProps, getItemProps } =
useInteractions([click, role, dismiss, listNavigation, typeahead]);

let isKnownButton = false;
if (
trigger?.type?.displayName === "Button" ||
trigger?.type?.displayName === "ButtonIcon"
) {
isKnownButton = true;
}
const noInternalClick =
getDisplayName(trigger) === "Button" ||
getDisplayName(trigger) === "ButtonIcon";

const triggerElement = React.cloneElement(trigger as React.ReactElement, {
mode,
focusMode,
spacing,
noInternalClick: isKnownButton,
noInternalClick,
"aria-label": label,
"data-open": isOpen ? "" : undefined,
"data-focus-inside": hasFocusInside ? "" : undefined,
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-components/src/components/Menu/MenuTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Placement } from "@floating-ui/react";
import type { SpacingProps } from "@versini/ui-private/dist/utilities";
import React from "react";

export type MenuProps = {
/**
* The component to use to open the menu, e.g. a ButtonIcon, a Button, etc.
*/
trigger: any;
trigger: React.ReactNode;
/**
* The children to render.
*/
Expand Down

0 comments on commit 04e346e

Please sign in to comment.