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

Replay recent PR improving animatedHeight prop #585

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 32 additions & 3 deletions packages/react-swipeable-views/src/SwipeableViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class SwipeableViews extends React.Component {

startIndex = 0;

resizeListener = null;

transitionListener = null;

touchMoveListener = null;
Expand Down Expand Up @@ -260,6 +262,15 @@ class SwipeableViews extends React.Component {
}

componentDidMount() {
// Subscribe to resize events and update height if animateHeight param is set.
this.resizeListener = addEventListener(window, 'resize', () => {
if (!this.props.animateHeight) {
return;
}

this.updateHeight();
});

// Subscribe to transition end events.
this.transitionListener = addEventListener(this.containerNode, 'transitionend', event => {
if (event.target !== this.containerNode) {
Expand Down Expand Up @@ -318,7 +329,18 @@ class SwipeableViews extends React.Component {
}
}

componentDidUpdate(prevProps) {
// If animateHeight or adjustHeight is on
// and has changed children, readjust height
const { animateHeight, children, adjustHeight } = this.props;
const animate = animateHeight === true || adjustHeight === true;
if (animate && prevProps.children !== children) {
this.updateHeight();
}
}

componentWillUnmount() {
this.resizeListener.remove();
this.transitionListener.remove();
this.touchMoveListener.remove();
clearTimeout(this.firstRenderTimeout);
Expand Down Expand Up @@ -688,6 +710,7 @@ class SwipeableViews extends React.Component {
render() {
const {
action,
adjustHeight,
animateHeight,
animateTransitions,
axis,
Expand Down Expand Up @@ -755,7 +778,7 @@ So animateHeight is most likely having no effect at all.`,
transition = createTransition('transform', springConfig);
WebkitTransition = createTransition('-webkit-transform', springConfig);

if (heightLatest !== 0) {
if (heightLatest !== 0 && animateHeight) {
const additionalTranstion = `, ${createTransition('height', springConfig)}`;
transition += additionalTranstion;
WebkitTransition += additionalTranstion;
Expand All @@ -777,7 +800,7 @@ So animateHeight is most likely having no effect at all.`,
containerStyle.transform = transform;
}

if (animateHeight) {
if (animateHeight || adjustHeight) {
containerStyle.height = heightLatest;
}

Expand Down Expand Up @@ -812,7 +835,7 @@ We are expecting a valid React Element`,
if (indexChild === indexLatest) {
hidden = false;

if (animateHeight) {
if (animateHeight || adjustHeight) {
ref = this.setActiveSlide;
slideStyle.overflowY = 'hidden';
}
Expand Down Expand Up @@ -851,6 +874,11 @@ SwipeableViews.propTypes = {
* that can be triggered programmatically.
*/
action: PropTypes.func,
/**
* If `true`, the height of the container will be adjusted to match the current slide height.
* Does not animate.
*/
adjustHeight: PropTypes.bool,
/**
* If `true`, the height of the container will be animated to match the current slide height.
* Animating another style property has a negative impact regarding performance.
Expand Down Expand Up @@ -997,6 +1025,7 @@ SwipeableViews.propTypes = {
};

SwipeableViews.defaultProps = {
adjustHeight: false,
animateHeight: false,
animateTransitions: true,
axis: 'x',
Expand Down