Skip to content

Commit

Permalink
Fix bug with always starting at 0
Browse files Browse the repository at this point in the history
  • Loading branch information
melvin-chen committed Jan 14, 2025
1 parent 7d649b1 commit 4b15005
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/nuka/src/Carousel/Carousel.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const GoToPage: Story = {
},
};

export const InitialSlide: Story = {
export const InitialPage: Story = {
args: {
initialPage: 2,
scrollDistance: 'slide',
Expand Down
14 changes: 8 additions & 6 deletions packages/nuka/src/hooks/use-paging.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';

import { CarouselProps } from '../types';

Expand All @@ -20,11 +20,13 @@ export function usePaging({
wrapMode,
initialPage,
}: PagingProps): UsePagingReturnType {
const [currentPage, setCurrentPage] = useState(
initialPage && initialPage >= 0 && initialPage < totalPages
? initialPage
: 0,
);
const [currentPage, setCurrentPage] = useState(0);

useEffect(() => {
if (initialPage) {
setCurrentPage(Math.max(0, Math.min(initialPage, totalPages)));
}
}, [initialPage, totalPages]);

const goToPage = (idx: number) => {
if (idx < 0 || idx >= totalPages) return;
Expand Down

0 comments on commit 4b15005

Please sign in to comment.