Complete nav rewrite: native details/summary, zero JS state, zero transforms
Local Build and Deploy / deploy (push) Successful in 1m5s
Local Build and Deploy / deploy (push) Successful in 1m5s
- Mobile nav: uses <details>/<summary> for toggle, no useState, no useEffect - Nav dropdown: uses <details>/<summary>, no useState, no event listeners - Desktop: separate div with desktop nav items (hidden on mobile) - Mobile: hamburger dropdown with absolute positioned panel - CSS: removed ::after pseudo-element with transform, replaced hover with background-color only, added details[open] CSS rules - No backdrop-filter, no will-change, no transition-all, no GPU hacks - Works on every device without JS state management
This commit is contained in:
1 parent
5978b3c13d
commit
48a5394204
4 files changed
+194
-140
No files matched your search
@@ -1,7 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import Image from "next/image";
|
||||
import { type ReactNode, useEffect, useRef, useState } from "react";
|
||||
import { type ReactNode } from "react";
|
||||
|
||||
interface NavDropdownProps {
|
||||
label: string;
|
||||
@@ -10,36 +8,10 @@ interface NavDropdownProps {
|
||||
}
|
||||
|
||||
export function NavDropdown({ label, icon, children }: NavDropdownProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const onClick = (e: MouseEvent) => {
|
||||
if (ref.current && !ref.current.contains(e.target as Node))
|
||||
setOpen(false);
|
||||
};
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") setOpen(false);
|
||||
};
|
||||
document.addEventListener("mousedown", onClick);
|
||||
document.addEventListener("keydown", onKey);
|
||||
return () => {
|
||||
document.removeEventListener("mousedown", onClick);
|
||||
document.removeEventListener("keydown", onKey);
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative md:flex">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") setOpen((v) => !v);
|
||||
}}
|
||||
className="nav-item cursor-pointer"
|
||||
aria-expanded={open}
|
||||
<details className="relative">
|
||||
<summary
|
||||
className="nav-item cursor-pointer [&::-webkit-details-marker]:hidden"
|
||||
aria-haspopup="true"
|
||||
>
|
||||
{icon ? (
|
||||
@@ -54,7 +26,7 @@ export function NavDropdown({ label, icon, children }: NavDropdownProps) {
|
||||
) : null}
|
||||
{label}
|
||||
<svg
|
||||
className={`ml-auto md:ml-1 w-3 h-3 ${open ? "rotate-180" : ""}`}
|
||||
className="ml-auto md:ml-1 w-3 h-3 details-open:rotate-180"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
@@ -66,16 +38,13 @@ export function NavDropdown({ label, icon, children }: NavDropdownProps) {
|
||||
d="M19 9l-7 7-7-7"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
{open && (
|
||||
<div
|
||||
className="dropdown-menu md:absolute left-0 top-full mt-0.5 min-w-0 md:min-w-[200px] rounded-xl z-50 py-2"
|
||||
role="menu"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</summary>
|
||||
<div
|
||||
className="dropdown-menu md:absolute left-0 top-full mt-0.5 min-w-0 md:min-w-[200px] rounded-xl z-50 py-2"
|
||||
role="menu"
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</details>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user