Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): support RTL in Carousel (icons, scroll direction, button offset) (#4473)

nynexman4464f47124b87da9
diff --git a/.changeset/rtl-carousel.md b/.changeset/rtl-carousel.md
new file mode 100644
index 0000000000..aafc84f901
--- /dev/null
+++ b/.changeset/rtl-carousel.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Carousel now supports RTL: the directional scroll-button chevrons mirror under RTL, the scroll buttons respect RTL scroll semantics (previously the button was a no-op under RTL), and the button pills sit on the correct edges.
+
+@nynexman4464
diff --git a/packages/core/src/Carousel/Carousel.tsx b/packages/core/src/Carousel/Carousel.tsx
index dcc1d377a0..28620f49e1 100644
--- a/packages/core/src/Carousel/Carousel.tsx
+++ b/packages/core/src/Carousel/Carousel.tsx
@@ -41,8 +41,9 @@ import {Button} from '../Button';
import {Icon} from '../Icon';
import {useLayer} from '../Layer';
import {useScrollOverflow} from '../hooks/useScrollOverflow';
+import {isRtlElement} from '../hooks/isRtlElement';
import type {BaseProps} from '../BaseProps';
-import {mergeProps, mergeRefs} from '../utils';
+import {mergeProps, mergeRefs, rtlStyles} from '../utils';
import type {SpacingStep} from '../utils/types';
import {themeProps} from '../utils/themeProps';
import {useTranslator} from '../i18n';
@@ -166,10 +167,16 @@ const styles = stylex.create({
transitionTimingFunction: easeVars['--ease-standard'],
},
buttonPillStart: {
- transform: 'translateX(-50%)',
+ transform: {
+ default: 'translateX(-50%)',
+ ':is([dir="rtl"] *)': 'translateX(50%)',
+ },
},
buttonPillEnd: {
- transform: 'translateX(50%)',
+ transform: {
+ default: 'translateX(50%)',
+ ':is([dir="rtl"] *)': 'translateX(-50%)',
+ },
},
buttonHidden: {
opacity: 0,
@@ -347,7 +354,11 @@ export function Carousel({
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
el.scrollBy({
- left: direction * Math.max(amount, itemWidth),
+ // `direction` is the logical intent (-1 = toward content start, +1 =
+ // toward content end). Under RTL the scroll axis is inverted (start is
+ // scrollLeft 0, the end is negative), so flip the physical delta sign.
+ left:
+ (isRtlElement(el) ? -1 : 1) * direction * Math.max(amount, itemWidth),
behavior: prefersReducedMotion ? 'auto' : 'smooth',
});
}, []);
@@ -425,7 +436,11 @@ export function Carousel({
!overflowStart && styles.buttonHidden,
)}>
<Button
- icon={<Icon icon="chevronLeft" size="xsm" />}
+ icon={
+ <span {...stylex.props(rtlStyles.mirror)}>
+ <Icon icon="chevronLeft" size="xsm" />
+ </span>
+ }
label={t('@astryx.carousel.scrollLeft')}
variant="ghost"
size="sm"
@@ -446,7 +461,11 @@ export function Carousel({
!overflowEnd && styles.buttonHidden,
)}>
<Button
- icon={<Icon icon="chevronRight" size="xsm" />}
+ icon={
+ <span {...stylex.props(rtlStyles.mirror)}>
+ <Icon icon="chevronRight" size="xsm" />
+ </span>
+ }
label={t('@astryx.carousel.scrollRight')}
variant="ghost"
size="sm"