Skip to content

Commit

Permalink
refactor/sidebar-and-cookie
Browse files Browse the repository at this point in the history
refactor: fix cookie modal height, revert sidebar, separate context
  • Loading branch information
rikhall1515 authored May 11, 2024
2 parents 0b9e73a + 266c265 commit 0ab4f74
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 182 deletions.
4 changes: 2 additions & 2 deletions components/cookies/manage/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const Dialog = forwardRef<HTMLDialogElement, Props>(function Dialog({ children }
className={cn(
"hidden open:flex",
"flex-col items-center rounded-2xl text-foreground",
"fixed left-0 top-0 z-[99] box-border overflow-hidden md:left-1/2 md:top-1/2 md:-translate-x-1/2 md:-translate-y-1/2",
"h-auto max-h-[calc(100%-16px)] w-[calc(100%-16px)] max-w-[80rem] rounded-2xl bg-background pt-6 shadow transition-all md:min-h-[30rem]",
"fixed bottom-0 left-0 right-0 top-0 z-[99] box-border overflow-hidden md:bottom-[initial] md:left-1/2 md:right-[initial] md:top-1/2 md:-translate-x-1/2 md:-translate-y-1/2",
"m-auto h-full w-[calc(100%-16px)] max-w-[80rem] rounded-2xl bg-background pt-6 shadow transition-all md:h-[40rem] md:min-h-[30rem]",
"backdrop:fixed backdrop:bottom-[-100%] backdrop:left-[-100%] backdrop:right-[-100%] backdrop:top-[-100%] backdrop:z-40 backdrop:m-auto backdrop:h-[100vh] backdrop:w-full backdrop:overflow-hidden backdrop:bg-black backdrop:bg-opacity-50",
"backdrop:!bg-no-repeat backdrop:object-cover backdrop:backdrop-blur backdrop:backdrop-brightness-50"
)}
Expand Down
2 changes: 1 addition & 1 deletion components/cookies/manage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ManageConsent() {
<>
<ManageWrapper>
<ScrollArea className="flex h-full w-full flex-col items-center align-baseline">
<Logo className="mx-auto mb-6 flex-shrink-0" />
<Logo className="mx-auto mb-6 h-12 w-auto flex-shrink-0" />
<Tabs defaultValue="Consent" className="flex w-full flex-col items-center">
<TabsList className="mx-auto mb-6">
<TabsTrigger value="Consent">Consent</TabsTrigger>
Expand Down
22 changes: 7 additions & 15 deletions components/header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import BaseLogo from "@/components/logo";
import SidebarContextProvider from "@/context/sidebar";
import HeaderContextProvider from "@/context/header";
import { env } from "@/env/client";
import { cn } from "@/lib/utils";

import Nav from "./menu";
import Sidebar from "./sidebar";
import JumpToContent from "./util/jumpToContent";
import HeaderWrapper from "./util/wrapper";

export default function Header() {
return (
<>
<JumpToContent />
<SidebarContextProvider>
<header
className={cn(
"fixed left-0 right-0 top-0 z-10", //position
"h-[8rem] w-[100svw]", //size
"border-b-[1px] border-[transparent] bg-[initial]", //color
"transition-[height,border,background-color] duration-300", //transition
"h-[4.5rem] border-b border-border/40 bg-background/95 supports-[backdrop-filter]:bg-background/60",
"backdrop-blur"
)}
>
<HeaderContextProvider>
<HeaderWrapper>
<div
className={cn(
"flex items-center justify-between",
Expand All @@ -34,7 +26,7 @@ export default function Header() {
href="/"
className={cn(
"h-8",
"flex items-center gap-1",
"z-[11] flex items-center gap-1",
"text-base font-bold tracking-[0.2em]",
"text-foreground"
)}
Expand All @@ -47,8 +39,8 @@ export default function Header() {
</div>
<Sidebar />
</div>
</header>
</SidebarContextProvider>
</HeaderWrapper>
</HeaderContextProvider>
</>
);
}
28 changes: 17 additions & 11 deletions components/header/sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import SidebarContextProvider from "@/context/sidebar";
import MenuBtnRefContextProvider from "@/context/sidebar/btnRef";

import Nav from "./nav";
import TopBar from "./topbar";
import BackDrop from "./util/backdrop";
import InnerWrapper from "./util/innerWrapper";
import { MenuButton } from "./util/menuButton";
import MenuButton from "./util/menuButton";

export default function Sidebar() {
return (
<>
<div className="-mr-6 block h-[4.5rem] lg:hidden">
<MenuButton />

{/* <SwipeArea /> */}
<InnerWrapper>
<TopBar />
<Nav />
</InnerWrapper>
<BackDrop />
</div>
<SidebarContextProvider>
<MenuBtnRefContextProvider>
<div className="-mr-6 block h-[4.5rem] lg:hidden">
<MenuButton />
<BackDrop />
{/* <SwipeArea /> */}
<InnerWrapper>
<TopBar />
<Nav />
</InnerWrapper>
</div>
</MenuBtnRefContextProvider>
</SidebarContextProvider>
</>
);
}
8 changes: 7 additions & 1 deletion components/header/sidebar/nav/privacy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import Link from "next/link";
import { usePathname } from "next/navigation";

import { useSidebarContext } from "@/context/sidebar";
import { useMenuBtnRefContext } from "@/context/sidebar/btnRef";
import { cn } from "@/lib/utils";

export default function Privacy({ children }: { children?: React.ReactNode }) {
const pathname = usePathname();

const { isExpanded, toggle } = useSidebarContext();
const { trapFocus } = useMenuBtnRefContext();
return (
<>
<Link
Expand All @@ -17,6 +20,9 @@ export default function Privacy({ children }: { children?: React.ReactNode }) {
pathname === "/legal/privacy-policy" ? "bg-primary text-primary-foreground" : "",
"hover:bg-primary"
)}
tabIndex={isExpanded ? 0 : -1}
onKeyDown={trapFocus}
onClick={toggle}
>
{children}
</Link>
Expand Down
4 changes: 4 additions & 0 deletions components/header/sidebar/nav/sidebarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from "next/link";
import { usePathname } from "next/navigation";
import { memo } from "react";

import { useSidebarContext } from "@/context/sidebar";
import { cn } from "@/lib/utils";

export const SidebarItem = memo(function SidebarItem({
Expand All @@ -13,6 +14,7 @@ export const SidebarItem = memo(function SidebarItem({
children?: React.ReactNode;
}) {
const pathname = usePathname();
const { isExpanded, toggle } = useSidebarContext();
return (
<>
<Link
Expand All @@ -23,6 +25,8 @@ export const SidebarItem = memo(function SidebarItem({
pathname === href ? "bg-primary text-primary-foreground" : "",
"hover:bg-primary"
)}
tabIndex={isExpanded ? 0 : -1}
onClick={toggle}
>
{children}
</Link>
Expand Down
2 changes: 0 additions & 2 deletions components/header/sidebar/topbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cn } from "@/lib/utils";

import InnerMenuButton from "./menuButton";
import SearchButton from "./searchBtn";

export default function TopBar() {
Expand All @@ -15,7 +14,6 @@ export default function TopBar() {
)}
>
<SearchButton />
<InnerMenuButton />
</div>
</>
);
Expand Down
28 changes: 0 additions & 28 deletions components/header/sidebar/topbar/menuButton.tsx

This file was deleted.

11 changes: 5 additions & 6 deletions components/header/sidebar/util/backdrop.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"use client";
import { useHeaderAPIContext } from "@/context/sidebar";
import { useSidebarContext } from "@/context/sidebar";
import { cn } from "@/lib/utils";

export default function BackDrop() {
const { toggleSidebarDialog } = useHeaderAPIContext();
const { isExpanded, toggle } = useSidebarContext();
return (
<>
<div
className={cn(
"pointer-events-none opacity-0 peer-open/dialog:pointer-events-auto peer-open/dialog:opacity-100",
"fixed bottom-0 left-0 right-0 top-0 z-[10] h-[100vh] w-full bg-background/70 backdrop-brightness-50 transition-all duration-300 dark:backdrop-brightness-100",
"!bg-no-repeat object-cover"
"fixed bottom-0 left-0 right-0 top-0 z-[10] h-[100vh] w-full bg-background/50 backdrop-blur backdrop-brightness-100 transition-opacity duration-300",
isExpanded ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0"
)}
onClick={toggleSidebarDialog}
onClick={toggle}
></div>
</>
);
Expand Down
37 changes: 23 additions & 14 deletions components/header/sidebar/util/innerWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
"use client";
import { useState } from "react";

import { useHeaderAPIContext, useSidebarRefContext } from "@/context/sidebar";
import { useHeaderContext } from "@/context/header";
import { useSidebarContext } from "@/context/sidebar";
import { useMenuBtnRefContext } from "@/context/sidebar/btnRef";
import { cn } from "@/lib/utils";

export default function InnerWrapper({ children }: { children: React.ReactNode }) {
const { toggleSidebarDialog } = useHeaderAPIContext();
const { sidebarRef } = useSidebarRefContext();

const { isAtTop } = useHeaderContext();
const { isExpanded, toggle, sidebarRef } = useSidebarContext();
const { mainMenuBtnRef } = useMenuBtnRefContext();
const [touchStart, setTouchStart] = useState(0);
const [touchEnd, setTouchEnd] = useState(0);
//127 - 72 = 55 / 2 = 22 - 23
return (
<>
<dialog
<aside
className={cn(
"peer/dialog",
"fixed bottom-0 right-0 top-0 z-[29]", //34px 24 - 34 = 10 / 2 = 5
"h-[100svh] w-[100vw] transition-all duration-300 m:w-[20rem]",
"h-[100svh] w-[100vw] transition-all duration-300 md:w-[20rem]",
"outline-none",
"bg-background",
"open:pointer-events-auto open:translate-x-0 open:opacity-100",
"pointer-events-none translate-x-full opacity-0"
"bg-white",
isExpanded
? "pointer-events-auto translate-x-0 opacity-100"
: "pointer-events-none translate-x-full opacity-0",
isAtTop ? "pt-[1.625rem]" : ""
)}
aria-label="Sidebar navigation dialog"
aria-label="Sidebar navigation menu"
aria-hidden={!isExpanded}
onKeyDown={(e) => {
if (e.code === "Escape" && mainMenuBtnRef.current) {
toggle();
mainMenuBtnRef.current.focus();
}
}}
onTouchStart={(e) => {
setTouchStart(e.targetTouches[0].clientX);
setTouchEnd(e.targetTouches[0].clientX);
Expand All @@ -33,15 +43,14 @@ export default function InnerWrapper({ children }: { children: React.ReactNode }
}}
onTouchEnd={() => {
if (touchEnd - touchStart > 100) {
toggleSidebarDialog();
toggle();
}
setTouchStart(0);
}}
ref={sidebarRef}
id="sidebarMenuNav"
>
{children}
</dialog>
</aside>
</>
);
}
28 changes: 14 additions & 14 deletions components/header/sidebar/util/menuButton.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
"use client";
import { forwardRef } from "react";

import { useHeaderAPIContext } from "@/context/sidebar";
import { useSidebarContext } from "@/context/sidebar";
import { useMenuBtnRefContext } from "@/context/sidebar/btnRef";
import { cn } from "@/lib/utils";

type Props = JSX.IntrinsicElements["button"];

type Ref = HTMLButtonElement;
export const MenuButton = forwardRef<Ref, Props>(function MenuButton() {
const { toggleSidebarDialog } = useHeaderAPIContext();
export default function MenuButton() {
const { isExpanded, toggle } = useSidebarContext();
const { mainMenuBtnRef } = useMenuBtnRefContext();
return (
<>
<button
aria-expanded={false}
aria-label={"open menu"}
onClick={toggleSidebarDialog}
aria-expanded={isExpanded}
aria-label={isExpanded ? "Close menu" : "Open menu"}
onClick={toggle}
className={cn("relative z-[30] fill-[inherit] p-6")}
aria-haspopup="menu"
aria-controls="sidebarMenuNav"
onKeyDown={(e) => {
if (e.code === "Escape") toggle();
}}
ref={mainMenuBtnRef}
>
<div className={cn("menuBtn h-6 w-6")}>
<div className={cn("menuBtn h-6 w-6", isExpanded ? "opened" : "")}>
<span></span>
<span></span>
<span></span>
</div>
</button>
</>
);
});
}
36 changes: 36 additions & 0 deletions components/header/util/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";
import { useEffect } from "react";
import { useInView } from "react-intersection-observer";

import { useHeaderContext } from "@/context/header";
import { cn } from "@/lib/utils";

export default function HeaderWrapper({ children }: { children: React.ReactNode }) {
const { setIsAtTop } = useHeaderContext();
const { ref, inView } = useInView({
threshold: 0,
initialInView: true,
fallbackInView: false,
});

useEffect(() => {
setIsAtTop(inView);
}, [inView]);

Check warning on line 18 in components/header/util/wrapper.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'setIsAtTop'. Either include it or remove the dependency array
return (
<>
<div ref={ref} className="absolute left-0 right-0 top-0 h-[1px] w-full"></div>
<header
className={cn(
"fixed left-0 right-0 top-0 z-10", //position
"flex items-center justify-center", //display
"h-[8rem] w-full", //size
"border-b-[1px] border-[transparent] bg-[initial]", //color
"transition-all duration-300", //transition
inView === true || undefined || null ? "" : "scrolled"
)}
>
{children}
</header>
</>
);
}
Loading

0 comments on commit 0ab4f74

Please sign in to comment.