Open History to see the last commit
1import * as React from 'react';
2import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock';
3import cn from 'classnames';
4import { Container } from './Container';
5import { FiChevronRight as ArrowRightSidebar } from 'react-icons/fi';
6import { Search } from './Search';
7import { useRouter } from 'next/router';
8export function SidebarMobile({
9 children
10}) {
11 const [opened, setOpen] = React.useState(false);
12 const menuRef = React.useRef(null);
13 const searchRef = React.useRef(null);
14 const router = useRouter();
15β
16 const openMenu = () => {
17 if (menuRef.current != null) {
18 disableBodyScroll(menuRef.current);
19 setOpen(true);
20 }
21 };
22β
23 const closeMenu = () => {
24 if (menuRef.current != null) {
25 enableBodyScroll(menuRef.current);
26 setOpen(false);
27 }
28 };
29β
30 const toggleOpen = () => {
31 if (opened) {
32 closeMenu();
33 } else {
34 openMenu();
35 }
36 };
37β
38 const onRouteChange = () => {
39 closeMenu();
40 };
41β
42 React.useEffect(() => {
43 onRouteChange();
44 return () => {
45 clearAllBodyScrollLocks();
46 };
47 }, [router.asPath]);
48 return <div className="lg:hidden">
49 <Container>
50 <div className="sidebar-search py-2 z-10">
51 <Search />
52 </div>
53 <label htmlFor="dropdown-input" className={cn('w-full', {
54 opened
55 })}>
56 <input id="dropdown-input" className="hidden" type="checkbox" checked={opened} onChange={toggleOpen} />
57 <div className="docs-select flex w-full items-center">
58 <ArrowRightSidebar className="text-gray-600 -ml-1" />
59 Menu
60 </div>
61 </label>
62 <div className="docs-dropdown shadow-xl" ref={menuRef}>
63 <Container>
64 <nav>{children}</nav>
65 </Container>
66 </div>
67 <style jsx>{`
68 .docs-select {
69 display: flex;
70 height: 2.5rem;
71 width: 100%;
72 line-height: 3rem;
73 align-items: center;
74 text-align: left;
75 cursor: pointer;
76 }
77 .docs-dropdown {
78 position: absolute;
79 left: 0;
80 right: 0;
81 top: 100%;
82 bottom: 100%;
83 background: white;
84 overflow-y: auto;
85 -webkit-overflow-scrolling: touch;
86 }
87 .docs-dropdown nav {
88 padding: 10px 0;
89 }
90 .opened ~ .docs-dropdown {
91 min-height: 80px;
92 bottom: calc(153px - 90vh);
93 border-top: 1px solid #eaeaea;
94 }
95 .docs-select :global(svg) {
96 margin-left: 1px;
97 margin-right: 14px;
98 transition: transform 0.15s ease;
99 }
100 .opened > .docs-select :global(svg) {
101 transform: rotate(90deg);
102 }
103β
104 @media screen and (max-width: 640px) {
105 .opened ~ .docs-dropdown {
106 bottom: calc(203px - 90vh);
107 }
108 }
109 `}</style>
110 </Container>
111 </div>;
112}