-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMenu.tsx
88 lines (81 loc) · 2.37 KB
/
Menu.tsx
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
80
81
82
83
84
85
86
87
88
import type { VariantProps } from 'class-variance-authority';
import type { Ref } from 'react';
import type {
MenuItemProps as AriaMenuItemProps,
MenuProps as AriaMenuProps,
MenuTriggerProps,
SubmenuTriggerProps,
} from 'react-aria-components';
import { Icon } from '@launchpad-ui/icons';
import { cva } from 'class-variance-authority';
import {
Menu as AriaMenu,
MenuItem as AriaMenuItem,
MenuTrigger,
SubmenuTrigger,
composeRenderProps,
} from 'react-aria-components';
import { CheckboxInner, checkbox } from './Checkbox';
import styles from './styles/Menu.module.css';
const menu = cva(styles.menu);
const item = cva(styles.item, {
variants: {
variant: {
default: styles.default,
destructive: styles.destructive,
},
},
defaultVariants: {
variant: 'default',
},
});
interface MenuProps<T> extends AriaMenuProps<T> {
ref?: Ref<HTMLDivElement>;
}
interface MenuItemProps<T> extends AriaMenuItemProps<T>, VariantProps<typeof item> {
ref?: Ref<T>;
}
/**
* A menu displays a list of actions or options that a user can choose.
*
* https://react-spectrum.adobe.com/react-aria/Menu.html
*/
const Menu = <T extends object>({ className, ref, ...props }: MenuProps<T>) => {
return <AriaMenu {...props} ref={ref} className={menu({ className })} />;
};
/**
* A MenuItem represents an individual action in a Menu.
*/
const MenuItem = <T extends object>({ variant = 'default', ref, ...props }: MenuItemProps<T>) => {
return (
<AriaMenuItem
{...props}
ref={ref}
className={composeRenderProps(props.className, (className, renderProps) =>
item({ ...renderProps, variant, className }),
)}
>
{composeRenderProps(
props.children,
(children, { selectionMode, isSelected, hasSubmenu, isDisabled }) => (
<>
{selectionMode === 'multiple' && (
<div
className={checkbox()}
data-selected={isSelected || undefined}
data-disabled={isDisabled || undefined}
>
<CheckboxInner isSelected={isSelected} />
</div>
)}
<span className={styles.content}>{children}</span>
{selectionMode === 'single' && isSelected && <Icon name="check-circle" size="small" />}
{hasSubmenu && <Icon name="chevron-right" size="small" />}
</>
),
)}
</AriaMenuItem>
);
};
export { Menu, MenuItem, MenuTrigger, SubmenuTrigger };
export type { MenuProps, MenuItemProps, MenuTriggerProps, SubmenuTriggerProps };