-
Notifications
You must be signed in to change notification settings - Fork 9
/
Command.tsx
159 lines (146 loc) · 4.9 KB
/
Command.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
import * as React from 'react'
import { MoreLine as MoreLineIcon } from '@fluent-windows/icons' // TODO treeShaking
import { useClickOutside, useReveal, usePopper } from '@fluent-windows/hooks' // TODO treeShaking
import { omit } from '../utils'
import Box from '../Box'
import { styles } from './Command.styled'
import Secondary from './components/Secondary'
import Content from './components/Content'
import Portal from '../Portal'
import Item from '../Item'
import Transition from '../Transition'
import {
CommandProps,
CommandContainer,
CommandType,
CommandPropTypes,
CommandClassProps
} from './Command.type'
import { createUseStyles } from 'react-jss'
import classNames from 'classnames'
export const name = 'Command'
const useStyles = createUseStyles<CommandClassProps>(styles, { name })
export const CommandContext = React.createContext(false)
const Command: React.FC<CommandProps> = React.forwardRef<HTMLDivElement, CommandProps>(
(props, ref): React.ReactElement => {
const {
as = 'div',
className: classNameProp,
acrylic = false,
reveal = false,
children,
...rest
} = props
const classes = useStyles(props)
const className = classNames(classes.root, classNameProp)
const container = React.useMemo<CommandContainer>(
(): CommandContainer =>
React.Children.toArray(children).reduce<CommandContainer>(
(acc, cur): CommandContainer => {
if (cur.type.displayName! === 'FCommandContent') {
return {
...acc,
content: [...acc.content, cur]
}
} else if (cur.type.displayName === 'FCommandSecondary') {
return {
...acc,
secondary: cur.props.children
}
}
return {
...acc,
standard: [...acc.standard, cur]
}
},
{
content: [],
standard: [],
secondary: null
}
),
[children]
)
// Reveal does not take effect when using acrylic
const _reveal = React.useMemo((): boolean => (acrylic ? false : reveal), [acrylic, reveal])
const [RevealWrapper] = useReveal()
// Secondary Popup related
const [secondaryVisible, setSecondaryVisible] = React.useState(false)
const handleSecondaryVisible = React.useCallback((): void => {
if (secondaryVisible) return
setSecondaryVisible((visible: boolean): boolean => !visible)
}, [secondaryVisible])
// Click on the area outside the More menu to close the More menu.
const [referenceRef, popperRef] = usePopper<HTMLDivElement, HTMLDivElement>({
placement: 'bottom-end'
})
useClickOutside(popperRef, (event: MouseEvent | TouchEvent): void => {
// @ts-ignore
if (!referenceRef.current || referenceRef.current.contains(event.target)) return
setSecondaryVisible((visible: boolean): boolean => !visible)
})
const otherProps = omit(rest, ['display', 'backgroundColor', 'color'])
return (
<Box
ref={ref}
as={as}
acrylic={acrylic}
className={className}
backgroundColor="standard.light2"
{...otherProps}
>
<CommandContext.Provider value={_reveal as boolean}>
{!!container.content.length && <div className={classes.content}>{container.content}</div>}
<div className={classes.primary}>
{_reveal
? container.standard.map(
(child, i): React.ReactElement => <RevealWrapper key={i}>{child}</RevealWrapper>
)
: container.standard}
</div>
{container.secondary !== null &&
(_reveal ? (
<RevealWrapper>
<Item
ref={referenceRef}
style={{ height: '100%' }}
onClick={handleSecondaryVisible}
prefix={<MoreLineIcon />}
/>
</RevealWrapper>
) : (
<Item ref={referenceRef} onClick={handleSecondaryVisible} prefix={<MoreLineIcon />} />
))}
<Portal>
<Transition visible={secondaryVisible} wrapper={true} mountOnEnter unmountOnExit>
<Box
ref={popperRef}
acrylic={acrylic}
backgroundColor="standard.light2"
className={classes.secondaryRoot}
>
{container.secondary}
</Box>
</Transition>
</Portal>
</CommandContext.Provider>
</Box>
)
}
)
Object.defineProperty(Command, 'Secondary', {
get(): typeof Secondary {
return Secondary
}
})
Object.defineProperty(Command, 'Content', {
get(): typeof Content {
return Content
}
})
Command.displayName = `F${name}`
Command.propTypes = CommandPropTypes
Command.defaultProps = {
reveal: false
}
export default Command as CommandType