Skip to content
Open
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
69 changes: 49 additions & 20 deletions dashboards/src/components/Panel/PanelActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { Stack, Box, Popover, CircularProgress, styled, PopoverPosition } from '@mui/material';
import { isValidElement, PropsWithChildren, ReactElement, ReactNode, useMemo, useState } from 'react';
import { Stack, Box, CircularProgress, styled, Popper, ClickAwayListener } from '@mui/material';
import { isValidElement, PropsWithChildren, ReactElement, ReactNode, useMemo, useState, MouseEvent } from 'react';
import { InfoTooltip } from '@perses-dev/components';
import { QueryData } from '@perses-dev/plugin-system';
import DatabaseSearch from 'mdi-material-ui/DatabaseSearch';
Expand Down Expand Up @@ -338,25 +338,44 @@ export const PanelActions: React.FC<PanelActionsProps> = ({
};

const OverflowMenu: React.FC<
PropsWithChildren<{ title?: string; icon?: ReactElement; direction?: 'row' | 'column' }>
> = ({ children, title, icon, direction = 'row' }) => {
const [anchorPosition, setAnchorPosition] = useState<PopoverPosition>();
PropsWithChildren<{
title?: string;
icon?: ReactElement;
direction?: 'row' | 'column';
placement?:
| 'bottom'
| 'top'
| 'left'
| 'right'
| 'bottom-start'
| 'bottom-end'
| 'top-start'
| 'top-end'
| 'left-start'
| 'left-end'
| 'right-start'
| 'right-end';
offsetX?: number;
offsetY?: number;
}>
> = ({ children, title, icon, direction = 'row', placement = 'bottom', offsetX = -2, offsetY = -25 }) => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);

// do not show overflow menu if there is no content (for example, edit actions are hidden)
const hasContent = isValidElement(children) || (Array.isArray(children) && children.some(isValidElement));
if (!hasContent) {
return null;
}

const handleClick = (event: React.MouseEvent<HTMLElement>): void => {
setAnchorPosition(event.currentTarget.getBoundingClientRect());
const handleClick = (event: MouseEvent<HTMLElement>): void => {
setAnchorEl(anchorEl ? null : event.currentTarget);
};

const handleClose = (): void => {
setAnchorPosition(undefined);
setAnchorEl(null);
};

const open = Boolean(anchorPosition);
const open = Boolean(anchorEl);
const id = open ? 'actions-menu' : undefined;

return (
Expand All @@ -370,21 +389,31 @@ const OverflowMenu: React.FC<
>
{icon ?? <MenuIcon fontSize="inherit" />}
</HeaderIconButton>
<Popover
<Popper
id={id}
open={open}
anchorReference="anchorPosition"
anchorPosition={anchorPosition}
onClose={handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
anchorEl={anchorEl}
placement={placement}
modifiers={[
{
name: 'offset',
options: {
offset: [offsetX, offsetY],
},
},
]}
sx={{
backgroundColor: (theme) => theme.palette.background.paper,
borderRadius: 1,
boxShadow: (theme) => theme.shadows[4],
}}
>
<Stack direction={direction} alignItems="center" sx={{ padding: 1 }} onClick={handleClose}>
{children}
</Stack>
</Popover>
<ClickAwayListener onClickAway={handleClose}>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In addition to this we might want to add a keyboard handler, so when clicking escape the popper is closed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, good idea 👍

<Stack direction={direction} alignItems="center" sx={{ padding: 1 }} onClick={handleClose}>
{children}
</Stack>
</ClickAwayListener>
</Popper>
</Box>
);
};
Loading