Skip to content

Commit 8ddcfbc

Browse files
committed
feat: morpher click opens lang picker
mobile + desktop
1 parent e5aaf3a commit 8ddcfbc

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

src/components/Morpher.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { useEffect, useState } from "react"
2+
import { useBreakpointValue } from "@chakra-ui/react"
23

34
import { Button } from "@/components/Buttons"
45

6+
import {
7+
DESKTOP_LANGUAGE_BUTTON_NAME,
8+
HAMBURGER_BUTTON_ID,
9+
MOBILE_LANGUAGE_BUTTON_NAME,
10+
} from "@/lib/constants"
11+
512
const Morpher = () => {
613
const [state, setState] = useState({
714
text: "Ethereum",
@@ -118,11 +125,34 @@ const Morpher = () => {
118125
// eslint-disable-next-line react-hooks/exhaustive-deps
119126
}, [])
120127

121-
const clickLanguagePicker = () => {
128+
const handleMobileClick = () => {
129+
if (!document) return
130+
;(document.getElementById(HAMBURGER_BUTTON_ID) as HTMLButtonElement).click()
131+
setTimeout(
132+
() =>
133+
(
134+
document.querySelector(
135+
`button[name="${MOBILE_LANGUAGE_BUTTON_NAME}"`
136+
) as HTMLButtonElement
137+
).click(),
138+
1
139+
)
140+
}
141+
const handleDesktopClick = () => {
122142
if (!document) return
123-
;(document.querySelector("[id*=menu-button]") as HTMLButtonElement).click()
143+
;(
144+
document.querySelector(
145+
`button[name="${DESKTOP_LANGUAGE_BUTTON_NAME}"`
146+
) as HTMLButtonElement
147+
).click()
124148
}
125149

150+
const handleClick =
151+
useBreakpointValue({
152+
base: handleMobileClick,
153+
md: handleDesktopClick,
154+
}) || handleDesktopClick
155+
126156
return (
127157
<Button
128158
w="fit-content"
@@ -132,7 +162,7 @@ const Morpher = () => {
132162
fontSize="md"
133163
color="body.medium"
134164
_hover={{ color: "primary.base" }}
135-
onClick={clickLanguagePicker}
165+
onClick={handleClick}
136166
>
137167
{state.text}
138168
</Button>

src/components/Nav/Mobile/HamburgerButton.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { motion } from "framer-motion"
22
import { useTranslation } from "next-i18next"
33
import { type ButtonProps, Icon, IconButton } from "@chakra-ui/react"
44

5+
import { HAMBURGER_BUTTON_ID } from "@/lib/constants"
6+
57
const hamburgerSvg =
68
"M 2 13 l 10 0 l 0 0 l 10 0 M 4 19 l 8 0 M 12 19 l 8 0 M 2 25 l 10 0 l 0 0 l 10 0"
79
const glyphSvg =
@@ -26,6 +28,7 @@ const HamburgerButton = ({
2628

2729
return (
2830
<IconButton
31+
id={HAMBURGER_BUTTON_ID}
2932
onClick={onToggle}
3033
aria-label={t("aria-toggle-search-button")}
3134
variant="ghost"

src/components/Nav/Mobile/MenuFooter.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import LanguagePicker from "@/components/LanguagePicker"
1212

1313
import FooterButton from "./FooterButton"
1414
import FooterItemText from "./FooterItemText"
15+
import { MOBILE_LANGUAGE_BUTTON_NAME } from "@/lib/constants"
1516

1617
type MenuFooterProps = {
1718
onToggle: () => void
@@ -67,7 +68,7 @@ const MenuFooter = ({
6768
opacity: 0.4,
6869
}} // TODO: Replace with overlay component
6970
>
70-
<MenuButton as={FooterButton} icon={BsTranslate}>
71+
<MenuButton as={FooterButton} icon={BsTranslate} name={MOBILE_LANGUAGE_BUTTON_NAME}>
7172
<FooterItemText>{t("languages")}</FooterItemText>
7273
</MenuButton>
7374
</LanguagePicker>

src/components/Nav/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import LanguagePicker from "@/components/LanguagePicker"
2222
import { BaseLink } from "@/components/Link"
2323
import Search from "@/components/Search"
2424

25-
import { NAV_PY } from "@/lib/constants"
25+
import { DESKTOP_LANGUAGE_BUTTON_NAME, NAV_PY } from "@/lib/constants"
2626

2727
import Menu from "./Menu"
2828
import MobileNavMenu from "./Mobile"
@@ -126,6 +126,7 @@ const Nav = () => {
126126
>
127127
<MenuButton
128128
as={Button}
129+
name={DESKTOP_LANGUAGE_BUTTON_NAME}
129130
ref={languagePickerRef}
130131
variant="ghost"
131132
color="body.base"

src/lib/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ export const SECTION_LABELS: NavSectionKey[] = [
9292
export const DEFAULT_GLOSSARY_NS = "glossary"
9393

9494
export const NAV_PY = 4
95+
96+
export const HAMBURGER_BUTTON_ID = "mobile-menu-button"
97+
export const MOBILE_LANGUAGE_BUTTON_NAME = "mobile-language-button"
98+
export const DESKTOP_LANGUAGE_BUTTON_NAME = "desktop-language-button"

0 commit comments

Comments
 (0)