functionality for fixed navbar

This commit is contained in:
Atreya Choudhury 2024-07-25 01:52:13 -04:00
parent 947d3a0fa7
commit d1daf73bff
6 changed files with 33 additions and 13 deletions

View File

@ -98,13 +98,11 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<ThemeProviders>
<Analytics analyticsConfig={siteMetadata.analytics as AnalyticsConfig} />
<SectionContainer>
<div className="flex h-screen flex-col justify-between font-sans">
<SearchProvider searchConfig={siteMetadata.search as SearchConfig}>
<Header />
<main className="mb-auto">{children}</main>
</SearchProvider>
<Footer />
</div>
<SearchProvider searchConfig={siteMetadata.search as SearchConfig}>
<Header />
<main className="mb-auto">{children}</main>
</SearchProvider>
<Footer />
</SectionContainer>
</ThemeProviders>
</body>

View File

@ -7,8 +7,13 @@ import ThemeSwitch from './ThemeSwitch'
import SearchButton from './SearchButton'
const Header = () => {
let headerClass = "flex items-center bg-white dark:bg-gray-950 justify-between py-10"
if (siteMetadata.stickyNav){
headerClass += " sticky top-0"
}
return (
<header className="flex items-center justify-between py-10">
<header className={headerClass}>
<div>
<Link href="/" aria-label={siteMetadata.headerTitle}>
<div className="flex items-center justify-between">

View File

@ -1,25 +1,31 @@
'use client'
import { Dialog, Transition } from '@headlessui/react'
import { Fragment, useState } from 'react'
import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
import { Fragment, useState, useEffect, useRef } from 'react'
import Link from './Link'
import headerNavLinks from '@/data/headerNavLinks'
const MobileNav = () => {
const [navShow, setNavShow] = useState(false)
const navRef = useRef(null)
const onToggleNav = () => {
setNavShow((status) => {
if (status) {
document.body.style.overflow = 'auto'
enableBodyScroll(navRef.current)
} else {
// Prevent scrolling
document.body.style.overflow = 'hidden'
disableBodyScroll(navRef.current)
}
return !status
})
}
useEffect(() => {
return clearAllBodyScrollLocks
})
return (
<>
<button aria-label="Toggle Menu" onClick={onToggleNav} className="sm:hidden">
@ -37,7 +43,7 @@ const MobileNav = () => {
</svg>
</button>
<Transition appear show={navShow} as={Fragment}>
<Dialog as="div" className="relative z-10" onClose={onToggleNav}>
<Dialog as="div" ref={navRef} className="relative z-10" onClose={onToggleNav}>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"

View File

@ -21,6 +21,8 @@ const siteMetadata = {
threads: 'https://www.threads.net',
instagram: 'https://www.instagram.com',
locale: 'en-US',
// set to true if you want a navbar fixed to the top
stickyNav: false,
analytics: {
// If you want to use an analytics provider you have to add it to the
// content security policy in the `next.config.js` file.
@ -34,7 +36,7 @@ const siteMetadata = {
},
// plausibleAnalytics: {
// plausibleDataDomain: '', // e.g. tailwind-nextjs-starter-blog.vercel.app
// If you are hosting your own Plausible.
// If you are hosting your own Plausible.
// src: '', // e.g. https://plausible.my-domain.com/js/script.js
// },
// simpleAnalytics: {},

View File

@ -17,6 +17,7 @@
"@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.12",
"autoprefixer": "^10.4.13",
"body-scroll-lock": "^4.0.0-beta.0",
"contentlayer2": "0.4.6",
"esbuild": "0.20.2",
"github-slugger": "^2.0.0",

View File

@ -4002,6 +4002,13 @@ __metadata:
languageName: node
linkType: hard
"body-scroll-lock@npm:^4.0.0-beta.0":
version: 4.0.0-beta.0
resolution: "body-scroll-lock@npm:4.0.0-beta.0"
checksum: 61d40007fddf64ecc69e9e02ed9d96bb895f88d7da65cea7651081110225de48efa44ffc4acd376ed004788e242a9af12059fec728c096774b49365524ea6f46
languageName: node
linkType: hard
"boolbase@npm:^1.0.0":
version: 1.0.0
resolution: "boolbase@npm:1.0.0"
@ -10908,6 +10915,7 @@ __metadata:
"@typescript-eslint/eslint-plugin": ^6.1.0
"@typescript-eslint/parser": ^6.1.0
autoprefixer: ^10.4.13
body-scroll-lock: ^4.0.0-beta.0
contentlayer2: 0.4.6
cross-env: ^7.0.3
esbuild: 0.20.2