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

feat: new popoverContent feature for MenuButton, plus UserProfileMenuButton #2414

Merged
merged 6 commits into from
Nov 19, 2024
Merged
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
Expand Up @@ -16,21 +16,22 @@ import {
HTMLAttributes,
memo,
ReactElement,
ReactNode,
useCallback,
useImperativeHandle,
useMemo,
useRef,
} from "react";

import { useButton } from "./ButtonContext";
import type { HtmlProps } from "./HtmlProps";
import { FocusHandle } from "./inputUtils";
import { useButton } from "../Buttons";
import type { HtmlProps } from "../HtmlProps";
import { FocusHandle } from "../inputUtils";
import {
MuiPropsContext,
MuiPropsContextType,
useMuiProps,
} from "./MuiPropsContext";
import { Tooltip } from "./Tooltip";
} from "../MuiPropsContext";
import { Tooltip } from "../Tooltip";

export const buttonSizeValues = ["small", "medium", "large"] as const;
export const buttonTypeValues = ["button", "submit", "reset"] as const;
Expand All @@ -43,7 +44,7 @@ export const buttonVariantValues = [
"floatingAction",
] as const;

export type ButtonProps = {
export type BaseButtonProps = {
/**
* The ref forwarded to the Button
*/
Expand Down Expand Up @@ -85,64 +86,41 @@ export type ButtonProps = {
* The click event handler for the Button
*/
onClick?: MuiButtonProps["onClick"];
} & (
| {
/**
* The icon element to display at the end of the Button
*/
endIcon?: ReactElement;
/**
* The text content of the Button
*/
label: string;
/**
* The icon element to display at the start of the Button
*/
startIcon?: ReactElement;
}
| {
/**
* The icon element to display at the end of the Button
*/
endIcon?: ReactElement;
/**
* The text content of the Button
*/
label?: string | "" | undefined;
/**
* The icon element to display at the start of the Button
*/
startIcon: ReactElement;
}
| {
/**
* The icon element to display at the end of the Button
*/
endIcon: ReactElement;
/**
* The text content of the Button
*/
label?: never;
/**
* The icon element to display at the start of the Button
*/
startIcon?: ReactElement;
}
) &
Pick<
HtmlProps,
| "ariaControls"
| "ariaDescribedBy"
| "ariaExpanded"
| "ariaHasPopup"
| "ariaLabel"
| "ariaLabelledBy"
| "tabIndex"
| "testId"
| "translate"
>;
/**
* The contents of the button. Only available internal to Odyssey here in BaseButton. If set, label is ignored.
*/
children?: ReactNode;
/**
* The icon element to display at the end of the Button
*/
endIcon?: ReactElement;
/**
* The text content of the Button
*/
label?: string;
/**
* The icon element to display at the start of the Button
*/
startIcon?: ReactElement;
};

// These are split and exported separately from the above because wrappers of this (e.g. Button) will
// want to omit children, which they cannot do from the combined union type. Instead, they should
// omit from BaseButtonProps, then union with the AdditionalBaseButtonProps (as seen in Button)
export type AdditionalBaseButtonProps = Pick<
HtmlProps,
| "ariaControls"
| "ariaDescribedBy"
| "ariaExpanded"
| "ariaHasPopup"
| "ariaLabel"
| "ariaLabelledBy"
| "tabIndex"
| "testId"
| "translate"
>;

const Button = ({
const BaseButton = ({
ariaControls,
ariaDescribedBy,
ariaExpanded,
Expand All @@ -156,6 +134,7 @@ const Button = ({
isDisabled,
isFullWidth: isFullWidthProp,
label = "",
children,
onClick,
size = "medium",
startIcon,
Expand All @@ -165,7 +144,7 @@ const Button = ({
translate,
type = "button",
variant: variantProp,
}: ButtonProps) => {
}: BaseButtonProps & AdditionalBaseButtonProps) => {
const muiProps = useMuiProps();

// We're deprecating the "tertiary" variant, so map it to
Expand Down Expand Up @@ -225,7 +204,7 @@ const Button = ({
type={type}
variant={variant}
>
{label}
{children ?? label}
</MuiButton>
);
},
Expand All @@ -242,6 +221,7 @@ const Button = ({
isDisabled,
isFullWidth,
label,
children,
onClick,
size,
startIcon,
Expand All @@ -264,7 +244,7 @@ const Button = ({
return renderButton(muiProps);
};

const MemoizedButton = memo(Button);
MemoizedButton.displayName = "Button";
const MemoizedBaseButton = memo(BaseButton);
MemoizedBaseButton.displayName = "BaseButton";

export { MemoizedButton as Button };
export { MemoizedBaseButton as BaseButton };
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ import {
useState,
ReactNode,
} from "react";
import { Menu as MuiMenu, PopoverOrigin } from "@mui/material";
import {
Menu as MuiMenu,
Popover as MuiPopover,
PopoverOrigin,
} from "@mui/material";

import { Button, buttonSizeValues, buttonVariantValues, useUniqueId } from "./";
import { ChevronDownIcon, MoreIcon } from "./icons.generated";
import { FieldComponentProps } from "./FieldComponentProps";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext";
import { Box, buttonSizeValues, buttonVariantValues, useUniqueId } from "..";
import { BaseButton } from "./BaseButton";
import { ChevronDownIcon, MoreIcon } from "../icons.generated";
import { FieldComponentProps } from "../FieldComponentProps";
import { MenuContext, MenuContextType } from "./MenuContext";
import { NullElement } from "./NullElement";
import type { HtmlProps } from "./HtmlProps";
import { NullElement } from "../NullElement";
import type { HtmlProps } from "../HtmlProps";

export const menuAlignmentValues = ["left", "right"] as const;

export type MenuButtonProps = {
export type BaseMenuButtonProps = {
/**
* The button children for the triggering Button. Only available internal to Odyssey here in BaseMenuButton. If set, buttonLabel is ignored.
*/
buttonChildren?: ReactNode;
/**
* The label on the triggering Button
*/
Expand All @@ -38,14 +48,14 @@ export type MenuButtonProps = {
* The variant of the triggering Button
*/
buttonVariant?: (typeof buttonVariantValues)[number];
/**
* The <MenuItem> components within the Menu.
*/
children: ReactNode | NullElement;
/**
* The end Icon on the trigggering Button
*/
endIcon?: ReactElement;
/**
* Whether to omit the endIcon if not set (rather than use a default value for it based on overflow)
*/
omitEndIcon?: boolean;
/**
* The id of the Button
*/
Expand All @@ -71,7 +81,12 @@ export type MenuButtonProps = {
* The tooltip text for the Button if it's icon-only
*/
tooltipText?: string;
} & Pick<
};

// These are split and exported separately from the above because wrappers of this (e.g. MenuButton) will
// want to omit buttonChildren, which they cannot do from the combined union type. Instead, they should
// omit from BaseMenuButtonProps, then union with the AdditionalBaseMenuButtonProps (as seen in MenuButton)
export type AdditionalBaseMenuButtonProps = Pick<
HtmlProps,
"ariaDescribedBy" | "ariaLabel" | "ariaLabelledBy" | "testId" | "translate"
> &
Expand All @@ -86,26 +101,52 @@ export type MenuButtonProps = {
Partial<Pick<HtmlProps, "ariaLabelledBy">> & {
buttonLabel?: undefined | "";
})
) &
(
| {
/**
* The <MenuItem> components within the Menu.
*/
children: ReactNode | NullElement;
/**
* popoverConten is disallowed if children are present
*/
popoverContent?: never;
}
| {
/**
* children is disallowed if popoverContent is present
*/
children?: never;
/**
* The content for the popover that is triggered on click.
*/
popoverContent: ReactNode | NullElement;
}
);

const MenuButton = ({
const BaseMenuButton = ({
ariaLabel,
ariaLabelledBy,
ariaDescribedBy,
buttonChildren,
buttonLabel = "",
buttonVariant = "secondary",
children,
popoverContent,
shouldCloseOnSelect = true,
endIcon: endIconProp,
id: idOverride,
isDisabled,
isOverflow,
menuAlignment = "left",
omitEndIcon = false,
size,
testId,
tooltipText,
translate,
}: MenuButtonProps) => {
}: BaseMenuButtonProps & AdditionalBaseMenuButtonProps) => {
const odysseyDesignTokens = useOdysseyDesignTokens();
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);

const isOpen = Boolean(anchorEl);
Expand Down Expand Up @@ -134,7 +175,7 @@ const MenuButton = ({
[closeMenu, openMenu, shouldCloseOnSelect],
);

const endIcon = endIconProp ? (
const endIcon = omitEndIcon ? undefined : endIconProp ? (
endIconProp
) : isOverflow ? (
<MoreIcon />
Expand Down Expand Up @@ -162,7 +203,7 @@ const MenuButton = ({

return (
<div>
<Button
<BaseButton
ariaControls={isOpen ? `${uniqueId}-menu` : undefined}
ariaExpanded={isOpen ? "true" : undefined}
ariaHasPopup="true"
Expand All @@ -174,31 +215,52 @@ const MenuButton = ({
id={`${uniqueId}-button`}
isDisabled={isDisabled}
label={buttonLabel}
children={buttonChildren}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NON-BLOCKING
I'd like to fix the types for if children or label, but not in this PR. Bryan's working on this already.

My comment is here for historical purposes.

onClick={openMenu}
size={size}
tooltipText={tooltipText}
translate={translate}
variant={buttonVariant}
/>

<MuiMenu
anchorOrigin={anchorOrigin}
transformOrigin={transformOrigin}
anchorEl={anchorEl}
id={`${uniqueId}-menu`}
MenuListProps={menuListProps}
onClose={closeMenu}
open={isOpen}
>
<MenuContext.Provider value={providerValue}>
{children}
</MenuContext.Provider>
</MuiMenu>
{children && (
<MuiMenu
anchorOrigin={anchorOrigin}
transformOrigin={transformOrigin}
anchorEl={anchorEl}
id={`${uniqueId}-menu`}
MenuListProps={menuListProps}
onClose={closeMenu}
open={isOpen}
>
<MenuContext.Provider value={providerValue}>
{children}
</MenuContext.Provider>
</MuiMenu>
)}

{popoverContent && (
<MuiPopover
open={isOpen}
anchorEl={anchorEl}
onClose={closeMenu}
anchorOrigin={anchorOrigin}
transformOrigin={transformOrigin}
>
<Box
sx={{
padding: odysseyDesignTokens.Spacing4,
}}
>
{popoverContent}
</Box>
</MuiPopover>
)}
</div>
);
};

const MemoizedMenuButton = memo(MenuButton);
MemoizedMenuButton.displayName = "MenuButton";
const MemoizedBaseMenuButton = memo(BaseMenuButton);
MemoizedBaseMenuButton.displayName = "BaseMenuButton";

export { MemoizedMenuButton as MenuButton };
export { MemoizedBaseMenuButton as BaseMenuButton };
Loading