jackmerrill.com/components/Navbar.tsx

120 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-10-04 16:38:50 -07:00
import { useState, useEffect } from 'react'
import { Disclosure } from '@headlessui/react'
import { MenuIcon, XIcon } from '@heroicons/react/outline'
2021-03-30 22:27:36 -07:00
2021-09-03 10:13:30 -07:00
import Logo from '../components/Images/Logo';
2021-03-30 22:27:36 -07:00
2021-09-03 10:13:30 -07:00
const navigation = [
{ name: 'Home', href: '#', current: true },
2021-09-07 10:14:50 -07:00
{ name: 'About', href: '#about', current: false },
{ name: 'Projects', href: '#projects', current: false },
{ name: 'Graphics', href: '#graphics', current: false },
{ name: 'Contact', href: '#contact', current: false },
2021-09-03 10:13:30 -07:00
];
2021-09-07 10:14:50 -07:00
function classNames(...classes: any[]) {
2021-09-03 10:13:30 -07:00
return classes.filter(Boolean).join(' ')
}
const Navbar = ({ views }: { views: { view: string, viewing: boolean }[] }) => {
2021-03-30 22:27:36 -07:00
const [atTop, setAtTop] = useState<boolean>()
2021-10-04 15:13:53 -07:00
function updateCurrent() {
const currentlyViewing = views.find((view) => view.viewing === true)
navigation.forEach((nav) => nav.current = false)
if (currentlyViewing) {
const nav = navigation.find((nav) => nav.name === currentlyViewing.view)
if (!nav) return
nav.current = true
}
}
2021-03-30 22:27:36 -07:00
useEffect(() => {
if (window.scrollY === 0) {
setAtTop(true)
} else {
setAtTop(false)
}
window.addEventListener("scroll", () => {
if (window.scrollY === 0) {
setAtTop(true)
} else {
setAtTop(false)
}
2021-09-03 10:13:30 -07:00
2021-10-04 15:13:53 -07:00
updateCurrent()
2021-03-30 22:27:36 -07:00
})
})
2021-09-03 10:13:30 -07:00
return (
2021-10-04 15:13:53 -07:00
<Disclosure as="nav" className={`fixed w-full z-50 bg-black border-b-2 border-white ${atTop ? '' : 'shadow-lg'}`}>
2021-09-03 10:13:30 -07:00
{({ open }) => (
<>
2021-10-04 16:38:50 -07:00
<div className="px-4 mx-auto max-w-7xl sm:px-6 lg:px-8">
2021-09-03 10:13:30 -07:00
<div className="flex items-center justify-between h-16">
<div className="flex items-center">
<div className="flex-shrink-0">
2021-10-04 16:38:50 -07:00
<Logo className="w-8 h-8" />
2021-03-30 22:27:36 -07:00
</div>
2021-09-03 10:13:30 -07:00
<div className="hidden md:block">
2021-10-04 16:38:50 -07:00
<div className="flex items-baseline ml-10 space-x-4">
2021-09-03 10:13:30 -07:00
{navigation.map((item) => (
<a
key={item.name}
href={item.href}
2021-09-07 10:14:50 -07:00
id={item.name + item.href}
2021-10-04 16:38:50 -07:00
onClick={() => {
2021-10-04 15:13:53 -07:00
updateCurrent()
2021-09-07 10:14:50 -07:00
}}
2021-09-03 10:13:30 -07:00
className={classNames(
item.current
2021-10-04 15:13:53 -07:00
? 'border-2 border-white text-white'
: 'text-gray-300 hover:border-gray-300 hover:border-2 hover:text-white',
'px-3 py-2 rounded-md text-sm font-medium transition-border duration-75'
2021-09-03 10:13:30 -07:00
)}
aria-current={item.current ? 'page' : undefined}
>
{item.name}
</a>
))}
2021-03-30 22:27:36 -07:00
</div>
2021-09-03 10:13:30 -07:00
</div>
</div>
2021-10-04 16:38:50 -07:00
<div className="flex -mr-2 md:hidden">
2021-09-03 10:13:30 -07:00
{/* Mobile menu button */}
2021-10-04 16:38:50 -07:00
<Disclosure.Button className="inline-flex items-center justify-center p-2 text-gray-400 bg-gray-800 rounded-md hover:text-white hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white">
2021-09-03 10:13:30 -07:00
<span className="sr-only">Open main menu</span>
{open ? (
2021-10-04 16:38:50 -07:00
<XIcon className="block w-6 h-6" aria-hidden="true" />
2021-09-03 10:13:30 -07:00
) : (
2021-10-04 16:38:50 -07:00
<MenuIcon className="block w-6 h-6" aria-hidden="true" />
2021-09-03 10:13:30 -07:00
)}
</Disclosure.Button>
</div>
</div>
2021-03-30 22:27:36 -07:00
</div>
2021-09-03 10:13:30 -07:00
<Disclosure.Panel className="md:hidden">
<div className="px-2 pt-2 pb-3 space-y-1 sm:px-3">
{navigation.map((item) => (
<a
key={item.name}
href={item.href}
className={classNames(
item.current ? 'bg-gray-900 text-white' : 'text-gray-300 hover:bg-gray-700 hover:text-white',
'block px-3 py-2 rounded-md text-base font-medium'
)}
aria-current={item.current ? 'page' : undefined}
>
{item.name}
</a>
))}
</div>
</Disclosure.Panel>
</>
)}
</Disclosure>
)
2021-03-30 22:27:36 -07:00
}
export default Navbar;