After some trial and error, I came up with a solution that involved tweaking the JSX element tree by creating a specially styled wrapper div:
const appBarHeightCSS = css`
min-height: 56px;
@media (min-width: 0px) and (orientation: landscape) {
min-height: 48px;
}
@media (min-width: 600px) {
min-height: 64px;
}
`;
const StyledButtonContainerDiv = styled.div`
${appBarHeightCSS}
display: flex;
justify-content: center;
align-items: center;
color: ${theme.palette.grey['400']};
`;
Then, I passed the div element using a reference:
const accountButtonDiv = React.useRef<HTMLDivElement | null>();
...
<StyledButtonContainerDiv ref={accountButtonDiv}>
<ToolbarButton
anchorRef={accountButtonDiv}
Menu={AccountMenu}
aria-label="account of current user"
aria-controls="primary-search-account-menu"
aria-haspopup="true"
>
<AccountCircle />
</ToolbarButton>
...
This was all to enhance the custom ToolbarButton component:
export default function ToolbarButton(props): JSX.Element {
const { anchorRef, Menu, badgeContent, children, ...defaultProps } = props;
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const handleMenuOpen = () => {
setAnchorEl(anchorRef.current);
};
return (
<>
<IconButton {...defaultProps} onClick={handleMenuOpen} color="inherit">
<Badge badgeContent={badgeContent} color="secondary">
{children}
</Badge>
</IconButton>
<Menu anchorEl={anchorEl} setAnchorEl={setAnchorEl} />
</>
);
}