nuka-carousel icon indicating copy to clipboard operation
nuka-carousel copied to clipboard

Height is always equal to first slide's height

Open brendangkchan opened this issue 8 years ago • 6 comments

Hi, I found an issue where the slider height is always equal to the first slide's height. When other slides are taller, they are getting cut off.

Here in the setDimensions function the slide height is set equal to the first slide's height. https://github.com/FormidableLabs/nuka-carousel/blob/master/src/carousel.js#L788

I think to actually get the largest slide's height we need to iterate through them like so:

setDimensions: function setDimensions(props) {
    props = props || this.props;

    var self = this,
        slideWidth,
        slidesToScroll,
        firstSlide,
        frame,
        frameWidth,
        frameHeight,
        slideHeight;

    slidesToScroll = props.slidesToScroll;
    frame = this.refs.frame;
    firstSlide = frame.childNodes[0].childNodes[0];
    
    if (firstSlide) {
      // iterate through the slides to find the biggest height
      var biggestHeight = 0;
      (frame.childNodes[0].childNodes).forEach(function(node) {
        if (node.offsetHeight > biggestHeight) {
          biggestHeight = node.offsetHeight;
        }
      });

      firstSlide.style.height = 'auto';
      slideHeight = this.props.vertical ? firstSlide.offsetHeight * props.slidesToShow : biggestHeight;
    } else {
      slideHeight = 100;
    }

    if (typeof props.slideWidth !== 'number') {
      slideWidth = parseInt(props.slideWidth);
    } else {
      if (props.vertical) {
        slideWidth = slideHeight / props.slidesToShow * props.slideWidth;
      } else {
        slideWidth = frame.offsetWidth / props.slidesToShow * props.slideWidth;
      }
    }

    if (!props.vertical) {
      slideWidth -= props.cellSpacing * ((100 - 100 / props.slidesToShow) / 100);
    }

    frameHeight = slideHeight + props.cellSpacing * (props.slidesToShow - 1);
    frameWidth = props.vertical ? frameHeight : frame.offsetWidth;

    if (props.slidesToScroll === 'auto') {
      slidesToScroll = Math.floor(frameWidth / (slideWidth + props.cellSpacing));
    }

    this.setState({
      slideHeight: slideHeight,
      frameWidth: frameWidth,
      slideWidth: slideWidth,
      slidesToScroll: slidesToScroll,
      left: props.vertical ? 0 : this.getTargetLeft(),
      top: props.vertical ? this.getTargetLeft() : 0
    }, function () {
      self.setLeft();
    });
  },

brendangkchan avatar Jan 08 '18 17:01 brendangkchan

Is this still an issue on [email protected]+ now that we've landed https://github.com/FormidableLabs/nuka-carousel/pull/291?

ryan-roemer avatar Mar 10 '18 21:03 ryan-roemer

Hi, I still have the problem on [email protected] The carousel is initialized with the first slide height, and the next image slides are cut off...

MatBdry avatar Apr 18 '18 09:04 MatBdry

I also have this, even after upgrading to 4.1.0.

Pruxis avatar May 02 '18 09:05 Pruxis

@Pruxis @MatBdry @brendangkchan

Does this bug happen even when using the heightMode="MAX" prop?

@ryan-roemer Do you suppose it would make sense to make this the default?

mhink avatar May 10 '18 22:05 mhink

@mhink I have another problem when I set heightMode="max", but I don't know if it depends on this property. The first slide is cut off when the carousel is initialized:

capture d ecran 2018-05-14 a 09 12 28

Then, when I change the slide, the height is ok:

capture d ecran 2018-05-14 a 09 12 36

MatBdry avatar May 14 '18 07:05 MatBdry

@MatBdry Hello, I'm trying to repro the bug you're finding when you set heightMode="max". When I do it on my local copy using [email protected] it seems to calculate the "max" height properly based on the tallest slide (whether it's the last, first, or somewhere in the center). And the carousel renders correctly on the first slide and each one after.

Can you post some code to help us repro the bug you're getting? Or use something like https://codesandbox.io/ to code up a sample that shows off the bug?

mariano-formidable avatar Jul 03 '18 00:07 mariano-formidable