Skip to content

Commit

Permalink
Create menu component (#100)
Browse files Browse the repository at this point in the history
* Create menu component

This component represents a menu that opens on a button press. Feedback on the API is very welcome - I'm pretty happy with how I managed to condense Radix's tree of menu components into a single component, but we need to make sure that it's intuitive for others too.

* Address review feedback

* Expand a comment

* Replace MenuDivider with Separator
  • Loading branch information
robintown authored Nov 23, 2023
1 parent 1e0aafe commit 7c9be5f
Show file tree
Hide file tree
Showing 32 changed files with 642 additions and 159 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@
"vitest": "^0.34.4"
},
"dependencies": {
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-form": "^0.0.3",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.6",
"classnames": "^2.3.2",
"graphemer": "^1.4.0"
"graphemer": "^1.4.0",
"vaul": "^0.7.0"
},
"peerDependencies": {
"@fontsource/inconsolata": "^5",
Expand Down
14 changes: 9 additions & 5 deletions src/components/Button/Button.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 New Vector Ltd
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,7 +71,8 @@ limitations under the License.
}
}

.button[data-kind="primary"]:active {
.button[data-kind="primary"]:active,
.button[data-kind="primary"][aria-expanded="true"] {
background: var(--cpd-color-bg-action-primary-pressed);
}

Expand All @@ -93,7 +94,8 @@ limitations under the License.
}
}

.button[data-kind="secondary"]:active {
.button[data-kind="secondary"]:active,
.button[data-kind="secondary"][aria-expanded="true"] {
border-color: var(--cpd-color-border-interactive-hovered);
background: var(--cpd-color-bg-subtle-primary);
}
Expand All @@ -117,7 +119,8 @@ limitations under the License.
}
}

.button[data-kind="tertiary"]:active {
.button[data-kind="tertiary"]:active,
.button[data-kind="tertiary"][aria-expanded="true"] {
background: var(--cpd-color-bg-subtle-primary);
}

Expand All @@ -138,7 +141,8 @@ limitations under the License.
}
}

