/
Dock.tsx
91 lines (79 loc) · 1.79 KB
/
Dock.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
import React, { CSSProperties } from 'react'
import useDock from './useDock'
import { Orientation } from './reducer'
const styles: { [key: string]: CSSProperties } = {
dock: {
position: 'absolute',
zIndex: 100000,
transition: 'width 0.5s, min-width 0.5s, height 0.5s, min-height 0.5s',
overflow: 'auto',
boxShadow: '5px 10px 8px 10px #888',
border: '1px solid rgb(183, 183, 185)',
},
dockClosed: {
boxShadow: 'none',
border: 'none',
},
right: {
top: 0,
right: 0,
height: '100vh',
},
left: {
top: 0,
left: 0,
height: '100vh',
},
top: {
top: 0,
right: 0,
width: '100vw',
},
bottom: {
bottom: 0,
right: 0,
width: '100vw',
},
}
interface DockProps {
id?: string
style?: CSSProperties
}
function Dock({ id = 'react-use-dock', style = {} }: DockProps) {
const dock = useDock()
const getStyle = (): CSSProperties => {
const getSize = (value: number, unit: string): string => {
if (dock.isOpen) return value + unit
return '0px'
}
const orientationStyle = styles[dock.orientation]
const sizeStyle: CSSProperties = ([
'right',
'left',
] as Orientation[]).includes(dock.orientation)
? {
width: getSize(dock.size, 'vw'),
minWidth: getSize(dock.minSize, 'px'),
}
: {
height: getSize(dock.size, 'vh'),
minHeight: getSize(dock.minSize, 'px'),
}
const dockClosedStyle = dock.isOpen ? {} : styles.dockClosed
const dockStyle: CSSProperties = {
...styles.dock,
...orientationStyle,
...sizeStyle,
...dockClosedStyle,
...style,
}
return dockStyle
}
return (
<div id={id} style={getStyle()}>
{dock.render()}
</div>
)
}
export default Dock
export { Dock }