feat(direction): auto-detect RTL from HTML document when no DirectionProvider is present (#3830)#3866
Open
mixelburg wants to merge 1 commit intoradix-ui:mainfrom
Open
feat(direction): auto-detect RTL from HTML document when no DirectionProvider is present (#3830)#3866mixelburg wants to merge 1 commit intoradix-ui:mainfrom
mixelburg wants to merge 1 commit intoradix-ui:mainfrom
Conversation
…Provider is present Fixes radix-ui#3830 When no DirectionProvider exists in the React tree, useDirection() previously fell back to a hardcoded "ltr" — ignoring <html dir="rtl"> entirely. This commit adds a fallback to document.documentElement.dir, so RTL apps work correctly without wrapping in DirectionProvider. - DirectionProvider still takes precedence (no breaking changes) - SSR safe via typeof document guard - LTR apps unaffected (document.dir defaults to ltr)
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #3830
When no
DirectionProviderexists in the React tree,useDirection()currently falls back to a hardcoded'ltr'— ignoring<html dir="rtl">entirely. This commit adds a fallback todocument.documentElement.dirso RTL apps work correctly out of the box.How
Changed
useDirectionto readdocument.documentElement.diras a last-resort fallback before defaulting to'ltr':Why this is safe
DirectionProviderstill takes precedence — existing apps are unaffected<html dir="rtl">, sodocumentDirdefaults to'ltr'typeof document !== 'undefined'guard handles server-side renderingdirchanges on<html>)Who this helps
Arabic, Hebrew, Persian, and Urdu speakers (~500 million people). Every app using Radix that sets
<html dir="rtl">currently has broken RTL layout for dropdowns, dialogs, and popovers.Checks
pnpm -r --filter @radix-ui/react-direction run lint)pnpm -r --filter @radix-ui/react-direction run typecheck)