-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
menu-bar.tsx
120 lines (103 loc) 路 2.88 KB
/
menu-bar.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/** @jsx jsx */
import { jsx } from '@emotion/core';
import { isPlainObject } from '@remirror/core-helpers';
import { useRemirrorTheme } from '@remirror/ui';
import { Button, ButtonProps } from '@remirror/ui-buttons';
import { DropdownProps, DropdownSelect } from '@remirror/ui-dropdown';
import { FC, forwardRef, Fragment, ReactElement } from 'react';
interface MenubarDropdownConfiguration extends DropdownProps {
type: 'dropdown';
}
interface MenubarGroupConfiguration extends MenubarGroupProps {
type: 'group';
}
interface MenubarButton extends Omit<ButtonProps, 'ref'> {
type: 'button';
}
export type MenubarContent = MenubarDropdownConfiguration | MenubarGroupConfiguration | MenubarButton;
interface MenubarProps {
content: MenubarContent[];
}
export const Menubar = ({ content }: MenubarProps) => {
const { sx } = useRemirrorTheme();
return (
<nav css={sx({ display: 'flex' })}>
<MenuTree content={content} />
</nav>
);
};
interface MenuTreeProps {
content: MenubarContent[];
}
const MenuTree: FC<MenuTreeProps> = ({ content }) => {
const tree = content.map((item, index) => {
switch (item.type) {
case 'group': {
const { type, ...rest } = item;
return (
<MenubarGroup key={index} {...rest}>
<MenuTree content={rest.content} />
</MenubarGroup>
);
}
case 'dropdown': {
const { type, ...rest } = item;
return <DropdownSelect key={index} dropdownPosition='below left' {...rest} />;
}
case 'button': {
const { type, ...rest } = item;
return <Button key={index} variant='background' {...rest} />;
}
default:
throw new Error(
`Invalid configuration passed into the MenuTree: ${
isPlainObject(item) ? JSON.stringify(item, null, 2) : item
}`,
);
}
});
return <Fragment>{tree}</Fragment>;
};
interface MenubarGroupProps {
render?(MenubarGroupProps: MenubarGroupConfiguration): ReactElement;
content: MenubarContent[];
}
export const MenubarGroup: FC<MenubarGroupProps> = ({ children, render, ...props }) => {
const { sx } = useRemirrorTheme();
if (render) {
return render({ ...props, type: 'group' });
}
return (
<div
css={sx({
display: 'flex',
position: 'relative',
':after': {
position: 'absolute',
content: '""',
top: 0,
right: 0,
height: '100%',
width: '1px',
backgroundColor: 'grey:fade',
},
})}
>
{children}
</div>
);
};
/**
* The menu component.
*/
export const Menu = forwardRef<HTMLMenuElement, JSX.IntrinsicElements['menu']>(
({ children, ...props }, ref) => {
const { sx } = useRemirrorTheme();
return (
<nav {...props} css={sx({ display: 'flex' })} ref={ref}>
{children}
</nav>
);
},
);
Menu.displayName = 'Menu';