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

Added possibility to react on responsive prop changes #1377

Closed
wants to merge 1 commit into from
Closed
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
74 changes: 42 additions & 32 deletions src/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,7 @@ export default class Slider extends React.Component {
//whyDidYouUpdate(React)
//}
if (this.props.responsive) {
let breakpoints = this.props.responsive.map(
breakpt => breakpt.breakpoint
);
// sort them in increasing order of their numerical value
breakpoints.sort((x, y) => x - y);

breakpoints.forEach((breakpoint, index) => {
// media query for each breakpoint
let bQuery;
if (index === 0) {
bQuery = json2mq({ minWidth: 0, maxWidth: breakpoint });
} else {
bQuery = json2mq({
minWidth: breakpoints[index - 1] + 1,
maxWidth: breakpoint
});
}
// when not using server side rendering
canUseDOM() &&
this.media(bQuery, () => {
this.setState({ breakpoint: breakpoint });
});
});

// Register media query for full screen. Need to support resize from small to large
// convert javascript object to media query string
let query = json2mq({ minWidth: breakpoints.slice(-1)[0] });

canUseDOM() &&
this.media(query, () => {
this.setState({ breakpoint: null });
});
this.calculateBreakpoints(this.props.responsive);
}
}

Expand All @@ -73,6 +42,47 @@ export default class Slider extends React.Component {
});
}

componentWillReceiveProps(nextProps) {
if (this.props.responsive !== nextProps.responsive) {
this.calculateBreakpoints(nextProps.responsive);
}
}

calculateBreakpoints = (responsive) => {
let breakpoints = responsive.map(
breakpt => breakpt.breakpoint
);
// sort them in increasing order of their numerical value
breakpoints.sort((x, y) => x - y);

breakpoints.forEach((breakpoint, index) => {
// media query for each breakpoint
let bQuery;
if (index === 0) {
bQuery = json2mq({ minWidth: 0, maxWidth: breakpoint });
} else {
bQuery = json2mq({
minWidth: breakpoints[index - 1] + 1,
maxWidth: breakpoint
});
}
// when not using server side rendering
canUseDOM() &&
this.media(bQuery, () => {
this.setState({ breakpoint: breakpoint });
});
});

// Register media query for full screen. Need to support resize from small to large
// convert javascript object to media query string
let query = json2mq({ minWidth: breakpoints.slice(-1)[0] });

canUseDOM() &&
this.media(query, () => {
this.setState({ breakpoint: null });
});
}

slickPrev = () => this.innerSlider.slickPrev();

slickNext = () => this.innerSlider.slickNext();
Expand Down