-
Notifications
You must be signed in to change notification settings - Fork 105
feat(mobile): add initialPage prop to Carousel #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import React, { | |
| useCallback, | ||
| useEffect, | ||
| useImperativeHandle, | ||
| useLayoutEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
|
|
@@ -188,6 +189,12 @@ export type CarouselBaseProps = SharedProps & | |
| * @default 'page' | ||
| */ | ||
| snapMode?: 'item' | 'page'; | ||
| /** | ||
| * Zero-based page index to show on first layout, with no animation. Uncontrolled — | ||
| * updating it later does not move the carousel. Out-of-range values are clamped. | ||
| * @default 0 | ||
| */ | ||
| initialPage?: number; | ||
| /** | ||
| * Hides the navigation arrows (previous/next buttons). | ||
| */ | ||
|
|
@@ -563,6 +570,7 @@ export const Carousel = memo( | |
| paginationVariant, | ||
| drag = 'snap', | ||
| snapMode = 'page', | ||
| initialPage, | ||
| NavigationComponent = DefaultCarouselNavigation, | ||
| PaginationComponent = DefaultCarouselPagination, | ||
| style, | ||
|
|
@@ -581,13 +589,14 @@ export const Carousel = memo( | |
| ...props | ||
| } = mergedProps; | ||
| const carouselScrollX = useRef(0); | ||
| const hasAppliedInitialPageRef = useRef(false); | ||
|
|
||
| const animationApi = useSpring({ | ||
| x: carouselScrollX.current, | ||
| config: animationConfig, | ||
| }); | ||
|
|
||
| const [activePageIndex, setActivePageIndex] = useState(0); | ||
| const [activePageIndex, setActivePageIndex] = useState(() => Math.max(0, initialPage ?? 0)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably use the value from the ref here in case the initialPage prop does in fact change. If that were the case then the activePage state would also change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @caitlin-coyiuto-cb I think we need to clamp this with the max as well right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can here on first render since it'd be 0? totalPages gets derived on line 725 later. Also have tests for clamping on last page (if initialPage > last page) and negative initialPage
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I was wondering if we needed to set activePageIndex when we set hasAppliedInitialPageRef to true. I suppose goToPage does clamp automatically but I wasn't sure if there is an interim side effects if this value was way off. |
||
| const [containerSize, onLayout] = useLayout(); | ||
| const [carouselItemRects, setCarouselItemRects] = useState<{ | ||
| [itemId: string]: Rect; | ||
|
|
@@ -766,7 +775,7 @@ export const Carousel = memo( | |
| }); | ||
|
|
||
| const goToPage = useCallback( | ||
| (page: number) => { | ||
| (page: number, { animate: shouldAnimate = true }: { animate?: boolean } = {}) => { | ||
| const newPage = Math.max(0, Math.min(totalPages - 1, page)); | ||
| updateActivePageIndex(newPage); | ||
| updateVisibleCarouselItems(pageOffsets[newPage]); | ||
|
|
@@ -777,7 +786,11 @@ export const Carousel = memo( | |
| : pageOffsets[newPage]; | ||
|
|
||
| carouselScrollX.current = targetOffset; | ||
| animationApi.x.start({ to: targetOffset, config: animationConfig }); | ||
| if (shouldAnimate) { | ||
| animationApi.x.start({ to: targetOffset, config: animationConfig }); | ||
| } else { | ||
| animationApi.x.set(targetOffset); | ||
| } | ||
| reset(); | ||
| }, | ||
| [ | ||
|
|
@@ -792,6 +805,16 @@ export const Carousel = memo( | |
| ], | ||
| ); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (hasAppliedInitialPageRef.current || totalPages === 0) return; | ||
| hasAppliedInitialPageRef.current = true; | ||
|
|
||
| const targetPage = initialPage ?? 0; | ||
| if (targetPage > 0) { | ||
| goToPage(targetPage, { animate: false }); | ||
|
caitlin-coyiuto-cb marked this conversation as resolved.
|
||
| } | ||
| }, [totalPages, initialPage, goToPage]); | ||
|
|
||
| useImperativeHandle( | ||
| ref, | ||
| () => ({ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.