Skip to content
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

Feat() Update carousel callback to include rightmost visible slide in… #1447

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "pcln-carousel",
"comment": "Upate Carousel callback to send rightmost visible slide instead of left most",
"type": "patch"
}
],
"packageName": "pcln-carousel"
}
14 changes: 9 additions & 5 deletions packages/carousel/src/Carousel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import {
import { RenderInView } from './RenderInView'
import { CAROUSEL_BREAKPOINT_1 } from './constants'

const ChangeDetector = ({ onSlideChange }) => {
const ChangeDetector = ({ onSlideChange, numVisibleSlides }) => {
const carouselContext = useContext(CarouselContext)
// eslint-disable-next-line no-unused-vars
const [_currentSlide, setCurrentSlide] = useState(carouselContext.state.currentSlide)

useEffect(() => {
function onChange() {
setCurrentSlide(carouselContext.state.currentSlide)
const slideNum = Math.floor(carouselContext.state.currentSlide + numVisibleSlides - 1)
setCurrentSlide(slideNum)

if (typeof onSlideChange === 'function') {
onSlideChange(carouselContext.state.currentSlide)
onSlideChange(slideNum)
}
}
carouselContext.subscribe(onChange)
return () => carouselContext.unsubscribe(onChange)
}, [carouselContext, onSlideChange])
}, [carouselContext, onSlideChange, numVisibleSlides])

return null
}
Expand Down Expand Up @@ -100,7 +101,10 @@ export const Carousel = ({
infinite={infinite}
currentSlide={currentSlide}
>
<ChangeDetector onSlideChange={onSlideChange} />
<ChangeDetector
onSlideChange={onSlideChange}
numVisibleSlides={layoutSize || responsiveVisibleSlides}
/>
{arrowsPosition === 'top' ? (
<Flex justifyContent='flex-end' mb={2} mr={slideSpacing}>
<ArrowButton type='prev' position='top' setPosition={arrowsPosition} buttonSize={buttonSize} />
Expand Down
Loading