Skip to content

Commit d81f28b

Browse files
authored
Merge pull request #943 from Spiteless/ts.refactor_MobileNav
Refactor MobileNav to use headlessui
2 parents 3c53f58 + f345685 commit d81f28b

File tree

1 file changed

+67
-38
lines changed

1 file changed

+67
-38
lines changed

components/MobileNav.tsx

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

3-
import { useState } from 'react'
3+
import { Dialog, Transition } from '@headlessui/react'
4+
import { Fragment, useState } from 'react'
45
import Link from './Link'
56
import headerNavLinks from '@/data/headerNavLinks'
67

@@ -26,8 +27,7 @@ const MobileNav = () => {
2627
xmlns="http://www.w3.org/2000/svg"
2728
viewBox="0 0 20 20"
2829
fill="currentColor"
29-
className="h-8 w-8 text-gray-900 hover:text-primary-500 dark:text-gray-100
30-
dark:hover:text-primary-400"
30+
className="h-8 w-8 text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
3131
>
3232
<path
3333
fillRule="evenodd"
@@ -36,43 +36,72 @@ const MobileNav = () => {
3636
/>
3737
</svg>
3838
</button>
39-
<div
40-
className={`fixed left-0 top-0 z-10 h-full w-full transform bg-white opacity-95 duration-300 ease-in-out dark:bg-gray-950 dark:opacity-[0.98] ${
41-
navShow ? 'translate-x-0' : 'translate-x-full'
42-
}`}
43-
>
44-
<div className="flex justify-end">
45-
<button className="mr-8 mt-11 h-8 w-8" aria-label="Toggle Menu" onClick={onToggleNav}>
46-
<svg
47-
xmlns="http://www.w3.org/2000/svg"
48-
viewBox="0 0 20 20"
49-
fill="currentColor"
50-
className="text-gray-900 hover:text-primary-500 dark:text-gray-100
51-
dark:hover:text-primary-400"
52-
>
53-
<path
54-
fillRule="evenodd"
55-
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
56-
clipRule="evenodd"
57-
/>
58-
</svg>
59-
</button>
60-
</div>
61-
<nav className="fixed mt-8 h-full">
62-
{headerNavLinks.map((link) => (
63-
<div key={link.title} className="px-12 py-4">
64-
<Link
65-
href={link.href}
66-
className="text-2xl font-bold tracking-widest text-gray-900 hover:text-primary-500 dark:text-gray-100
67-
dark:hover:text-primary-400"
68-
onClick={onToggleNav}
39+
<Transition appear show={navShow} as={Fragment}>
40+
<Dialog as="div" className="relative z-10" onClose={onToggleNav}>
41+
<Transition.Child
42+
as={Fragment}
43+
enter="ease-out duration-300"
44+
enterFrom="opacity-0"
45+
enterTo="opacity-100"
46+
leave="ease-in duration-200"
47+
leaveFrom="opacity-100"
48+
leaveTo="opacity-0"
49+
>
50+
<div className="fixed inset-0 bg-black/25" />
51+
</Transition.Child>
52+
53+
<div className="fixed inset-0 overflow-y-auto">
54+
<div className="flex min-h-full items-center justify-center p-4 text-center">
55+
<Transition.Child
56+
as={Fragment}
57+
enter="transition ease-in-out duration-300 transform"
58+
enterFrom="translate-x-full opacity-0"
59+
enterTo="translate-x-0 opacity-95"
60+
leave="transition ease-in duration-200 transform"
61+
leaveFrom="translate-x-0 opacity-95"
62+
leaveTo="translate-x-full opacity-0"
6963
>
70-
{link.title}
71-
</Link>
64+
<Dialog.Panel className="fixed left-0 top-0 z-10 h-full w-full bg-white opacity-95 duration-300 dark:bg-gray-950 dark:opacity-[0.98]">
65+
<nav className="fixed mt-8 h-full text-left">
66+
{headerNavLinks.map((link) => (
67+
<div key={link.title} className="px-12 py-4">
68+
<Link
69+
href={link.href}
70+
className="text-2xl font-bold tracking-widest text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
71+
onClick={onToggleNav}
72+
>
73+
{link.title}
74+
</Link>
75+
</div>
76+
))}
77+
</nav>
78+
79+
<div className="flex justify-end">
80+
<button
81+
className="mr-8 mt-11 h-8 w-8"
82+
aria-label="Toggle Menu"
83+
onClick={onToggleNav}
84+
>
85+
<svg
86+
xmlns="http://www.w3.org/2000/svg"
87+
viewBox="0 0 20 20"
88+
fill="currentColor"
89+
className="text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
90+
>
91+
<path
92+
fillRule="evenodd"
93+
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
94+
clipRule="evenodd"
95+
/>
96+
</svg>
97+
</button>
98+
</div>
99+
</Dialog.Panel>
100+
</Transition.Child>
72101
</div>
73-
))}
74-
</nav>
75-
</div>
102+
</div>
103+
</Dialog>
104+
</Transition>
76105
</>
77106
)
78107
}

0 commit comments

Comments
 (0)