-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
32 changed files
with
642 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.