Skip to content

Commit f8e42bd

Browse files
committed
migrate nav to tailwind and new components
1 parent 6eed7bc commit f8e42bd

File tree

4 files changed

+62
-100
lines changed

4 files changed

+62
-100
lines changed

src/components/Nav/Desktop/index.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { useRouter } from "next/router"
33
import { useTranslation } from "next-i18next"
44
import { BsTranslate } from "react-icons/bs"
55
import { MdBrightness2, MdWbSunny } from "react-icons/md"
6-
import { HStack, useColorModeValue, useEventListener } from "@chakra-ui/react"
76

8-
import { IconButton } from "@/components/Buttons"
97
import LanguagePicker from "@/components/LanguagePicker"
108
import { Button } from "@/components/ui/buttons/Button"
9+
import { HStack } from "@/components/ui/flex"
1110

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

13+
import useColorModeValue from "@/hooks/useColorModeValue"
14+
import { useEventListener } from "@/hooks/useEventListener"
15+
1416
type DesktopNavMenuProps = {
1517
toggleColorMode: () => void
1618
}
@@ -20,20 +22,12 @@ const DesktopNavMenu = ({ toggleColorMode }: DesktopNavMenuProps) => {
2022
const { locale } = useRouter()
2123
const languagePickerRef = useRef<HTMLButtonElement>(null)
2224

23-
const ThemeIcon = useColorModeValue(<MdBrightness2 />, <MdWbSunny />)
25+
const ThemeIcon = useColorModeValue(MdBrightness2, MdWbSunny)
2426
const themeIconAriaLabel = useColorModeValue(
2527
"Switch to Dark Theme",
2628
"Switch to Light Theme"
2729
)
2830

29-
const desktopHoverFocusStyles = {
30-
"& > svg": {
31-
transform: "rotate(10deg)",
32-
color: "primary.hover",
33-
transition: "transform 0.5s, color 0.2s",
34-
},
35-
}
36-
3731
/**
3832
* Adds a keydown event listener to toggle color mode (ctrl|cmd + \)
3933
* or open the language picker (\).
@@ -52,17 +46,16 @@ const DesktopNavMenu = ({ toggleColorMode }: DesktopNavMenuProps) => {
5246
})
5347

5448
return (
55-
<HStack hideBelow="md" gap="0">
56-
<IconButton
57-
icon={ThemeIcon}
49+
<HStack className="hidden gap-0 md:flex">
50+
<Button
5851
aria-label={themeIconAriaLabel}
5952
variant="ghost"
6053
isSecondary
61-
px={{ base: "2", xl: "3" }}
62-
_hover={desktopHoverFocusStyles}
63-
_focus={desktopHoverFocusStyles}
54+
className="px-2 xl:px-3 [&>svg]:transition-all [&>svg]:duration-500 [&>svg]:hover:rotate-12 [&>svg]:hover:text-primary-hover"
6455
onClick={toggleColorMode}
65-
/>
56+
>
57+
<ThemeIcon />
58+
</Button>
6659

6760
{/* Locale-picker menu */}
6861
<LanguagePicker>

src/components/Nav/index.tsx

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { lazy, Suspense, useRef } from "react"
22
import { useTranslation } from "next-i18next"
3-
import { Box, Flex, Hide, Show } from "@chakra-ui/react"
3+
import { MdSearch } from "react-icons/md"
44

55
import { EthHomeIcon } from "@/components/icons"
6-
import { BaseLink } from "@/components/Link"
76
import Search from "@/components/Search"
87

98
import { isDesktop } from "@/lib/utils/isDesktop"
109

11-
import { NAV_PY } from "@/lib/constants"
10+
import SearchButton from "../Search/SearchButton"
11+
import { Button } from "../ui/buttons/Button"
12+
import { BaseLink } from "../ui/Link"
1213

