Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadcn migration - desktop nav menu & search modal fixes #13647

Merged
merged 16 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"reading-time": "^1.5.0",
"remark-gfm": "^3.0.1",
"tailwind-merge": "^2.3.0",
"tailwind-variants": "^0.2.1",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^3.1.0",
"yaml-loader": "^0.8.0"
Expand Down
29 changes: 11 additions & 18 deletions src/components/Nav/Desktop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { useRouter } from "next/router"
import { useTranslation } from "next-i18next"
import { BsTranslate } from "react-icons/bs"
import { MdBrightness2, MdWbSunny } from "react-icons/md"
import { HStack, useColorModeValue, useEventListener } from "@chakra-ui/react"

import { IconButton } from "@/components/Buttons"
import LanguagePicker from "@/components/LanguagePicker"
import { Button } from "@/components/ui/buttons/Button"
import { HStack } from "@/components/ui/flex"

import { DESKTOP_LANGUAGE_BUTTON_NAME } from "@/lib/constants"

import useColorModeValue from "@/hooks/useColorModeValue"
import { useEventListener } from "@/hooks/useEventListener"

type DesktopNavMenuProps = {
toggleColorMode: () => void
}
Expand All @@ -20,20 +22,12 @@ const DesktopNavMenu = ({ toggleColorMode }: DesktopNavMenuProps) => {
const { locale } = useRouter()
const languagePickerRef = useRef<HTMLButtonElement>(null)

const ThemeIcon = useColorModeValue(<MdBrightness2 />, <MdWbSunny />)
const ThemeIcon = useColorModeValue(MdBrightness2, MdWbSunny)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all against this, but just noting an alternative way this can be done as we migrate to TW:

<MdBrightness2 className="dark:hidden" />
<MdWbSunny className="not-[dark]:hidden" />

Can see where both approaches could come in handy, not sure I have a strong preference or desire to standardize it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! I haven't thought about that option, I like the no-js dependency. I'll test it out and let you know.

const themeIconAriaLabel = useColorModeValue(
"Switch to Dark Theme",
"Switch to Light Theme"
)

const desktopHoverFocusStyles = {
"& > svg": {
transform: "rotate(10deg)",
color: "primary.hover",
transition: "transform 0.5s, color 0.2s",
},
}

/**
* Adds a keydown event listener to toggle color mode (ctrl|cmd + \)
* or open the language picker (\).
Expand All @@ -52,17 +46,16 @@ const DesktopNavMenu = ({ toggleColorMode }: DesktopNavMenuProps) => {
})

return (
<HStack hideBelow="md" gap="0">
<IconButton
icon={ThemeIcon}
<HStack className="hidden gap-0 md:flex">
<Button
aria-label={themeIconAriaLabel}
variant="ghost"
isSecondary
px={{ base: "2", xl: "3" }}
_hover={desktopHoverFocusStyles}
_focus={desktopHoverFocusStyles}
className="px-2 xl:px-3 [&>svg]:transition-all [&>svg]:duration-500 [&>svg]:hover:rotate-12 [&>svg]:hover:text-primary-hover"
onClick={toggleColorMode}
/>
>
<ThemeIcon />
</Button>

{/* Locale-picker menu */}
<LanguagePicker>
Expand Down
52 changes: 39 additions & 13 deletions src/components/Nav/Menu/MenuContent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import { motion } from "framer-motion"
import { Box } from "@chakra-ui/react"
import { tv } from "tailwind-variants"
import { Content } from "@radix-ui/react-navigation-menu"

import { cn } from "@/lib/utils/cn"

import { NavItem, NavSections } from "../types"

import SubMenu from "./SubMenu"
import { useNavMenu } from "./useNavMenu"

