react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

Add .slick-active class on slides that are visible

Open weaintplastic opened this issue 7 years ago • 15 comments

When reaching the end of a finite slider with multiple slides, not all of them do get the .slick-active class.

Here's a little snapshot to describe the issue.

The slider is set up as following:

slidesToShow={4}
slidesToSroll={4}
infinite={false}

Initial - first 4 slides visible


<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide"></div>
<div class="slick-slide"></div>

After click on next arrow (actual behavoir) - last 4 slides visible

<div class="slick-slide"></div>
<div class="slick-slide"></div>
<div class="slick-slide"></div>
<div class="slick-slide"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>

After click on next arrow (expeted behavoir) - last 4 slides visible

<div class="slick-slide"></div>
<div class="slick-slide"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>
<div class="slick-slide slick-active"></div>

Is this an expected bahvoir? If not, I've a fix ready and I'm happy to file a pull request.

Best

Roland

weaintplastic avatar May 19 '17 10:05 weaintplastic

hey @akiran. did you have any chance to take a look at this proposal? Would love to get your feedback.

Thanks,

Roland

weaintplastic avatar Jun 06 '17 13:06 weaintplastic

You should be able to do this in your component using componentWillReceiveProps and the nextProps. You know the length of the array of slides, react-slick gives you your currentSlide, and you know how many slides you're showing in the carousel, in your case 4 slides. If currentSlide = 0, and you're displaying 4 slides, then positions 0-3 in your array are 'active'

brownsmith avatar Jun 06 '17 13:06 brownsmith

@brownsmith thanks for the fast reply. You mean the componentWillReceiveProps of my slide component? This is what I get there as nextProps:

children:Object
className:"slick-slide"
data-index:1
onClick:function onClick(e)
style:Object
tabIndex:"-1"

So there is no hint on the current slide included.

weaintplastic avatar Jun 06 '17 14:06 weaintplastic

I'll try to hook into the beforeChange callback

weaintplastic avatar Jun 06 '17 14:06 weaintplastic

Okay. this seems to be an option but just as long as there are no responsive settings set up. As soon as there are responsive settings I end up doing the same match media calculations outside of the Slider component which adds redundant complexity.

weaintplastic avatar Jun 06 '17 14:06 weaintplastic

This is true, we're having to do the same thing twice depending on the number of slides at both of our breakpoints. It's overkill, but it solved our problem. Perhaps it could be passed out to a helper function with the number of slides as an argument? It's a problem I'll have to fix myself at some point.

brownsmith avatar Jun 06 '17 14:06 brownsmith

Also recalculating the active slide in the main component causes the slides to rerender and therefore it breaks the animation. Only option I currently see is to share it with context which is also not nice.

weaintplastic avatar Jun 06 '17 14:06 weaintplastic

This is still an issue. My workaround was setting slidesToScroll option one lower then the slidesToShow option.

marcbey avatar Dec 04 '17 13:12 marcbey

This has been fixed way back, please make sure you're using the latest version.

laveesingh avatar Mar 26 '18 11:03 laveesingh

The same issue version 0.23.1 https://codesandbox.io/s/ymn9kr8xjz Broken slick-active and aria-hidden for latest slide after click nextArrow Settings

var settings = {
  slidesToShow: 2,
  slidesToScroll: 2
};

maxval1 avatar Aug 29 '18 12:08 maxval1

This has not been fixed, since there is no logic to handle this case: https://github.com/akiran/react-slick/blob/master/src/track.js#L32-L34

loginov-rocks avatar Sep 24 '18 15:09 loginov-rocks

Hi @maddhruv @akiran Is there any update on the issue?

qqli1209 avatar Dec 11 '18 03:12 qqli1209

@akiran Do you plan on fixing this issue any time soon? It would be greatly appreciated

re11434 avatar Aug 08 '19 21:08 re11434

@akiran same issue on v0.25.2 !!

yasenhaji avatar Nov 13 '19 16:11 yasenhaji

How can I set my active style on the custom paging? or how can I modify slick-active class.?

mizanmahi avatar Dec 12 '21 11:12 mizanmahi