diff --git a/client/index.html b/client/index.html index 049c2e3fc4..ca68ab0dee 100644 --- a/client/index.html +++ b/client/index.html @@ -47,6 +47,24 @@ document.documentElement.style.setProperty('--ui-scale', String(uiScale)); } + /* Direction before first paint, from the same sources `detectInitialLanguage` reads, + * so a right-to-left interface does not render one frame left-to-right. The list is + * the right-to-left locales the app ships; i18n.ts reapplies `dir` once it loads. */ + const cookie = document.cookie.match(/(?:^|;\s*)lang=([^;]*)/); + let lang = cookie ? cookie[1] : ''; + if (!lang) { + try { + lang = localStorage.getItem('lang') || ''; + lang = JSON.parse(lang); + } catch (e) { + /* Stored unquoted, or Web Storage denied: the raw value or the browser language. */ + } + } + lang = String(lang || navigator.language || '').toLowerCase(); + if (['ar', 'he', 'fa', 'ug'].indexOf(lang.split(/[-_]/)[0]) !== -1) { + document.documentElement.dir = 'rtl'; + } + let theme = null; try { theme = localStorage.getItem('color-theme'); diff --git a/client/package.json b/client/package.json index a26e0cb9ae..a07bb8cded 100644 --- a/client/package.json +++ b/client/package.json @@ -47,6 +47,7 @@ "@radix-ui/react-checkbox": "^1.3.11", "@radix-ui/react-collapsible": "^1.1.20", "@radix-ui/react-dialog": "^1.1.23", + "@radix-ui/react-direction": "^1.1.4", "@radix-ui/react-dropdown-menu": "^2.1.24", "@radix-ui/react-hover-card": "^1.1.23", "@radix-ui/react-icons": "^1.3.0", diff --git a/client/src/App.jsx b/client/src/App.jsx index f8fc49a552..7b57c13a59 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -2,10 +2,10 @@ import { useEffect } from 'react'; import { RecoilRoot } from 'recoil'; import { DndProvider } from 'react-dnd'; import { RouterProvider } from 'react-router-dom'; -import * as RadixToast from '@radix-ui/react-toast'; import { HTML5Backend } from 'react-dnd-html5-backend'; import { Toast, ToastViewport, ToastProvider, useInputModality } from '@librechat/client'; import { QueryClient, QueryClientProvider, QueryCache, MutationCache } from '@tanstack/react-query'; +import Direction, { ToastSwipeProvider } from '~/Providers/Direction'; import { ScreenshotProvider, useApiErrorBoundary } from './hooks'; import WakeLockManager from '~/components/System/WakeLockManager'; import QueryDevtoolsGate from '~/components/QueryDevtoolsGate'; @@ -55,12 +55,13 @@ const App = () => { - - - - - - {/* Location updates commit in the caller's own task instead + + + + + + + {/* Location updates commit in the caller's own task instead of React's transition lane. A transition keeps the OUTGOING route painted until the incoming one finishes rendering, so switching conversations left the previous @@ -79,16 +80,17 @@ const App = () => { transition-safe reads are gated behind `_TRANSITION_SUPPORT_UNSTABLE` hooks this app does not use. Worth revisiting once that state has moved to Jotai. */} - - - - - - - - - - + + + + + + + + + + + ); diff --git a/client/src/Providers/Direction.tsx b/client/src/Providers/Direction.tsx new file mode 100644 index 0000000000..314a46a795 --- /dev/null +++ b/client/src/Providers/Direction.tsx @@ -0,0 +1,24 @@ +import { useTranslation } from 'react-i18next'; +import * as RadixToast from '@radix-ui/react-toast'; +import { DirectionProvider, useDirection } from '@radix-ui/react-direction'; +import type { ReactNode } from 'react'; + +/** + * Radix primitives read their direction from this context, never from ``, and fall back + * to `ltr`. Without it Tabs, Select and menus render `dir="ltr"` subtrees and map arrow keys as LTR + * inside a right-to-left interface. + */ +export default function Direction({ children }: { children: ReactNode }) { + const { i18n } = useTranslation(); + return {children}; +} + +/** Swiping a toast away runs toward the inline end, which is the left edge in a right-to-left UI. */ +export function ToastSwipeProvider({ children }: { children: ReactNode }) { + const dir = useDirection(); + return ( + + {children} + + ); +} diff --git a/client/src/Providers/MessagePartsHostContext.tsx b/client/src/Providers/MessagePartsHostContext.tsx index 6b00fa3e44..7e73f7b271 100644 --- a/client/src/Providers/MessagePartsHostContext.tsx +++ b/client/src/Providers/MessagePartsHostContext.tsx @@ -130,8 +130,7 @@ export const appMessagePartsHost: MessagePartsHost = { useShowThinking: () => useAtomValue(showThinkingAtom), useAutoExpandTools: () => useRecoilValue(store.autoExpandTools), useLaTeXParsing: () => useRecoilValue(store.LaTeXParsing), - useChatDirection: () => - useRecoilValue(store.chatDirection).toLowerCase() === 'rtl' ? 'rtl' : 'ltr', + useChatDirection: () => useRecoilValue(store.chatDirection), useUserTextPreferences: useAppUserTextPreferences, /* The atom rather than the auth context: AuthContextProvider mirrors the user into it, and the * public share route mounts outside that provider. */ diff --git a/client/src/Providers/__tests__/bootDirection.spec.ts b/client/src/Providers/__tests__/bootDirection.spec.ts new file mode 100644 index 0000000000..967b6b250d --- /dev/null +++ b/client/src/Providers/__tests__/bootDirection.spec.ts @@ -0,0 +1,48 @@ +import { resolve } from 'path'; +import { readFileSync } from 'fs'; + +/** The inline shell script in `client/index.html` that sets `dir` before first paint. */ +const bootScript = (() => { + const html = readFileSync(resolve(__dirname, '../../../index.html'), 'utf8'); + const scripts = [...html.matchAll(/