Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slick-pets-love.md
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.
4 changes: 2 additions & 2 deletions packages/clerk-js/bundlewatch.config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"files": [
{ "path": "./dist/clerk.js", "maxSize": "594kB" },
{ "path": "./dist/clerk.js", "maxSize": "594.1kB" },
{ "path": "./dist/clerk.browser.js", "maxSize": "68.3KB" },
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "110KB" },
{ "path": "./dist/clerk.headless*.js", "maxSize": "52KB" },
{ "path": "./dist/ui-common*.js", "maxSize": "104.05KB" },
{ "path": "./dist/ui-common*.js", "maxSize": "104.4KB" },
{ "path": "./dist/vendors*.js", "maxSize": "39.5KB" },
{ "path": "./dist/coinbase*.js", "maxSize": "38KB" },
{ "path": "./dist/createorganization*.js", "maxSize": "5KB" },
Expand Down
4 changes: 2 additions & 2 deletions packages/clerk-js/src/ui/components/Plans/PlanDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
sx={t => ({
position: 'absolute',
top: t.space.$2,
right: t.space.$2,
insetInlineEnd: t.space.$2,
})}
>
{closeSlot}
Expand All @@ -382,7 +382,7 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
) : null}
<Box
sx={t => ({
paddingRight: t.space.$10,
paddingBlockEnd: t.space.$10,
})}
>
<Flex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ const CardHeader = React.forwardRef<HTMLDivElement, CardHeaderProps>((props, ref
elementDescriptor={descriptors.pricingTableCardDescription}
variant='subtitle'
colorScheme='secondary'
sx={{
justifySelf: 'flex-start',
}}
>
{plan.description}
</Text>
Expand Down Expand Up @@ -412,6 +415,7 @@ const CardHeader = React.forwardRef<HTMLDivElement, CardHeaderProps>((props, ref
plan.isDefault ? localizationKeys('commerce.alwaysFree') : localizationKeys('commerce.billedMonthlyOnly')
}
sx={{
justifySelf: 'flex-start',
alignSelf: 'center',
}}
/>
Expand Down
14 changes: 10 additions & 4 deletions packages/clerk-js/src/ui/elements/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as React from 'react';
import { transitionDurationValues, transitionTiming } from '../../ui/foundations/transitions';
import type { LocalizationKey } from '../customizables';
import { Box, descriptors, Flex, Heading, Icon, Span, useAppearance } from '../customizables';
import { usePrefersReducedMotion } from '../hooks';
import { useDirection, usePrefersReducedMotion } from '../hooks';
import { useScrollLock } from '../hooks/useScrollLock';
import { Close as CloseIcon } from '../icons';
import type { ThemableCssProp } from '../styledSystem';
Expand All @@ -38,6 +38,7 @@ interface DrawerContext {
context: ReturnType<typeof useFloating>['context'];
getFloatingProps: ReturnType<typeof useInteractions>['getFloatingProps'];
portalProps: FloatingPortalProps;
direction: ReturnType<typeof useDirection>;
}

const DrawerContext = React.createContext<DrawerContext | null>(null);
Expand Down Expand Up @@ -87,12 +88,14 @@ function Root({
portalProps,
dismissProps,
}: RootProps) {
const direction = useDirection();

const { refs, context } = useFloating({
open,
onOpenChange,
transform: false,
strategy,
placement: 'right',
placement: direction === 'ltr' ? 'right' : 'left',
...floatingProps,
});

Expand All @@ -112,6 +115,7 @@ function Root({
refs,
context,
getFloatingProps,
direction,
}}
>
<FloatingPortal {...portalProps}>{children}</FloatingPortal>
Expand Down Expand Up @@ -195,7 +199,7 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(({ children }, re
const prefersReducedMotion = usePrefersReducedMotion();
const { animations: layoutAnimations } = useAppearance().parsedLayout;
const isMotionSafe = !prefersReducedMotion && layoutAnimations === true;
const { strategy, refs, context, getFloatingProps } = useDrawerContext();
const { strategy, refs, context, getFloatingProps, direction } = useDrawerContext();
const mergedRefs = useMergeRefs([ref, refs.setFloating]);

const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
Expand Down Expand Up @@ -236,7 +240,9 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(({ children }, re
// Apply the conditional right offset + the spread of the
// box shadow to ensure it is fully offscreen before unmounting
'--transform-offset':
strategy === 'fixed' ? `calc(100% + ${t.space.$3} + ${t.space.$8x75})` : `calc(100% + ${t.space.$8x75})`,
strategy === 'fixed'
? `calc((100% + ${t.space.$3} + ${t.space.$8x75}) * ${direction === 'rtl' ? -1 : 1})`
: `calc((100% + ${t.space.$8x75}) * ${direction === 'rtl' ? -1 : 1})`,
willChange: 'transform',
position: strategy,
insetBlock: strategy === 'fixed' ? t.space.$3 : 0,
Expand Down
102 changes: 102 additions & 0 deletions packages/clerk-js/src/ui/hooks/__tests__/useDirection.test.ts
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');
});
});
});
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './useClerkModalStateParams';
export * from './useClipboard';
export * from './useDebounce';
export * from './useDelayedVisibility';
export * from './useDirection';
export * from './useEmailLink';
export * from './useEnabledThirdPartyProviders';
export * from './useEnterpriseSSOLink';
Expand Down
32 changes: 32 additions & 0 deletions packages/clerk-js/src/ui/hooks/useDirection.ts
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';
Comment on lines +4 to +19
Copy link
Member

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 ?

Suggested change
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';
const default = 'ltr'
if (dir === 'auto' || !dir) {
const computedDirection = window.getComputedStyle(element).direction;
return computedDirection || default;
}
return dir || default;

Copy link
Member Author

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 dir value as it's just an html attribute.

ce9376f

}

export function useDirection(element?: HTMLElement) {
if (typeof window === 'undefined') {
return 'ltr';
}

if (element) {
return getDirectionFromElement(element);
}
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we always using documen.documentElement inside drawer ?


return getDirectionFromElement(document.documentElement);
}