-
Notifications
You must be signed in to change notification settings - Fork 342
/
dropdown-menu-small.tsx
380 lines (351 loc) · 11.4 KB
/
dropdown-menu-small.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import React from 'react'
import styled, { ThemeProvider, css } from 'styled-components'
import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon'
import { ClickAway } from '@worldbrain/memex-common/lib/common-ui/components/click-away-wrapper'
import LoadingIndicator from '@worldbrain/memex-common/lib/common-ui/components/loading-indicator'
import { PopoutBox } from '@worldbrain/memex-common/lib/common-ui/components/popout-box'
import { AImodels } from '@worldbrain/memex-common/lib/summarization/types'
export interface MenuItemProps {
name: string
id?: AImodels
info?: string | JSX.Element
isDisabled?: boolean
soonAvailable?: boolean
}
interface ThemeProps {
leftMenuOffset?: string
}
export interface Props<T extends MenuItemProps = MenuItemProps> {
isOpen?: boolean
toggleOpen?: () => void
menuItems: MenuItemProps[]
btnChildren?: React.ReactNode
onMenuItemClick?: (item: T) => void
theme?: ThemeProps
keepSelectedState?: boolean
initSelectedItem?: string
selectedState?: string
btnId?: string
menuTitle?: string
width?: string
backgroundColor?: string
elementHeight?: string
hideDescriptionInPreview: boolean
getRootElement: () => HTMLElement
renderAICounter?: () => JSX.Element
}
interface State {
isOpen: boolean
selected: MenuItemProps
isOpened: boolean
}
export class DropdownMenuBtn extends React.PureComponent<Props, State> {
static defaultProps: Partial<Props> = { initSelectedItem: null }
private menuRef = React.createRef<HTMLDivElement>()
state: State = {
isOpen: false,
selected:
this.props.menuItems.find(
(item) => item.id === this.props.initSelectedItem,
) || this.props.menuItems[0],
isOpened: false,
}
private lastToggleCall = 0
private get theme() {
return {
...this.props.theme,
isMenuOpen: this.state.isOpen,
}
}
private get isOpen(): boolean {
return this.props.isOpen ?? this.state.isOpen
}
private toggleMenu = () => {
// This check covers the case when the menu is open and you click the button to close it:
// This case triggers the "ClickAway" event calling this method AND the btn's call,
// which results in flickering between the 2 states
const now = Date.now()
if (now - this.lastToggleCall < 100) {
return
}
this.lastToggleCall = now
if (this.props.toggleOpen) {
this.props.toggleOpen()
} else {
this.setState((state) => ({ isOpen: !state.isOpen }))
}
}
private handleItemClick: (
item: MenuItemProps,
) => React.MouseEventHandler = (item) => (e) => {
if (item.isDisabled) {
e.preventDefault()
return
}
this.setState({
isOpen: this.props.keepSelectedState,
selected: item,
isOpened: false,
})
this.props.onMenuItemClick(item)
}
private renderMenuItems() {
// if (this.props.selectedState === 1) {
// this.setState({
// selected: 1,
// })
// }
return (
<MenuItemContainerUnfolded isOpen={this.state.isOpened}>
{this.props.menuItems.map((item) => (
<MenuItem
onClick={this.handleItemClick(item)}
isSelected={this.state.selected.id === item.id}
backgroundColor={this.props.backgroundColor}
elementHeight={this.props.elementHeight}
isOpened={this.state.isOpened}
isDisabled={item.isDisabled}
>
<MenuItemName
isSelected={this.state.selected.id === item.id}
isOpened={this.state.isOpened}
>
<MenuItemBox isOpened={this.state.isOpened}>
<MenuItemName
isSelected={
this.state.selected.id === item.id
}
isOpened={this.state.isOpened}
>
{item.name}
</MenuItemName>
{item.info && (
<MenuItemInfo isDisabled={item.isDisabled}>
{item.info}
</MenuItemInfo>
)}
</MenuItemBox>
{this.state.selected.id === item.id && (
<Icon
filePath="check"
color="prime1"
heightAndWidth="18px"
hoverOff
/>
)}
</MenuItemName>
</MenuItem>
))}
{this.props.renderAICounter()}
</MenuItemContainerUnfolded>
)
}
renderPopoutMenu() {
if (this.state.isOpened) {
return (
<PopoutBox
targetElementRef={this.menuRef.current}
closeComponent={() => this.setState({ isOpened: false })}
placement="bottom-end"
width={this.props.width ?? '290px'}
getPortalRoot={this.props.getRootElement}
offsetX={15}
>
{this.props.children ?? this.renderMenuItems()}
</PopoutBox>
)
}
}
render() {
const selectedMenuItem = this.props.menuItems.find(
(item) =>
item.id ===
(this.state.selected.id || this.props.selectedState),
)
return (
<ThemeProvider theme={this.theme}>
<Menu
elementHeight={this.props.elementHeight}
ref={this.menuRef}
onMouseEnter={(e) => {
e.preventDefault()
e.stopPropagation
}}
onMouseLeave={(e) => {
e.preventDefault()
e.stopPropagation
}}
width={this.props.width}
isOpen={this.state.isOpened}
>
{' '}
<MenuItem
onClick={() => {
this.setState({
isOpened: true,
})
}}
isSelected={true}
backgroundColor={this.props.backgroundColor}
elementHeight={this.props.elementHeight}
isOpened={this.state.isOpened}
isDisabled={selectedMenuItem.isDisabled}
>
<MenuItemButton
isOpened={this.state.isOpened}
isSelected={true}
>
<MenuItemBox isOpened={false}>
{selectedMenuItem.name}
</MenuItemBox>
<Icon
filePath="arrowDown"
color="greyScale6"
heightAndWidth="18px"
hoverOff
padding={'0px'}
/>
</MenuItemButton>
</MenuItem>
{this.renderPopoutMenu()}
</Menu>
</ThemeProvider>
)
}
}
const MenuItemBox = styled.div<{ isOpened }>`
display: flex;
align-items: flex-start;
justify-content: center;
flex-direction: column;
grid-gap: 5px;
color: ${(props) =>
!props.isOpened
? props.theme.colors.greyScale6
: props.theme.colors.white};
`
const MenuItem = styled.div<{
isSelected
backgroundColor
elementHeight: string
isOpened: boolean
isDisabled: boolean
}>`
background: ${(props) =>
props.backgroundColor && props.theme.colors[props.backgroundColor]};
line-height: 20px;
width: fill-available;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
white-space: nowrap;
border-radius: 6px;
cursor: pointer;
height: ${(props) =>
props.elementHeight && props.isOpened ? props.elementHeight : '20px'};
${(props) =>
!props.isSelected &&
css`
cursor: pointer;
& * {
cursor: pointer;
}
`};
${(props) =>
!props.isOpened &&
css`
& * {
cursor: pointer;
}
`};
${(props) =>
props.isDisabled &&
css`
cursor: not-allowed;
opacity: 0.8;
* {
cursor: not-allowed;
opacity: 0.9;
}
`};
`
const MenuTitle = styled.div`
padding: 8px 15px 0px 15px;
margin-bottom: 10px;
font-size: 14px;
`
const MenuItemName = styled.div<{ isSelected: boolean; isOpened: boolean }>`
color: ${(props) =>
props.isOpened
? props.theme.colors.greyScale6
: props.theme.colors.greyScale5};
font-size: 14px;
font-weight: 400;
display: flex;
align-items: center;
grid-gap: 5px;
justify-content: 'space-between';
width: -webkit-fill-available;
${(props) =>
props.isOpened &&
css`
font-size: 14px;
`}
`
const MenuItemButton = styled.div<{ isSelected: boolean; isOpened: boolean }>`
color: ${(props) => props.theme.colors.greyScale5};
font-size: 12px;
font-weight: 400;
display: flex;
align-items: center;
width: fill-available;
width: -moz-available;
grid-gap: 5px;
justify-content: center;
width: -webkit-fill-available;
`
const MenuItemInfo = styled.div<{
isDisabled: boolean
}>`
font-weight: 300;
font-size: 14px;
color: ${(props) => props.theme.colors.greyScale5};
${(props) =>
props.isDisabled &&
css`
color: ${(props) => props.theme.colors.greyScale7};
`}
`
const Menu = styled.div<{
isOpen: boolean
elementHeight: string
width: string
}>`
list-style: none;
border-radius: 10px;
width: ${(props) => props.width ?? 'max-content'};
flex-direction: column;
z-index: 1000;
position: relative;
grid-gap: 5px;
overflow: visible;
height: fill-available;
height: -moz-available;
width: fill-available;
width: -moz-available;
display: flex;
align-items: center;
justify-content: center;
`
const MenuItemContainerUnfolded = styled.div<{
isOpen: boolean
}>`
display: flex;
flex-direction: column;
border-radius: 10px;
background: ${(props) => props.theme.colors.greyScale2};
padding: 15px;
width: fill-available;
grid-gap: 10px;
`