Skip to content

Add Menu component #517

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* The component implements the Chevron Right icon from Font Awesome: https://fontawesome.com/icons/chevron-right */
export default function SvgChevronRightIcon({ id = "chevron-right-icon" }) {
return (
<svg
id={id}
width="20"
height="20"
viewBox="0 0 320 512"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M310.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-192 192c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L242.7 256 73.4 86.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l192 192z"
fill="currentColor"
/>
</svg>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./SvgChevronRightIcon";
28 changes: 28 additions & 0 deletions packages/react-components/src/components/Menu/Menu.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.bcds-react-aria-Menu {
display: flex;
flex-direction: column;
gap: var(--layout-margin-small);
}

.bcds-react-aria-Popover {
background-color: var(--surface-color-forms-default);
border: 1px solid var(--surface-color-border-default);
border-radius: var(--layout-border-radius-medium);
box-shadow: var(--surface-shadow-medium);
box-sizing: border-box;
padding: var(--layout-padding-small) var(--layout-padding-small);
width: var(
--trigger-width
); /* Variable provided by Menu component: https://react-spectrum.adobe.com/react-aria/Menu.html#popover-1 */
}

/* Sections */
.bcds-react-aria-Menu .react-aria-Header {
font: var(--typography-bold-small-body);
}

.bcds-react-aria-Menu .react-aria-MenuSection {
display: flex;
flex-direction: column;
gap: var(--layout-margin-small);
}
26 changes: 26 additions & 0 deletions packages/react-components/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
MenuTrigger,
Menu as ReactAriaMenu,
MenuProps as ReactAriaMenuProps,
MenuSection,
Header as MenuSectionHeader,
SubmenuTrigger,
} from "react-aria-components";

import Popover from "../Popover";
import "./Menu.css";

export default function Menu<T extends object>({
children,
...props
}: ReactAriaMenuProps<T>) {
return (
<Popover>
<ReactAriaMenu className="bcds-react-aria-Menu" {...props}>
{children}
</ReactAriaMenu>
</Popover>
);
}

export { MenuTrigger, SubmenuTrigger, MenuSection, MenuSectionHeader };
8 changes: 8 additions & 0 deletions packages/react-components/src/components/Menu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
default,
MenuTrigger,
SubmenuTrigger,
MenuSection,
MenuSectionHeader,
} from "./Menu";
export type { MenuProps } from "react-aria-components";
52 changes: 52 additions & 0 deletions packages/react-components/src/components/MenuItem/MenuItem.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.bcds-react-aria-Menu-Item {
color: var(--typography-color-secondary);
display: inline-flex;
gap: var(--layout-margin-xsmall);
cursor: pointer;
}

/* Link styling */
a.bcds-react-aria-Menu-Item {
color: var(--typography-color-link);
text-decoration: underline;
text-underline-offset: var(--layout-padding-hair);
}

a.bcds-react-aria-Menu-Item[data-hovered] {
color: var(--surface-color-border-active);
}

/* Sizing */
.bcds-react-aria-Menu-Item.small {
font: var(--typography-regular-small-body);
}

.bcds-react-aria-Menu-Item.medium {
font: var(--typography-regular-body);
}

/* Icon displayed when an item has a submenu */
.bcds-react-aria-Menu-Item > svg {
width: var(--icons-size-xsmall);
height: var(--icons-size-xsmall);
padding: var(--layout-padding-hair);
align-self: center;
}

/* States */

.bcds-react-aria-Menu-Item[data-focus-visible] {
outline: solid var(--layout-border-width-medium)
var(--surface-color-border-active);
outline-offset: var(--layout-margin-xsmall);
border-radius: var(--layout-border-radius-small);
}

.bcds-react-aria-Menu-Item[data-hovered] {
color: var(--surface-color-border-active);
}

.bcds-react-aria-Menu-Item[data-disabled] {
color: var(--typography-color-disabled);
cursor: not-allowed;
}
43 changes: 43 additions & 0 deletions packages/react-components/src/components/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
MenuItem as ReactAriaMenuItem,
MenuItemProps as ReactAriaMenuItemProps,
MenuItemRenderProps,
} from "react-aria-components";
import { ReactNode } from "react";
import "./MenuItem.css";
import SvgChevronRightIcon from "../Icons/SvgChevronRightIcon";

export interface MenuItemProps extends ReactAriaMenuItemProps {
size?: "small" | "medium";
children?:
| ReactNode
| ((
props: MenuItemRenderProps & { defaultChildren: ReactNode }
) => ReactNode);
}

