Skip to content

Commit

Permalink
feat: apply rtl to carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
bang9 committed Nov 25, 2024
1 parent 51f43d4 commit 84302cc
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/components/ui/SnapCarousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Container = styled.div({
overflowY: 'scroll',
gap: 12,
scrollPadding: 0,
paddingLeft: 0,
paddingInlineStart: 0,
scrollbarWidth: 'none',
userSelect: 'none',
'::-webkit-scrollbar': {
Expand Down Expand Up @@ -58,25 +58,23 @@ export const SnapCarousel = ({
}: SnapCarouselProps) => {
const ref = useRef<HTMLDivElement>(null);
const [activeIndex, setActiveState] = useState(0);
const itemLength = React.Children.toArray(children).length;

const direction = (ref.current ? getComputedStyle(ref.current).direction : 'ltr') as 'rtl' | 'ltr';
const itemLength = React.Children.toArray(children).length;
const itemWidth = useMemo(() => {
const total = ref.current?.scrollWidth ?? 0;
return (total - (startPadding + endPadding + gap * (itemLength - 1))) / itemLength;
}, [ref.current?.scrollWidth, itemLength, gap, startPadding, endPadding]);

const onScroll = (e: React.UIEvent<HTMLDivElement>) => {
const idx = Math.round(e.currentTarget.scrollLeft / itemWidth);
const idx = Math.round(e.currentTarget.scrollLeft / itemWidth) * (direction === 'ltr' ? 1 : -1);
if (idx !== activeIndex) setActiveState(idx);
};

const scrollTo = (index: number) => {
if (ref.current) {
const nextIdx = Math.min(Math.max(0, index), itemLength - 1);
ref.current.scroll({
left: nextIdx * itemWidth,
behavior: 'smooth',
});
ref.current.scroll({ left: nextIdx * itemWidth * (direction === 'ltr' ? 1 : -1), behavior: 'smooth' });
}
};

Expand All @@ -88,8 +86,8 @@ export const SnapCarousel = ({
style={{
gap,
scrollPadding: startPadding,
paddingLeft: startPadding,
paddingRight: endPadding,
paddingInlineStart: startPadding,
paddingInlineEnd: endPadding,
...style,
}}
>
Expand Down

0 comments on commit 84302cc

Please sign in to comment.