Skip to content

Commit 0769095

Browse files
authored
Merge pull request #203 from ethereum/rtl-locale-refresh
Html direction update on locale change
2 parents f1b16aa + 396b513 commit 0769095

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/hooks/useLocaleDirection.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useEffect } from "react"
2+
import { useRouter } from "next/router"
3+
4+
import { Lang } from "@/lib/types"
5+
6+
import { isLangRightToLeft } from "@/lib/utils/translations"
7+
8+
/**
9+
* Custom hook that sets the DOM direction based on the locale,
10+
* responding to changes in the locale without requiring refresh.
11+
*/
12+
export const useLocaleDirection = (): void => {
13+
const { locale } = useRouter()
14+
15+
useEffect(() => {
16+
const dir = isLangRightToLeft(locale as Lang) ? "rtl" : "ltr"
17+
document.documentElement.setAttribute("dir", dir)
18+
}, [locale])
19+
}

src/pages/_app.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRouter } from "next/router"
21
import { appWithTranslation } from "next-i18next"
32
// ChakraProvider import updated as recommended on https://github.com/chakra-ui/chakra-ui/issues/4975#issuecomment-1174234230
43
// to reduce bundle size. Should be reverted to "@chakra-ui/react" in case on theme issues
@@ -7,10 +6,9 @@ import { extendBaseTheme } from "@chakra-ui/react"
76

87
import customTheme from "@/@chakra-ui/theme"
98

10-
import { AppPropsWithLayout, Lang } from "@/lib/types"
11-
12-
import { isLangRightToLeft } from "@/lib/utils/translations"
9+
import { AppPropsWithLayout } from "@/lib/types"
1310

11+
import { useLocaleDirection } from "@/hooks/useLocaleDirection"
1412
import { RootLayout } from "@/layouts"
1513
import { inter, mono } from "@/lib/fonts"
1614

@@ -19,12 +17,9 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => {
1917
// Use the layout defined at the page level, if available
2018
const getLayout = Component.getLayout ?? ((page) => page)
2119

22-
const { locale } = useRouter()
20+
useLocaleDirection()
2321

24-
const theme = extendBaseTheme({
25-
...customTheme,
26-
direction: isLangRightToLeft(locale as Lang) ? "rtl" : "ltr",
27-
})
22+
const theme = extendBaseTheme(customTheme)
2823

2924
return (
3025
<>

0 commit comments

Comments
 (0)