-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathaction-menu.tsx
More file actions
79 lines (72 loc) · 1.99 KB
/
action-menu.tsx
File metadata and controls
79 lines (72 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { FC } from 'react';
import React, { useState } from 'react';
import { useClickOutside } from '@ff-client/hooks/use-click-outside';
import { useOnKeypress } from '@ff-client/hooks/use-on-keypress';
import classes from '@ff-client/utils/classes';
import translate from '@ff-client/utils/translations';
import EllipsisIcon from '@ff-icons/actions/ellipsis.svg';
import {
ActionMenuButton,
ActionMenuDropdown,
ActionMenuItem,
ActionMenuWrapper,
} from './action-menu.styles';
import type { ActionMenuChoice } from './action-menu.types';
type Props = {
ariaLabel?: string;
choices: ActionMenuChoice[];
};
export const ActionMenu: FC<Props> = ({
choices,
ariaLabel = translate('Actions'),
}) => {
const [open, setOpen] = useState(false);
// Close menu on Esc key press
useOnKeypress({
callback: (event) => {
if (event.key === 'Escape') {
setOpen(false);
}
},
meetsCondition: open,
type: 'keyup',
});
// Close menu on click outside
const wrapperRef = useClickOutside<HTMLDivElement>({
isEnabled: open,
callback: () => setOpen(false),
});
return (
<ActionMenuWrapper ref={wrapperRef}>
<ActionMenuButton
type="button"
className={classes(open && 'open')}
onClick={() => setOpen((prev) => !prev)}
aria-label={ariaLabel}
aria-expanded={open}
title={ariaLabel}
>
<EllipsisIcon />
</ActionMenuButton>
{open && (
<ActionMenuDropdown>
{choices.map((choice) => (
<ActionMenuItem
key={choice.label}
type="button"
className={choice.className}
$destructive={choice.destructive}
onClick={() => {
setOpen(false);
choice.onClick();
}}
>
{choice.icon}
<span>{choice.label}</span>
</ActionMenuItem>
))}
</ActionMenuDropdown>
)}
</ActionMenuWrapper>
);
};