Skip to content

Commit 7823fea

Browse files
committed
fix(menu): fixed DropdownMenu not being able to provide style and className to Menu
Closes #989
1 parent 6b5bb41 commit 7823fea

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

packages/menu/src/DropdownMenu.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef } from "react";
1+
import React, { CSSProperties, forwardRef } from "react";
22
import { useIcon } from "@react-md/icon";
33
import { RenderConditionalPortalProps } from "@react-md/portal";
44

@@ -38,6 +38,16 @@ export interface BaseDropdownMenuProps
3838
*/
3939
menuLabelledBy?: string;
4040

41+
/**
42+
* An optional style object to pass to the `menuRenderer`/`Menu` component.
43+
*/
44+
menuStyle?: CSSProperties;
45+
46+
/**
47+
* An optional className to pass to the `menuRenderer`/`Menu` component.
48+
*/
49+
menuClassName?: string;
50+
4151
/**
4252
* A custom menu renderer to use. This defaults to just rendering the `Menu`
4353
* component with the base required props and a generated id from the button
@@ -82,6 +92,8 @@ export const DropdownMenu = forwardRef<HTMLButtonElement, DropdownMenuProps>(
8292
anchor,
8393
menuLabel,
8494
menuLabelledBy,
95+
menuStyle,
96+
menuClassName,
8597
menuRenderer = defaultMenuRenderer,
8698
items,
8799
itemRenderer = defaultMenuItemRenderer,
@@ -143,6 +155,8 @@ export const DropdownMenu = forwardRef<HTMLButtonElement, DropdownMenuProps>(
143155
"aria-labelledby": labelledBy as string,
144156
id: `${id}-menu`,
145157
controlId: id,
158+
style: menuStyle,
159+
className: menuClassName,
146160
anchor,
147161
positionOptions,
148162
closeOnScroll,

packages/menu/src/Menu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export const Menu = forwardRef<HTMLDivElement, MenuProps>(function Menu(
150150
role = "menu",
151151
tabIndex = -1,
152152
controlId,
153+
style: propStyle,
153154
className,
154155
visible,
155156
onRequestClose,
@@ -263,7 +264,7 @@ export const Menu = forwardRef<HTMLDivElement, MenuProps>(function Menu(
263264
ref={ref}
264265
role={role}
265266
tabIndex={tabIndex}
266-
style={style}
267+
style={{ ...propStyle, ...style }}
267268
className={cn(block({ horizontal }), className)}
268269
onClick={onClick}
269270
onKeyDown={onKeyDown}

packages/menu/src/__tests__/DropdownMenu.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react/jsx-key */
2-
import React from "react";
2+
import React, { CSSProperties } from "react";
33
import { fireEvent, render } from "@testing-library/react";
44

55
import { DropdownMenu } from "../DropdownMenu";
@@ -56,4 +56,24 @@ describe("DropdownMenu", () => {
5656
rerender(<DropdownMenu {...PROPS} items={items4} />);
5757
expect(container).toMatchSnapshot();
5858
});
59+
60+
it("should pass the menuStyle and menuClassName props to the menu correctly", () => {
61+
const menuStyle: CSSProperties = { color: "red" };
62+
const menuClassName = "my-custom-class-name";
63+
const { getByRole } = render(
64+
<DropdownMenu
65+
{...PROPS}
66+
menuStyle={menuStyle}
67+
menuClassName={menuClassName}
68+
items={["Item 1", "Item 2", "Item 3"]}
69+
/>
70+
);
71+
72+
const button = getByRole("button");
73+
fireEvent.click(button);
74+
75+
const menu = getByRole("menu");
76+
expect(menu.style.color).toBe(menuStyle.color);
77+
expect(menu.className).toContain(menuClassName);
78+
});
5979
});

packages/menu/src/defaultMenuRenderer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactElement, ReactNode } from "react";
1+
import React, { CSSProperties, ReactElement, ReactNode } from "react";
22
import { List } from "@react-md/list";
33
import { RenderConditionalPortalProps } from "@react-md/portal";
44
import { LabelRequiredForA11y } from "@react-md/utils";
@@ -33,6 +33,8 @@ export interface AllInjectedMenuProps
3333
RenderConditionalPortalProps {
3434
"aria-label"?: string;
3535
"aria-labelledby"?: string;
36+
style?: CSSProperties;
37+
className?: string;
3638
onClick?: React.MouseEventHandler<HTMLDivElement>;
3739
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
3840
}

0 commit comments

Comments
 (0)