-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.tsx
145 lines (134 loc) · 3.82 KB
/
index.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
import React, { useEffect, useMemo, useRef } from 'react'
import type { CSSProperties } from 'react'
import './styles.css'
type IDirection = 'left' | 'right' | 'top' | 'bottom'
type Props = {
open: boolean
onClose?: VoidFunction
direction: IDirection
lockBackgroundScroll?: boolean
children?: React.ReactNode
duration?: number
overlayOpacity?: number
overlayColor?: String
enableOverlay?: boolean
style?: CSSProperties
zIndex?: number
size?: number | string
className?: string
customIdSuffix?: string
overlayClassName?: string
}
type DirectionStyle = Pick<
CSSProperties,
'top' | 'left' | 'right' | 'bottom' | 'width' | 'height' | 'transform'
>
const getDirectionStyle = (
dir: IDirection,
size?: number | string,
): DirectionStyle => {
const directionStyle: Record<IDirection, DirectionStyle> = {
left: {
top: 0,
left: 0,
transform: 'translate3d(-100%, 0, 0)',
width: size,
height: '100vh',
},
right: {
top: 0,
right: 0,
transform: 'translate3d(100%, 0, 0)',
width: size,
height: '100vh',
},
bottom: {
left: 0,
right: 0,
bottom: 0,
transform: 'translate3d(0, 100%, 0)',
width: '100%',
height: size,
},
top: {
left: 0,
right: 0,
top: 0,
transform: 'translate3d(0, -100%, 0)',
width: '100%',
height: size,
},
}
return directionStyle[dir]
}
const EZDrawer: React.FC<Props> = (props) => {
const {
open,
onClose = () => {},
children,
style,
enableOverlay = true,
overlayColor = '#000',
overlayOpacity = 0.4,
zIndex = 100,
duration = 500,
direction,
size = 250,
className,
customIdSuffix,
lockBackgroundScroll = false,
overlayClassName = '',
} = props
const bodyRef = useRef<HTMLBodyElement | null>(null)
useEffect(() => {
const updatePageScroll = () => {
bodyRef.current = window.document.querySelector('body')
if (bodyRef.current && lockBackgroundScroll) {
bodyRef.current.style.overflow = open ? 'hidden' : ''
}
}
updatePageScroll()
}, [open])
const idSuffix = useMemo(() => {
return customIdSuffix || (Math.random() + 1).toString(36).substring(7)
}, [customIdSuffix])
const overlayStyles: CSSProperties = {
backgroundColor: overlayColor.toString(),
opacity: overlayOpacity,
zIndex: zIndex,
}
const drawerStyles: CSSProperties = {
zIndex: zIndex + 1,
transitionDuration: `${duration}ms`,
...getDirectionStyle(direction, size),
...style,
}
return (
<div id={'EZDrawer' + idSuffix} className='EZDrawer'>
<input
type='checkbox'
id={'EZDrawer__checkbox' + idSuffix}
className='EZDrawer__checkbox'
onChange={onClose}
checked={open}
/>
<nav
role='navigation'
id={'EZDrawer__container' + idSuffix}
style={drawerStyles}
className={'EZDrawer__container ' + className}
>
{children}
</nav>
{enableOverlay && (
<label
htmlFor={'EZDrawer__checkbox' + idSuffix}
id={'EZDrawer__overlay' + idSuffix}
className={'EZDrawer__overlay ' + overlayClassName}
style={overlayStyles}
/>
)}
</div>
)
}
export default EZDrawer