export const navMenu = tv({
slots: {
base: "text-body",
item: "w-full relative -me-4 py-4 hover:text-menu-active [&:hover_p]:text-menu-active focus-visible:text-menu-active [&:focus-visible_p]:text-menu-active hover:outline-0 hover:rounded-md hover:shadow-none focus-visible:outline-0 focus-visible:rounded-md focus-visible:shadow-none",
submenu: "grid h-full w-full grid-cols-1",
},
variants: {
level: {
1: {
submenu: "grid-cols-3 bg-menu-1-background",
item: "data-[active=true]:bg-menu-1-active-background hover:bg-menu-1-active-background focus-visible:bg-menu-1-active-background",
},
2: {
submenu: "grid-cols-2 bg-menu-2-background",
item: "hover:bg-menu-2-active-background focus-visible:bg-menu-2-active-background data-[active=true]:bg-menu-2-active-background",
},
3: {
submenu: "grid-cols-1 bg-menu-3-background",
item: "data-[active=true]:bg-menu-3-active-background hover:bg-menu-3-active-background",
},
4: {
submenu: "grid-cols-1 bg-menu-4-background",
item: "data-[active=true]:bg-menu-4-active-background hover:bg-menu-4-active-background",
},
},
},
})

type MenuContentProps = {
items: NavItem[]
isOpen: boolean
Expand All @@ -15,31 +45,27 @@ type MenuContentProps = {

// Desktop Menu content
const MenuContent = ({ items, isOpen, sections }: MenuContentProps) => {
const { activeSection, containerVariants, menuColors, onClose } =
useNavMenu(sections)
const { activeSection, containerVariants, onClose } = useNavMenu(sections)
const { base } = navMenu()

return (
<Content asChild>
<Box
as={motion.div}
<motion.div
className={cn(
"absolute inset-x-0 top-19 border border-body-light shadow-md",
base()
)}
variants={containerVariants}
initial={false}
animate={isOpen ? "open" : "closed"}
position="absolute"
top="19"
insetInline="0"
shadow="md"
border="1px"
borderColor={menuColors.stroke}
bg={menuColors.lvl[1].background}
>
<SubMenu
lvl={1}
items={items}
activeSection={activeSection}
onClose={onClose}
/>
</Box>
</motion.div>
</Content>
)
}
Expand Down
114 changes: 48 additions & 66 deletions src/components/Nav/Menu/SubMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { AnimatePresence, motion } from "framer-motion"
import NextLink from "next/link"
import {
Box,
Button,
Grid,
Icon,
ListItem,
UnorderedList,
} from "@chakra-ui/react"
import {
Content,
Item,
Expand All @@ -18,15 +10,18 @@ import {
Viewport,
} from "@radix-ui/react-navigation-menu"

import { ButtonProps } from "@/components/Buttons"
import Link from "@/components/Link"
import { Button } from "@/components/ui/buttons/Button"
import { BaseLink } from "@/components/ui/Link"
import { ListItem, UnorderedList } from "@/components/ui/list"

import { cn } from "@/lib/utils/cn"
import { trackCustomEvent } from "@/lib/utils/matomo"
import { cleanPath } from "@/lib/utils/url"

import type { Level, NavItem, NavSectionKey } from "../types"

import ItemContent from "./ItemContent"
import { navMenu } from "./MenuContent"
import NextChevron from "./NextChevron"
import { useSubMenu } from "./useSubMenu"

Expand All @@ -47,77 +42,56 @@ type LvlContentProps = {
* @returns The JSX element representing the menu content.
*/
const SubMenu = ({ lvl, items, activeSection, onClose }: LvlContentProps) => {
const { asPath, locale, menuColors, menuVariants, PADDING } = useSubMenu()
const { asPath, locale, menuVariants } = useSubMenu()
const { submenu, item: itemClasses } = navMenu({ level: lvl })

if (lvl > 3) return null

const templateColumns = `repeat(${4 - lvl}, 1fr)`

return (
<Sub orientation="vertical" asChild>
<AnimatePresence>
<Grid
as={motion.div}
<motion.div
className={submenu()}
variants={menuVariants}
initial="closed"
animate="open"
exit="closed"
w="full"
h="full"
gridTemplateColumns={templateColumns}
>
<List asChild>
<UnorderedList listStyleType="none" p={PADDING / 2} m="0">
<UnorderedList className="m-0 list-none p-2">
{items.map((item) => {
const { label, icon, ...action } = item
const { label, icon: Icon, ...action } = item
const subItems = action.items || []
const isLink = "href" in action
const isActivePage = isLink && cleanPath(asPath) === action.href
const activeStyles = {
outline: "none",
rounded: "md",
"p, svg": { color: menuColors.highlight },
bg: menuColors.lvl[lvl].activeBackground,
boxShadow: "none",
}
const buttonProps: ButtonProps = {
color: menuColors.body,
leftIcon: lvl === 1 && icon ? <Icon as={icon} /> : undefined,
rightIcon: isLink ? undefined : <NextChevron />,
position: "relative",
w: "full",
me: -PADDING,
sx: {
"span:first-of-type": { m: 0, me: 4 }, // Spacing for icon
},
py: PADDING,
bg: isActivePage
? menuColors.lvl[lvl].activeBackground
: "none",
_hover: activeStyles,
_focus: activeStyles,
variant: "ghost",
}

const buttonClasses = cn(
"no-underline text-body",
itemClasses()
)

return (
<Item key={label} asChild>
<ListItem
mb={PADDING / 2}
_last={{ mb: 0 }}
sx={{
'&:has(button[data-state="open"])': {
roundedStart: "md",
roundedEnd: "none",
bg: menuColors.lvl[lvl].activeBackground,
me: -PADDING,
pe: PADDING,
},
}}
className="mb-2 last:mb-0"
// TODO
// sx={{
// '&:has(button[data-state="open"])': {
// roundedStart: "md",
// roundedEnd: "none",
// bg: menuColors.lvl[lvl].activeBackground,
// me: -PADDING,
// pe: PADDING,
// },
// }}
>
{isLink ? (
<NextLink href={action.href!} passHref legacyBehavior>
<NavigationMenuLink asChild>
<Button
as={Link}
variant="ghost"
className={buttonClasses}
data-active={isActivePage}
onClick={() => {
onClose()
trackCustomEvent({
Expand All @@ -126,31 +100,39 @@ const SubMenu = ({ lvl, items, activeSection, onClose }: LvlContentProps) => {
eventName: action.href!,
})
}}
{...buttonProps}
asChild
>
<ItemContent item={item} lvl={lvl} />
<BaseLink>
{lvl === 1 && Icon ? (
<Icon className="me-4 h-6 w-6" />
) : null}

<ItemContent item={item} lvl={lvl} />
</BaseLink>
</Button>
</NavigationMenuLink>
</NextLink>
) : (
<>
<Trigger asChild>
<Button {...buttonProps}>
<Button variant="ghost" className={buttonClasses}>
{lvl === 1 && Icon ? (
<Icon className="me-4 h-6 w-6" />
) : null}

<ItemContent item={item} lvl={lvl} />
<NextChevron />
</Button>
</Trigger>
<Content asChild>
<Box
bg={menuColors.lvl[lvl + 1].background}
h="full"
>
<div className="h-full">
<SubMenu
lvl={(lvl + 1) as Level}
items={subItems}
activeSection={activeSection}
onClose={onClose}
/>
</Box>
</div>
</Content>
</>
)}
Expand All @@ -161,7 +143,7 @@ const SubMenu = ({ lvl, items, activeSection, onClose }: LvlContentProps) => {
</UnorderedList>
</List>
<Viewport style={{ gridColumn: "2/4" }} />
</Grid>
</motion.div>
</AnimatePresence>
</Sub>
)
Expand Down
5 changes: 1 addition & 4 deletions src/components/Nav/Menu/useNavMenu.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { useState } from "react"
import type { MotionProps } from "framer-motion"
import { useEventListener } from "@chakra-ui/react"

import { isModified } from "@/lib/utils/keyboard"

import { MAIN_NAV_ID, SECTION_LABELS } from "@/lib/constants"

import type { NavSectionKey, NavSections } from "../types"

import { useNavMenuColors } from "@/hooks/useNavMenuColors"
import { useEventListener } from "@/hooks/useEventListener"
import { useRtlFlip } from "@/hooks/useRtlFlip"

export const useNavMenu = (sections: NavSections) => {
const { direction } = useRtlFlip()
const menuColors = useNavMenuColors()
const [activeSection, setActiveSection] = useState<NavSectionKey | null>(null)

// Focus corresponding nav section when number keys pressed
Expand Down Expand Up @@ -72,7 +70,6 @@ export const useNavMenu = (sections: NavSections) => {
direction,
handleSectionChange,
isOpen,
menuColors,
onClose,
}
}
Loading
Loading