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

Can't seem to wrap carousel in flex box

Open realesh opened this issue 8 years ago • 19 comments

I'm new here, just wanted the Center Mode to show in my single page web, I have followed the getting started guide and add the CDN to my html file, problem is i cant seem to wrap the carousel into my flexbox, just wanted to make it cleaner and style it

here is my flexbox container using styled-components image

and my Carousel.js image image

realesh avatar Jul 24 '18 16:07 realesh

Even I'm facing the same issue looks like the slider is not working when the parent has flex property. Not sure if there is the solution to this issue.

Harshmakadia avatar Jul 27 '18 06:07 Harshmakadia

Right now I just set the container to be display: block.

yuwei-liang avatar Aug 02 '18 20:08 yuwei-liang

@edwardfhsiao - If you wrap the component inside the container it will work <div class="container"><Slider Component/></div>

Harshmakadia avatar Aug 09 '18 08:08 Harshmakadia

I'm having the same issue, even if a far above parent is flex everything brokes

also seems to happen with display: grid parents

DiegoVallejoDev avatar Aug 10 '18 17:08 DiegoVallejoDev

I have the same issue, and I think should we add width to the parentNode of <div className="slick-track" />?

The width of the child node will effect the layout of flex . However, the width of <div className="slick-track" /> is the total width of the children node, but we just need one of the children node`s width. Maybe add a width to the parentNode of <div className="slick-track" /> can fix this?

Do I make the sense?

HsuTing avatar Aug 16 '18 06:08 HsuTing

I overwrite the width of <div class='slick-track' /> and <div class='slick-slide' /> with below patch:

patch-package
--- a/node_modules/react-slick/lib/track.js
+++ b/node_modules/react-slick/lib/track.js
@@ -113,7 +113,9 @@ var renderSlides = function renderSlides(spec) {
       className: (0, _classnames2.default)(slideClasses, slideClass),
       tabIndex: "-1",
       "aria-hidden": !slideClasses["slick-active"],
-      style: _extends({ outline: "none" }, child.props.style || {}, childStyle),
+      style: _extends({ outline: "none" }, child.props.style || {}, childStyle, {
+        width: `calc(100% / ${spec.children.length * 2 + 1})`,
+      }),
       onClick: function onClick(e) {
         child.props && child.props.onClick && child.props.onClick(e);
         if (spec.focusOnSelect) {
@@ -137,7 +139,9 @@ var renderSlides = function renderSlides(spec) {
           tabIndex: "-1",
           className: (0, _classnames2.default)(slideClasses, slideClass),
           "aria-hidden": !slideClasses["slick-active"],
-          style: _extends({}, child.props.style || {}, childStyle),
+          style: _extends({}, child.props.style || {}, childStyle, {
+            width: `calc(100% / ${spec.children.length * 2 + 1})`,
+          }),
           onClick: function onClick(e) {
             child.props && child.props.onClick && child.props.onClick(e);
             if (spec.focusOnSelect) {
@@ -159,7 +163,9 @@ var renderSlides = function renderSlides(spec) {
           tabIndex: "-1",
           className: (0, _classnames2.default)(slideClasses, slideClass),
           "aria-hidden": !slideClasses["slick-active"],
-          style: _extends({}, child.props.style || {}, childStyle),
+          style: _extends({}, child.props.style || {}, childStyle, {
+            width: `calc(100% / ${spec.children.length * 2 + 1})`,
+          }),
           onClick: function onClick(e) {
             child.props && child.props.onClick && child.props.onClick(e);
             if (spec.focusOnSelect) {
@@ -192,14 +198,15 @@ var Track = exports.Track = function (_React$PureComponent) {
     var _props = this.props,
         onMouseEnter = _props.onMouseEnter,
         onMouseOver = _props.onMouseOver,
-        onMouseLeave = _props.onMouseLeave;
+        onMouseLeave = _props.onMouseLeave,
+        trackStyle = _props.trackStyle;

     var mouseEvents = { onMouseEnter: onMouseEnter, onMouseOver: onMouseOver, onMouseLeave: onMouseLeave };
     return _react2.default.createElement(
       "div",
       _extends({
         className: "slick-track",
-        style: this.props.trackStyle
+        style: _extends({}, trackStyle, { width: `calc(100% * ${slides.length})` }),
       }, mouseEvents),
       slides
     );

This work under flex to me. Hope to help you.

HsuTing avatar Aug 17 '18 07:08 HsuTing

Setting { min-height: 0px; min-width: 0px; } on container fixed the issue for me.

Gabriel-diez avatar Oct 05 '18 13:10 Gabriel-diez

Having the same issue here, any solution would be highly appreciated. Thanks

aclowwwn avatar Nov 02 '18 04:11 aclowwwn

Setting { min-height: 0px; min-width: 0px; } on container fixed the issue for me.

Thanks! I've been looking for 3 hours....

abathellier avatar Nov 27 '18 12:11 abathellier

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 22 '19 13:11 stale[bot]

Anyone still having this issue, just put * { min-height: 0; min-width: 0; } in the css file

im-amir avatar Feb 24 '20 15:02 im-amir

I'm still having this issue and above solutions didn't work. Is it possible to use react slick when it has an ancestor that is display: flex?

MarkPare avatar Aug 06 '20 19:08 MarkPare

I'm still having this issue and above solutions didn't work. Is it possible to use react slick when it has an ancestor that is display: flex?

I also had same error and was looking for solution for a week now. but i found out that the property is getting there through some other class due to improper naming of class and namespaces.

Hope in future everyone reading this will double check every classes and use modules....

Vaisakhkm2625 avatar Jul 30 '21 18:07 Vaisakhkm2625

i fixed it by setting height: 100%; width: 100%;

hitchcliff avatar Feb 14 '23 05:02 hitchcliff

Late response but you can fix it by setting a wrapper for the slider

<div className='flex-container'>
  <> any other content </>
    **<div className='wrapper'>**
         <Slider {...props} />
    **<div />**
<div />

and your wrapper should have display: block; and width and height setted to 100%

IsidoroAros avatar Jul 08 '23 10:07 IsidoroAros

for anyone facing this issue on slider child then try to wrap the child slide under the div like below: Screenshot 2023-12-26 at 2 15 12 PM

mshahzaib101 avatar Dec 26 '23 09:12 mshahzaib101

I see this issue... is there a solution for it?

ozzytop avatar Jan 17 '24 00:01 ozzytop

Same issue here =(

sebas7468 avatar Jun 13 '24 20:06 sebas7468

TL;DR: If you are using grid, you cannot use fractional units fr. The container needs to have a defined width.

Had this issue when trying to use the Slider in a display:grid layout. The workaround that most people seem to be suggesting is to add some sizing classes to the container. For me, all I needed was width while setting { min-height: 0; min-width: 0; } directly on the container didn't work.


	<div style={{ width: '600px' }}> // container needs a defined width 
		<Slider>
              ...rest of code
		</Slider>
	</div>

HOWEVER, I found the source of the issue in the grid for myself! The grid-template-columns cannot be set to fr units! I think the carousel needs a set width hence why add the width to the container classes did the trick before.

.your-grid-layout {
	display: grid;
	grid-template-columns: 50% 50%;   // 1fr 1fr would cause the flickering issue here
	...rest of code
}

ninajhun avatar Oct 07 '25 17:10 ninajhun