.button[data-kind="destructive"]:active {
.button[data-kind="destructive"]:active,
.button[data-kind="destructive"][aria-expanded="true"] {
border-color: var(--cpd-color-border-critical-hovered);
background: var(--cpd-color-bg-critical-subtle-hovered);
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Menu/DrawerMenu.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ limitations under the License.
contain: paint;
overflow: auto;
scrollbar-width: none;

--cpd-separator-spacing: 0;
--cpd-separator-inset: var(--cpd-space-4x);
}

.body::before {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Menu/DrawerMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import LeaveIcon from "@vector-im/compound-design-tokens/icons/leave.svg";

import { DrawerMenu as DrawerMenuComponent } from "./DrawerMenu";
import drawerStyles from "./DrawerMenu.module.css";
import { MenuItem } from "../MenuItem/MenuItem";
import { MenuDivider } from "../MenuItem/MenuDivider";
import { MenuItem } from "./MenuItem";
import { Separator } from "../Separator/Separator";

export default {
title: "Menu/DrawerMenu",
Expand All @@ -44,7 +44,7 @@ const Template: StoryFn<typeof DrawerMenuComponent> = (args) => (
onSelect={() => {}}
/>
<MenuItem Icon={ChatProblemIcon} label="Feedback" onSelect={() => {}} />
<MenuDivider />
<Separator />
<MenuItem
kind="critical"
Icon={LeaveIcon}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Menu/DrawerMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profil
import LeaveIcon from "@vector-im/compound-design-tokens/icons/leave.svg";

import { DrawerMenu } from "./DrawerMenu";
import { MenuItem } from "../MenuItem/MenuItem";
import { MenuDivider } from "../MenuItem/MenuDivider";
import { MenuItem } from "./MenuItem";
import { Separator } from "../Separator/Separator";

describe("DrawerMenu", () => {
it("renders", () => {
const { asFragment } = render(
<DrawerMenu title="Settings">
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
<MenuDivider />
<Separator />
<MenuItem
kind="critical"
Icon={LeaveIcon}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Menu/DrawerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import React, { ComponentPropsWithoutRef, ReactNode, forwardRef } from "react";
import styles from "./DrawerMenu.module.css";
import { platform } from "../../utils/platform";
import { getPlatform } from "../../utils/platform";
import classNames from "classnames";

interface Props extends ComponentPropsWithoutRef<"div"> {
Expand All @@ -41,7 +41,7 @@ export const DrawerMenu = forwardRef<HTMLDivElement, Props>(
ref={ref}
className={classNames(className, styles.drawer)}
aria-label={title}
data-platform={platform}
data-platform={getPlatform()}
{...props}
role="menu"
>
Expand Down
3 changes: 3 additions & 0 deletions src/components/Menu/FloatingMenu.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ limitations under the License.
flex-direction: column;
gap: var(--cpd-space-1x);
padding-block: var(--cpd-space-5x) var(--cpd-space-4x);

--cpd-separator-spacing: 0;
--cpd-separator-inset: var(--cpd-space-4x);
}

@keyframes slide-in {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Menu/FloatingMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import ChatProblemIcon from "@vector-im/compound-design-tokens/icons/chat-proble
import LeaveIcon from "@vector-im/compound-design-tokens/icons/leave.svg";

import { FloatingMenu as FloatingMenuComponent } from "./FloatingMenu";
import { MenuItem } from "../MenuItem/MenuItem";
import { MenuDivider } from "../MenuItem/MenuDivider";
import { MenuItem } from "./MenuItem";
import { Separator } from "../Separator/Separator";

export default {
title: "Menu/FloatingMenu",
Expand All @@ -42,7 +42,7 @@ const Template: StoryFn<typeof FloatingMenuComponent> = (args) => (
onSelect={() => {}}
/>
<MenuItem Icon={ChatProblemIcon} label="Feedback" onSelect={() => {}} />
<MenuDivider />
<Separator />
<MenuItem
kind="critical"
Icon={LeaveIcon}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Menu/FloatingMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profil
import LeaveIcon from "@vector-im/compound-design-tokens/icons/leave.svg";

import { FloatingMenu } from "./FloatingMenu";
import { MenuItem } from "../MenuItem/MenuItem";
import { MenuDivider } from "../MenuItem/MenuDivider";
import { MenuItem } from "./MenuItem";
import { Separator } from "../Separator/Separator";

describe("FloatingMenu", () => {
it("renders", () => {
const { asFragment } = render(
<FloatingMenu title="Settings">
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
<MenuDivider />
<Separator />
<MenuItem
kind="critical"
Icon={LeaveIcon}
Expand Down
68 changes: 68 additions & 0 deletions src/components/Menu/Menu.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { useState } from "react";
import { Meta, StoryFn } from "@storybook/react";
import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profile.svg";
import NotificationsIcon from "@vector-im/compound-design-tokens/icons/notifications.svg";
import ChatProblemIcon from "@vector-im/compound-design-tokens/icons/chat-problem.svg";
import LeaveIcon from "@vector-im/compound-design-tokens/icons/leave.svg";

import { Menu as MenuComponent } from "./Menu";
import { MenuItem } from "./MenuItem";
import { Separator } from "../Separator/Separator";
import { Button } from "../Button/Button";

export default {
title: "Menu",
component: MenuComponent,
tags: ["autodocs"],
argTypes: {},
args: {},
} as Meta<typeof MenuComponent>;

const Template: StoryFn<typeof MenuComponent> = (args) => {
const [open, setOpen] = useState(true);

return (
<MenuComponent
{...args}
title="Settings"
open={open}
onOpenChange={setOpen}
trigger={<Button>Open menu</Button>}
align="start"
>
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
<MenuItem
Icon={NotificationsIcon}
label="Notifications"
onSelect={() => {}}
/>
<MenuItem Icon={ChatProblemIcon} label="Feedback" onSelect={() => {}} />
<Separator />
<MenuItem
kind="critical"
Icon={LeaveIcon}
label="Sign out"
onSelect={() => {}}
/>
</MenuComponent>
);
};

export const Menu = Template.bind({});
Menu.args = {};
135 changes: 135 additions & 0 deletions src/components/Menu/Menu.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import React from "react";
import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profile.svg";

import { Menu } from "./Menu";
import { MenuItem } from "./MenuItem";
import { Button } from "../Button/Button";
import userEvent from "@testing-library/user-event";
import { getPlatform } from "../../utils/platform";

vi.mock("../../utils/platform", () => ({ getPlatform: vi.fn(() => "other") }));

async function withPlatform(
platform: ReturnType<typeof getPlatform>,
continuation: () => Promise<void>,
) {
const mock = vi.mocked(getPlatform).mockReturnValue(platform);
try {
await continuation();
} finally {
mock.mockRestore();
}
}

describe("Menu", () => {
it("opens", async () => {
const onOpenChange = vi.fn();
render(
<Menu
title="Settings"
open={false}
onOpenChange={onOpenChange}
trigger={<Button>Open menu</Button>}
>
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
</Menu>,
);

expect(screen.queryByRole("menu")).toBe(null);
await userEvent.click(screen.getByRole("button"));
expect(onOpenChange).toHaveBeenLastCalledWith(true);
});

it("closes as a floating menu", async () => {
const onOpenChange = vi.fn();
render(
<Menu
title="Settings"
open={true}
onOpenChange={onOpenChange}
trigger={<Button>Open menu</Button>}
>
<MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} />
</Menu>,
);

// Floating menus have a heading
screen.getByRole("menu");
screen.getByRole("heading", { name: "Settings" });
await userEvent.click(screen.getByRole("menuitem", { name: "Profile" }));
expect(onOpenChange).toHaveBeenLastCalledWith(false);
});

it("closes as a drawer menu", async () => {
// Simulate a touchscreen so that the menu turns into a drawer
await withPlatform("android", async () => {
const onOpenChange = vi.fn();
render(
<Menu
title="Settings"
open={true}
onOpenChange={onOpenChange}
trigger={<Button>Open menu</Button>}
>
<MenuItem
Icon={UserProfileIcon}
label="Profile"
onSelect={() => {}}
/>
</Menu>,
);

// Drawers don't have a heading
screen.getByRole("menu");
expect(screen.queryByRole("heading", { name: "Settings" })).toBe(null);
// Intentionally avoiding userEvent here, because that would trigger a
// callback that calls Element.setPointerCapture, which apparently JSDOM
// doesn't implement
screen.getByRole("menuitem", { name: "Profile" }).click();
expect(onOpenChange).toHaveBeenLastCalledWith(false);
});
});

it("doesn't close if preventDefault is called", async () => {
await withPlatform("ios", async () => {
const onOpenChange = vi.fn();
render(
<Menu
title="Settings"
open={true}
onOpenChange={onOpenChange}
trigger={<Button>Open menu</Button>}
>
<MenuItem
Icon={UserProfileIcon}
label="Profile"
onSelect={(e) => e.preventDefault()}
/>
</Menu>,
);

screen.getByRole("menu");
expect(screen.queryByRole("heading", { name: "Settings" })).toBe(null);
screen.getByRole("menuitem", { name: "Profile" }).click();
expect(onOpenChange).not.toHaveBeenCalledWith(false);
});
});
});
Loading

0 comments on commit 7c9be5f

Please sign in to comment.