File tree Expand file tree Collapse file tree 2 files changed +22
-12
lines changed Expand file tree Collapse file tree 2 files changed +22
-12
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- import { useEffect } from "react"
2
- import { useRouter } from "next/router"
3
1
import { appWithTranslation } from "next-i18next"
4
2
// ChakraProvider import updated as recommended on https://github.com/chakra-ui/chakra-ui/issues/4975#issuecomment-1174234230
5
3
// to reduce bundle size. Should be reverted to "@chakra-ui/react" in case on theme issues
@@ -8,10 +6,9 @@ import { extendBaseTheme } from "@chakra-ui/react"
8
6
9
7
import customTheme from "@/@chakra-ui/theme"
10
8
11
- import { AppPropsWithLayout , Lang } from "@/lib/types"
12
-
13
- import { isLangRightToLeft } from "@/lib/utils/translations"
9
+ import { AppPropsWithLayout } from "@/lib/types"
14
10
11
+ import { useLocaleDirection } from "@/hooks/useLocaleDirection"
15
12
import { RootLayout } from "@/layouts"
16
13
import { inter , mono } from "@/lib/fonts"
17
14
@@ -20,13 +17,7 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => {
20
17
// Use the layout defined at the page level, if available
21
18
const getLayout = Component . getLayout ?? ( ( page ) => page )
22
19
23
- const { locale } = useRouter ( )
24
-
25
- useEffect ( ( ) => {
26
- if ( ! document ) return
27
- const dir : "rtl" | "ltr" = isLangRightToLeft ( locale as Lang ) ? "rtl" : "ltr"
28
- document . getElementsByTagName ( "html" ) [ 0 ] . setAttribute ( "dir" , dir )
29
- } , [ locale ] )
20
+ useLocaleDirection ( )
30
21
31
22
const theme = extendBaseTheme ( customTheme )
32
23
You can’t perform that action at this time.
0 commit comments