-
Notifications
You must be signed in to change notification settings - Fork 404
fix(clerk-js): Ensure RTL is supported within Drawer component #5906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8242209
fix(clerk-js): Ensure RTL is supported within Drawer component
alexcarpenter 2e5279f
fixes
alexcarpenter 3bba27d
add changeset
alexcarpenter 8a74a11
bump
alexcarpenter f9753bd
Merge branch 'main' into alexcarpenter-drawer-rtl
alexcarpenter ce9376f
add test case
alexcarpenter 84fbac4
use ReturnType
alexcarpenter 9f859df
Merge branch 'main' into alexcarpenter-drawer-rtl
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| --- | ||
|
|
||
| Ensure Checkout drawer animation and content respects RTL usage. |
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
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
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
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
102 changes: 102 additions & 0 deletions
102
packages/clerk-js/src/ui/hooks/__tests__/useDirection.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
|
|
||
| import { useDirection } from '../useDirection'; | ||
|
|
||
| describe('useDirection', () => { | ||
| const originalWindow = window; | ||
| const mockGetComputedStyle = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| // Mock window.getComputedStyle | ||
| mockGetComputedStyle.mockReset(); | ||
| Object.defineProperty(window, 'getComputedStyle', { | ||
| value: mockGetComputedStyle, | ||
| writable: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Restore window | ||
| Object.defineProperty(global, 'window', { | ||
| value: originalWindow, | ||
| writable: true, | ||
| }); | ||
| }); | ||
|
|
||
| describe('SSR environment', () => { | ||
| const originalWindow = global.window; | ||
|
|
||
| beforeEach(() => { | ||
| // @ts-ignore - Intentionally removing window for SSR test | ||
| delete global.window; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| global.window = originalWindow; | ||
| }); | ||
|
|
||
| it('returns ltr when window is undefined', () => { | ||
| expect(useDirection()).toBe('ltr'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Browser environment', () => { | ||
| it('returns rtl when element has dir="rtl"', () => { | ||
| const element = document.createElement('div'); | ||
| element.dir = 'rtl'; | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('rtl'); | ||
| }); | ||
|
|
||
| it('returns ltr when element has dir="ltr"', () => { | ||
| const element = document.createElement('div'); | ||
| element.dir = 'ltr'; | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('ltr'); | ||
| }); | ||
|
|
||
| it('returns rtl when element has computed direction rtl', () => { | ||
| const element = document.createElement('div'); | ||
| element.dir = 'auto'; | ||
| mockGetComputedStyle.mockReturnValue({ direction: 'rtl' }); | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('rtl'); | ||
| }); | ||
|
|
||
| it('returns ltr when element has no dir attribute', () => { | ||
| const element = document.createElement('div'); | ||
| mockGetComputedStyle.mockReturnValue({ direction: 'ltr' }); | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('ltr'); | ||
| }); | ||
|
|
||
| it('returns ltr when element has invalid dir attribute value', () => { | ||
| const element = document.createElement('div'); | ||
| element.dir = 'test'; | ||
| mockGetComputedStyle.mockReturnValue({ direction: 'ltr' }); | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('ltr'); | ||
| }); | ||
|
|
||
| it('uses document.documentElement when no element is provided', () => { | ||
| document.documentElement.dir = 'rtl'; | ||
|
|
||
| const { result } = renderHook(() => useDirection()); | ||
| expect(result.current).toBe('rtl'); | ||
| }); | ||
|
|
||
| it('prioritizes element direction over document direction', () => { | ||
| document.documentElement.dir = 'rtl'; | ||
| const element = document.createElement('div'); | ||
| element.dir = 'ltr'; | ||
|
|
||
| const { result } = renderHook(() => useDirection(element)); | ||
| expect(result.current).toBe('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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| function getDirectionFromElement(element: HTMLElement): 'ltr' | 'rtl' { | ||
| const dir = element.dir; | ||
|
|
||
| if (dir === 'rtl') { | ||
| return 'rtl'; | ||
| } | ||
|
|
||
| if (dir === 'ltr') { | ||
| return 'ltr'; | ||
| } | ||
|
|
||
| if (dir === 'auto' || !dir) { | ||
| const computedDirection = window.getComputedStyle(element).direction; | ||
| if (computedDirection === 'rtl') { | ||
| return 'rtl'; | ||
| } | ||
| } | ||
|
|
||
| return 'ltr'; | ||
| } | ||
|
|
||
| export function useDirection(element?: HTMLElement) { | ||
| if (typeof window === 'undefined') { | ||
| return 'ltr'; | ||
| } | ||
|
|
||
| if (element) { | ||
| return getDirectionFromElement(element); | ||
| } | ||
|
Comment on lines
+27
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't we always using |
||
|
|
||
| return getDirectionFromElement(document.documentElement); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can it be simplified to this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test case why I went with the current approach. I think more explicit/defensive approach is right here, someone could hypothetically apply an invalid
dirvalue as it's just an html attribute.ce9376f