forked from lucko/paste
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuButton.tsx
More file actions
140 lines (124 loc) · 2.89 KB
/
MenuButton.tsx
File metadata and controls
140 lines (124 loc) · 2.89 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import styled from 'styled-components';
import Button from './Button';
interface MenuButtonProps<T extends string> {
label: string;
ids: T[] | Record<string, T[]>;
value: T;
setValue: (value: T) => void;
}
export default function MenuButton<T extends string>({
label,
ids,
value,
setValue,
}: MenuButtonProps<T>) {
const [open, setOpen] = useState<boolean>(false);
const [menuPosition, setMenuPosition] = useState<{
top: number;
left: number;
}>({ top: 0, left: 0 });
const buttonRef = useRef<HTMLDivElement | null>(null);
// close the menu when a click is made elsewhere
useEffect(() => {
const listener = () => setOpen(false);
window.addEventListener('click', listener);
return () => window.removeEventListener('click', listener);
}, [setOpen]);
function toggleOpen(e: React.MouseEvent) {
e.stopPropagation();
if (!buttonRef.current) {
return;
}
if (!open) {
const rect = buttonRef.current.getBoundingClientRect();
setMenuPosition({
top: rect.bottom + window.scrollY,
left: rect.left + window.scrollX,
});
}
setOpen(!open);
}
function select(e: React.MouseEvent<HTMLLIElement>, id: T) {
e.stopPropagation();
setOpen(false);
setValue(id);
}
function make(ids: T[]) {
return ids.map(id => (
<li
key={id}
className={id === value ? 'selected' : undefined}
onClick={e => select(e, id)}
>
{id}
</li>
));
}
let items;
if (Array.isArray(ids)) {
items = make(ids);
} else {
items = Object.entries(ids).map(([title, values]) =>
[
<li className="title" key={title} onClick={e => e.stopPropagation()}>
[{title}]
</li>,
]
.concat(make(values))
.flat()
);
}
return (
<>
<Button onClick={toggleOpen} ref={buttonRef}>
[<span>{label}: </span>
{value}]
</Button>
{open &&
createPortal(
<Menu style={{ top: menuPosition.top, left: menuPosition.left }}>
{items}
</Menu>,
document.body
)}
</>
);
}
const Menu = styled.ul`
position: absolute;
margin: 0;
padding: 0;
list-style: none;
background-color: ${props => props.theme.highlight};
max-height: calc(100vh - 2em);
overflow: auto;
z-index: 20;
> li {
cursor: pointer;
}
> li.title {
text-align: center;
padding: 0.7em 0 0.3em 0;
cursor: initial;
font-weight: bold;
}
> li.title:hover {
background-color: inherit;
}
> li.selected {
&::before {
content: '*';
font-weight: bold;
}
padding-left: calc(1em - 1ch);
font-weight: bold;
}
> li {
padding: 0em 1em 0.05em 1em;
}
> li:hover {
background-color: ${props => props.theme.secondary};
}
`;