forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SideBarDrawer.js
70 lines (63 loc) · 2.04 KB
/
SideBarDrawer.js
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
import { useRouter } from 'next/router'
import { useEffect } from 'react'
/**
* 侧边栏抽屉面板,可以从侧面拉出
* @returns {JSX.Element}
* @constructor
*/
const SideBarDrawer = ({
children,
isOpen,
onOpen,
onClose,
className,
showOnPC = false
}) => {
const router = useRouter()
useEffect(() => {
const sideBarDrawerRouteListener = () => {
switchSideDrawerVisible(false)
}
router.events.on('routeChangeComplete', sideBarDrawerRouteListener)
return () => {
router.events.off('routeChangeComplete', sideBarDrawerRouteListener)
}
}, [router.events])
// 点击按钮更改侧边抽屉状态
const switchSideDrawerVisible = showStatus => {
if (showStatus) {
onOpen && onOpen()
} else {
onClose && onClose()
}
const sideBarDrawer = window.document.getElementById('sidebar-drawer')
const sideBarDrawerBackground = window.document.getElementById(
'sidebar-drawer-background'
)
if (showStatus) {
sideBarDrawer?.classList.replace('translate-x-[-100%]', 'translate-x-0')
sideBarDrawerBackground?.classList.replace('hidden', 'block')
} else {
sideBarDrawer?.classList.replace('translate-x-0', 'translate-x-[-100%]')
sideBarDrawerBackground?.classList.replace('block', 'hidden')
}
}
return (
<div
id='sidebar-wrapper'
className={`block ${showOnPC ? '' : 'lg:hidden'} top-0`}>
<div
id='sidebar-drawer'
className={`z-50 ${className} ${isOpen ? 'translate-x-0 opacity-100' : 'translate-x-[-100%] opacity-0'} transform transition-transform duration-300 ease-in-out bg-white dark:bg-gray-900 flex flex-col fixed h-full left-0 overflow-y-scroll top-0`}>
{children}
</div>
{/* 背景蒙版 */}
<div
id='sidebar-drawer-background'
onClick={() => switchSideDrawerVisible(false)}
className={`${isOpen ? 'block' : 'hidden'} fixed top-0 left-0 z-20 w-full h-full bg-black/70 transition-opacity duration-300`}
/>
</div>
)
}
export default SideBarDrawer