export default function MenuItem({ size = "small", ...props }: MenuItemProps) {
const textValue =
props.textValue ||
(typeof props.children === "string" ? props.children : undefined);

return (
<ReactAriaMenuItem
{...props}
textValue={textValue}
className={`bcds-react-aria-Menu-Item ${size}`}
>
{(renderProps: MenuItemRenderProps & { defaultChildren: ReactNode }) => {
if (typeof props.children === "function") {
return props.children(renderProps);
}
return (
<>
{props.children}
{renderProps.hasSubmenu && <SvgChevronRightIcon />}
</>
);
}}
</ReactAriaMenuItem>
);
}
2 changes: 2 additions & 0 deletions packages/react-components/src/components/MenuItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from "./MenuItem";
export type { MenuItemProps } from "./MenuItem";
12 changes: 12 additions & 0 deletions packages/react-components/src/components/Popover/Popover.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.bcds-react-aria-Popover {
background-color: var(--surface-color-forms-default);
border: var(--layout-border-width-small) solid
var(--surface-color-border-default);
border-radius: var(--layout-border-radius-medium);
box-shadow: var(--surface-shadow-medium);
box-sizing: border-box;
padding: var(--layout-padding-xsmall) var(--layout-padding-xsmall);
width: var(
--trigger-width
); /* Variable provided by Menu component: https://react-spectrum.adobe.com/react-aria/Menu.html#popover-1 */
}
12 changes: 12 additions & 0 deletions packages/react-components/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Popover as ReactAriaPopover } from "react-aria-components";
import type { PopoverProps } from "react-aria-components";

import "./Popover.css";

export default function Popover(props: PopoverProps) {
return (
<ReactAriaPopover className="bcds-react-aria-Popover" {...props}>
{props.children}
</ReactAriaPopover>
);
}
2 changes: 2 additions & 0 deletions packages/react-components/src/components/Popover/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from "./Popover";
export type { PopoverProps } from "react-aria-components";
12 changes: 0 additions & 12 deletions packages/react-components/src/components/Select/Select.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@
}

/* Dropdown menu panel */
.bcds-react-aria-Select--Popover {
background-color: var(--surface-color-forms-default);
border: 1px solid var(--surface-color-border-default);
border-radius: var(--layout-border-radius-medium);
box-shadow: var(--surface-shadow-medium);
box-sizing: border-box;
overflow-y: auto;
padding: var(--layout-padding-hair) var(--layout-padding-xsmall);
width: var(
--trigger-width
); /* Variable provided by Select component https://react-spectrum.adobe.com/react-aria/Select.html#popover-1 */
}
.bcds-react-aria-Select--ListBox[data-focused] {
outline: none;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-components/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
ListBoxItem,
ListBoxItemProps as ReactAriaListBoxItemProps,
ListBoxSection,
Popover,
Select as ReactAriaSelect,
SelectProps as ReactAriaSelectProps,
SelectValue,
Text,
ValidationResult,
} from "react-aria-components";

import Popover from "../Popover";
import SvgExclamationIcon from "../Icons/SvgExclamationIcon";
import SvgChevronUpIcon from "../Icons/SvgChevronUpIcon";
import SvgChevronDownIcon from "../Icons/SvgChevronDownIcon";
Expand Down Expand Up @@ -108,7 +108,7 @@ export default function Select<T extends object>({
<FieldError className="bcds-react-aria-Select--Error">
{errorMessage}
</FieldError>
<Popover className="bcds-react-aria-Select--Popover" offset={4}>
<Popover offset={4}>
<ListBox
className="bcds-react-aria-Select--ListBox"
// This ternary statement is used to mock the data for `sections`
Expand Down
10 changes: 10 additions & 0 deletions packages/react-components/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export { default as Footer, FooterLinks } from "./Footer";
export { default as Form } from "./Form";
export { default as Header } from "./Header";
export { default as InlineAlert } from "./InlineAlert";
export {
default as Menu,
MenuTrigger,
SubmenuTrigger,
MenuSection,
MenuSectionHeader,
} from "./Menu";
export { default as MenuItem } from "./MenuItem";
export { default as Modal } from "./Modal";
export { default as Popover } from "./Popover";
export { default as Radio } from "./Radio";
export { default as RadioGroup } from "./RadioGroup";
export { default as Select } from "./Select";
Expand All @@ -28,6 +37,7 @@ export { default as SvgExclamationIcon } from "./Icons/SvgExclamationIcon";
export { default as SvgExclamationCircleIcon } from "./Icons/SvgExclamationCircleIcon";
export { default as SvgChevronUpIcon } from "./Icons/SvgChevronUpIcon";
export { default as SvgChevronDownIcon } from "./Icons/SvgChevronDownIcon";
export { default as SvgChevronRightIcon } from "./Icons/SvgChevronRightIcon";
export { default as SvgCloseIcon } from "./Icons/SvgCloseIcon";
export { default as SvgInfoIcon } from "./Icons/SvgInfoIcon";
export { default as SvgTooltipArrowUp } from "./Icons/SvgTooltipArrowUp";
Expand Down
Loading
Loading