Skip to content

Commit

Permalink
[Browser] Expose a slideUpdateHeight hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopik authored and oliviertassinari committed Apr 16, 2017
1 parent 3eff5cc commit cabd521
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
41 changes: 37 additions & 4 deletions docs/src/demo/DemoAnimateHeight.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow weak

import React from 'react';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import SwipeableViews from 'react-swipeable-views';

const styles = {
Expand All @@ -24,6 +25,9 @@ const styles = {
slide4: {
backgroundColor: '#FEA900',
},
button: {
marginTop: 14,
},
};

const list = [];
Expand All @@ -36,6 +40,37 @@ for (let i = 0; i < 30; i += 1) {
);
}

class Slide4 extends PureComponent {
static contextTypes = {
swipeableViews: PropTypes.object.isRequired,
};

state = {
large: false,
};

componentDidUpdate() {
this.context.swipeableViews.slideUpdateHeight();
}

handleClick = () => {
this.setState(() => ({
large: !this.state.large,
}));
};

render() {
return (
<div style={Object.assign({}, styles.slide, styles.slide4)}>
{list.slice(0, this.state.large ? 8 : 3)}
<button onClick={this.handleClick} type="button" style={styles.button}>
{this.state.large ? 'Collaspe' : 'Expand'}
</button>
</div>
);
}
}

const DemoAnimateHeight = () => {
return (
<SwipeableViews animateHeight>
Expand All @@ -51,9 +86,7 @@ const DemoAnimateHeight = () => {
<div style={Object.assign({}, styles.slide, styles.slide3)}>
{list.slice(0, 7)}
</div>
<div style={Object.assign({}, styles.slide, styles.slide4)}>
{list.slice(0, 3)}
</div>
<Slide4 />
</SwipeableViews>
);
};
Expand Down
28 changes: 24 additions & 4 deletions packages/react-swipeable-views/src/SwipeableViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,24 @@ class SwipeableViews extends Component {
resistance: false,
};

static childContextTypes = {
swipeableViews: PropTypes.shape({
slideUpdateHeight: PropTypes.func,
}),
};

state = {};

getChildContext() {
return {
swipeableViews: {
slideUpdateHeight: () => {
this.updateHeight();
},
},
};
}

componentWillMount() {
if (process.env.NODE_ENV !== 'production') {
checkIndexBounds(this.props);
Expand Down Expand Up @@ -469,6 +485,7 @@ class SwipeableViews extends Component {
startIndex = 0;
transitionListener = null;
touchMoveListener = null;
activeSlide = null;

handleSwipeStart = (event) => {
const { axis } = this.props;
Expand Down Expand Up @@ -768,9 +785,9 @@ class SwipeableViews extends Component {
}
};

updateHeight(node) {
if (node !== null) {
const child = node.children[0];
updateHeight() {
if (this.activeSlide !== null) {
const child = this.activeSlide.children[0];
if (child !== undefined && child.offsetHeight !== undefined &&
this.state.heightLatest !== child.offsetHeight) {
this.setState({
Expand Down Expand Up @@ -899,7 +916,10 @@ We are expecting a valid React Element`);
hidden = false;

if (animateHeight) {
ref = (node) => this.updateHeight(node);
ref = (node) => {
this.activeSlide = node;
this.updateHeight();
};
slideStyle.overflowY = 'hidden';
}
}
Expand Down

0 comments on commit cabd521

Please sign in to comment.