Skip to content

Commit 5778c93

Browse files
committed
chore: move onClick handlers to external functions to avoid re-creating them on each render
1 parent a53a8c0 commit 5778c93

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/components/LanguagePicker/index.tsx

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
useEventListener,
1717
} from "@chakra-ui/react"
1818

19+
import { LocaleDisplayInfo } from "@/lib/types"
20+
1921
import { Button } from "@/components/Buttons"
2022
import { BaseLink } from "@/components/Link"
2123

@@ -61,22 +63,38 @@ const LanguagePicker = ({
6163
inputRef.current?.focus()
6264
})
6365

64-
const MobileCloseBar = () => {
65-
return (
66-
<Flex
67-
justifyContent="end"
68-
hideFrom="md"
69-
position="sticky"
70-
zIndex="sticky"
71-
top="0"
72-
bg="background.base"
66+
// onClick handlers
67+
const handleMobileCloseBarClick = () => onClose()
68+
const handleMenuItemClose = (displayInfo: LocaleDisplayInfo) =>
69+
onClose({
70+
eventAction: "Locale chosen",
71+
eventName: displayInfo.localeOption,
72+
})
73+
const handleBaseLinkClose = () =>
74+
onClose({
75+
eventAction: "Translation program link (menu footer)",
76+
eventName: "/contributing/translation-program",
77+
})
78+
79+
const MobileCloseBar = () => (
80+
<Flex
81+
justifyContent="end"
82+
hideFrom="md"
83+
position="sticky"
84+
zIndex="sticky"
85+
top="0"
86+
bg="background.base"
87+
>
88+
<Button
89+
p="4"
90+
variant="ghost"
91+
alignSelf="end"
92+
onClick={handleMobileCloseBarClick}
7393
>
74-
<Button p="4" variant="ghost" alignSelf="end" onClick={() => onClose()}>
75-
{t("common:close")}
76-
</Button>
77-
</Flex>
78-
)
79-
}
94+
{t("common:close")}
95+
</Button>
96+
</Flex>
97+
)
8098

8199
return (
82100
<Menu isLazy placement={placement} autoSelect={false} {...disclosure}>
@@ -178,12 +196,7 @@ const LanguagePicker = ({
178196
e.preventDefault()
179197
inputRef.current?.focus()
180198
}}
181-
onClick={() =>
182-
onClose({
183-
eventAction: "Locale chosen",
184-
eventName: displayInfo.localeOption,
185-
})
186-
}
199+
onClick={() => handleMenuItemClose(displayInfo)}
187200
/>
188201
))}
189202

@@ -216,12 +229,7 @@ const LanguagePicker = ({
216229
<BaseLink
217230
ref={footerRef}
218231
href="/contributing/translation-program"
219-
onClick={() =>
220-
onClose({
221-
eventAction: "Translation program link (menu footer)",
222-
eventName: "/contributing/translation-program",
223-
})
224-
}
232+
onClick={handleBaseLinkClose}
225233
>
226234
{t("common:learn-more")}
227235
</BaseLink>

0 commit comments

Comments
 (0)