1314
import DesktopNavMenu from "./Desktop"
1415
import Menu from "./Menu"
@@ -29,73 +30,63 @@ const Nav = () => {
2930
const isDesktopFlag = isDesktop()
3031

3132
return (
32-
<Box position="sticky" top={0} zIndex="sticky" width="full">
33-
<Flex
33+
<div className="sticky top-0 z-sticky w-full">
34+
<nav
3435
ref={navWrapperRef}
35-
as="nav"
36+
className="flex h-19 justify-center border-b border-background-highlight bg-background px-4 py-4 xl:px-8"
3637
aria-label={t("nav-primary")}
37-
bg="background.base"
38-
borderBottom="1px"
39-
borderColor="rgba(0, 0, 0, 0.1)"
40-
height="4.75rem"
41-
justifyContent="center"
42-
py={NAV_PY}
43-
px={{ base: 4, xl: 8 }}
4438
>
45-
<Flex
46-
alignItems={{ base: "center", md: "normal" }}
47-
justifyContent={{ base: "space-between", md: "normal" }}
48-
width="full"
49-
maxW="container.2xl"
50-
>
39+
<div className="flex w-full max-w-screen-2xl items-center justify-between md:items-stretch md:justify-normal">
5140
<BaseLink
5241
href="/"
5342
aria-label={t("home")}
54-
display="inline-flex"
55-
alignItems="center"
56-
textDecor="none"
43+
className="inline-flex items-center no-underline"
5744
>
5845
<EthHomeIcon opacity={0.85} _hover={{ opacity: 1 }} />
5946
</BaseLink>
6047
{/* Desktop */}
61-
<Flex
62-
w="full"
63-
justifyContent={{ base: "flex-end", md: "space-between" }}
64-
ms={{ base: 3, xl: 8 }}
65-
>
48+
<div className="ms-3 flex w-full justify-end md:justify-between xl:ms-8">
6649
{/* avoid rendering desktop Menu version on mobile */}
67-
6850
{isClient && isDesktopFlag ? (
6951
<Menu className="hidden md:block" sections={linkSections} />
7052
) : (
71-
<Box />
53+
<div />
7254
)}
7355

74-
<Flex alignItems="center" /* justifyContent="space-between" */>
56+
<div className="flex items-center">
7557
{/* Desktop */}
76-
{/* avoid rendering desktop menu version on mobile */}
77-
<Show above="md">
78-
<Search {...searchModalDisclosure} />
58+
<div className="hidden md:flex">
59+
<Search {...searchModalDisclosure}>
60+
<SearchButton />
61+
</Search>
7962
<DesktopNavMenu toggleColorMode={toggleColorMode} />
80-
</Show>
63+
</div>
8164

82-
<Hide above="md">
65+
<div className="flex md:hidden">
8366
{/* Mobile */}
8467
{/* use Suspense to display the Search & the Menu at the same time */}
8568
<Suspense>
86-
<Search {...searchModalDisclosure} />
69+
<Search {...searchModalDisclosure}>
70+
<Button
71+
className="px-2 transition-transform duration-200 ease-in-out hover:rotate-6 hover:text-primary"
72+
variant="ghost"
73+
isSecondary
74+
>
75+
<MdSearch />
76+
</Button>
77+
</Search>
8778
<MobileNavMenu
8879
toggleColorMode={toggleColorMode}
8980
linkSections={linkSections}
9081
toggleSearch={searchModalDisclosure.onOpen}
9182
/>
9283
</Suspense>
93-
</Hide>
94-
</Flex>
95-
</Flex>
96-
</Flex>
97-
</Flex>
98-
</Box>
84+
</div>
85+
</div>
86+
</div>
87+
</div>
88+
</nav>
89+
</div>
9990
)
10091
}
10192

src/components/Search/SearchButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react"
2-
import { useTranslation } from "react-i18next"
2+
import { useTranslation } from "next-i18next"
33
import { DocSearchButton } from "@docsearch/react"
44

55
import { Button, type ButtonProps } from "../ui/buttons/Button"

src/components/Search/index.tsx

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@ import { forwardRef, useRef } from "react"
22
import dynamic from "next/dynamic"
33
import { useRouter } from "next/router"
44
import { useTranslation } from "next-i18next"
5-
import { MdSearch } from "react-icons/md"
65
import { useDocSearchKeyboardEvents } from "@docsearch/react"
76
import { DocSearchHit } from "@docsearch/react/dist/esm/types"
87
import { useComposedRefs } from "@radix-ui/react-compose-refs"
98
import * as Portal from "@radix-ui/react-portal"
9+
import { Slot } from "@radix-ui/react-slot"
1010

1111
import { trackCustomEvent } from "@/lib/utils/matomo"
1212
import { sanitizeHitTitle } from "@/lib/utils/sanitizeHitTitle"
1313
import { sanitizeHitUrl } from "@/lib/utils/url"
1414

15-
import { Button } from "../ui/buttons/Button"
16-
17-
import SearchButton from "./SearchButton"
18-
1915
import { type useDisclosure } from "@/hooks/useDisclosure"
2016

2117
const SearchModal = dynamic(() => import("./SearchModal"))
2218

23-
type Props = ReturnType<typeof useDisclosure>
19+
type Props = ReturnType<typeof useDisclosure> & { children?: React.ReactNode }
2420

2521
const Search = forwardRef<HTMLButtonElement, Props>(
26-
({ isOpen, onOpen, onClose }, ref) => {
22+
({ isOpen, onOpen, onClose, children }, ref) => {
2723
const { locale } = useRouter()
2824
const searchButtonRef = useRef<HTMLButtonElement>(null)
2925
const mergedButtonRefs = useComposedRefs(ref, searchButtonRef)
@@ -43,38 +39,20 @@ const Search = forwardRef<HTMLButtonElement, Props>(
4339

4440
return (
4541
<>
46-
<div className="hidden xl:block">
47-
<SearchButton
48-
ref={mergedButtonRefs}
49-
onClick={() => {
50-
onOpen()
51-
trackCustomEvent({
52-
eventCategory: "nav bar",
53-
eventAction: "click",
54-
eventName: "search open",
55-
})
56-
}}
57-
/>
58-
</div>
59-
<div className="block xl:hidden">
60-
<Button
61-
ref={mergedButtonRefs}
62-
className="px-2 transition-transform duration-200 ease-in-out hover:rotate-6 hover:text-primary"
63-
variant="ghost"
64-
isSecondary
65-
onClick={() => {
66-
onOpen()
67-
trackCustomEvent({
68-
eventCategory: "nav bar",
69-
eventAction: "click",
70-
eventName: "search open",
71-
})
72-
}}
73-
aria-label={t("aria-toggle-search-button")}
74-
>
75-
<MdSearch />
76-
</Button>
77-
</div>
42+
<Slot
43+
ref={mergedButtonRefs}
44+
aria-label={t("aria-toggle-search-button")}
45+
onClick={() => {
46+
onOpen()
47+
trackCustomEvent({
48+
eventCategory: "nav bar",
49+
eventAction: "click",
50+
eventName: "search open",
51+
})
52+
}}
53+
>
54+
{children}
55+
</Slot>
7856
<Portal.Root>
7957
{isOpen && (
8058
<SearchModal

0 commit comments

Comments
 (0)