How to enable full RTL behavior in shadcn/ui Carousel (like Embla’s native RTL support)? #9247
-
|
I’m using the Carousel component from shadcn/ui , and I need it to behave like Embla’s native RTL mode. I tried: <Carousel opts={{ direction: "rtl" }}> but the RTL behavior isn’t fully applied. The issues I’m facing are: The scroll behavior still feels like LTR The Previous/Next arrows aren’t reversed visually or functionally Layout and button positions don’t adapt to RTL mode Is there an official way to get full RTL support in shadcn/ui Carousel (similar to plain Embla)? Any working snippet or guidance would be appreciated. https://codesandbox.io/p/sandbox/gpxgh6 I want it like this |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Shadcn/ui does not currently support full RTL behavior for Carousel out of the box, even though Embla itself does. Passing opts={{ direction: "rtl" }} only enables Embla’s internal RTL logic, but the shadcn/ui wrapper keeps LTR assumptions for button behavior and positioning. To get true Embla-style RTL behavior, you need three things:
Here is a working snippet: "use client"
import * as React from "react"
import {
Carousel,
CarouselContent,
CarouselItem,
CarouselNext,
CarouselPrevious,
type CarouselApi,
} from "@/components/ui/carousel"
export default function RtlCarousel() {
const [api, setApi] = React.useState<CarouselApi | null>(null)
const isRtl =
typeof document !== "undefined" &&
document.documentElement.dir === "rtl"
return (
<Carousel
setApi={setApi}
opts={{ direction: "rtl" }}
dir="rtl"
className="w-full max-w-xl"
>
<CarouselContent>
{[1, 2, 3, 4, 5].map((i) => (
<CarouselItem key={i}>
<div className="flex h-40 items-center justify-center bg-muted">
Slide {i}
</div>
</CarouselItem>
))}
</CarouselContent>
<CarouselPrevious
onClick={() =>
isRtl ? api?.scrollNext() : api?.scrollPrev()
}
className="right-2 left-auto"
/>
<CarouselNext
onClick={() =>
isRtl ? api?.scrollPrev() : api?.scrollNext()
}
className="left-2 right-auto"
/>
</Carousel>
)
}This matches native Embla RTL behavior. There is no official shadcn/ui helper for this yet, so manual handling is currently the recommended solution. |
Beta Was this translation helpful? Give feedback.
Thank you for your technical input. Your answer was helpful as a starting point but not sufficient on its own. After further analysis and testing, with the assistance of AI tools, it turned out that modifying the original shadcn/ui component was required. Below is the updated code.