Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make title optional on Menu #174

Merged
merged 9 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/Menu/DrawerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Props extends ComponentPropsWithoutRef<"div"> {
/**
* The menu title.
*/
title: string;
title?: string;
/**
* The menu contents.
*/
Expand Down
25 changes: 14 additions & 11 deletions src/components/Menu/FloatingMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ import styles from "./FloatingMenu.module.css";
import useId from "../../utils/useId";
import { Text } from "../Typography/Text";

interface TitleProps {
title: string;
id: string;
}

const MenuTitle: React.FC<TitleProps> = ({ title, id }) => (
<Text as="h3" id={id} className={styles.title} size="sm" weight="semibold">
{title}
</Text>
);

interface Props extends ComponentPropsWithoutRef<"div"> {
/**
* The menu title.
*/
title: string;
title?: string;
/**
* The CSS class.
*/
Expand All @@ -47,19 +58,11 @@ export const FloatingMenu = forwardRef<HTMLDivElement, Props>(
<div
role="menu"
ref={ref}
aria-labelledby={titleId}
aria-labelledby={title ? titleId : undefined}
dbkr marked this conversation as resolved.
Show resolved Hide resolved
className={classnames(className, styles.menu)}
{...props}
>
<Text
as="h3"
id={titleId}
className={styles.title}
size="sm"
weight="semibold"
>
{title}
</Text>
{title && <MenuTitle title={title} id={titleId} />}
{children}
</div>
);
Expand Down
10 changes: 8 additions & 2 deletions src/components/Menu/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const Template: StoryFn<typeof MenuComponent> = (args) => {
return (
<MenuComponent
{...args}
title="Settings"
open={open}
onOpenChange={setOpen}
trigger={<Button>Open menu</Button>}
Expand All @@ -65,4 +64,11 @@ const Template: StoryFn<typeof MenuComponent> = (args) => {
};

export const Menu = Template.bind({});
Menu.args = {};
Menu.args = {
title: "Today's Menu",
};

export const WithoutTitle = Template.bind({});
WithoutTitle.args = {
title: undefined,
};
14 changes: 14 additions & 0 deletions src/components/Menu/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,18 @@ describe("Menu", () => {
expect(onOpenChange).not.toHaveBeenCalledWith(false);
});
});

it("renders without title if none supplied", async () => {
render(
<Menu
open={true}
onOpenChange={vi.fn()}
trigger={<Button>Open menu</Button>}
>
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
</Menu>,
);

expect(screen.queryByRole("heading")).toBe(null);
});
});
2 changes: 1 addition & 1 deletion src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface Props {
/**
* The menu title.
*/
title: string;
title?: string;
/**
* Whether the menu is open.
*/
Expand Down
Loading