From 04e346eb6970331500f4f5413841f6e50cf5e691 Mon Sep 17 00:00:00 2001 From: Arno V Date: Sat, 27 Apr 2024 10:58:30 -0400 Subject: [PATCH] better trigger type --- .../src/components/Menu/Menu.tsx | 25 +++++++++++++------ .../src/components/Menu/MenuTypes.d.ts | 3 ++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/ui-components/src/components/Menu/Menu.tsx b/packages/ui-components/src/components/Menu/Menu.tsx index 92bc7caf..3d8f0ce9 100644 --- a/packages/ui-components/src/components/Menu/Menu.tsx +++ b/packages/ui-components/src/components/Menu/Menu.tsx @@ -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 @@ -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, diff --git a/packages/ui-components/src/components/Menu/MenuTypes.d.ts b/packages/ui-components/src/components/Menu/MenuTypes.d.ts index 32f5be73..cdceeb0b 100644 --- a/packages/ui-components/src/components/Menu/MenuTypes.d.ts +++ b/packages/ui-components/src/components/Menu/MenuTypes.d.ts @@ -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. */