From 808ccb5085d8a49610ee47c172fdf168d974277e Mon Sep 17 00:00:00 2001 From: gabrielcatalin Date: Wed, 25 Nov 2015 15:05:12 -0500 Subject: [PATCH] Expose the Left Animated Property to the given menu Component --- index.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 23e47c1..3802aec 100644 --- a/index.js +++ b/index.js @@ -236,7 +236,8 @@ class SideMenu extends Component { return ( this.sideMenu = sideMenu} {...this.responder.panHandlers}> {this.props.children} @@ -245,6 +246,30 @@ class SideMenu extends Component { ); } + /** + * Returns the property interpolated + * on a scale from 0 to 100 + * + * @param prop {Animated.Value} + * @returns {Animated.Value} + */ + interpolateToPercentage(prop) { + if (this.menuPositionMultiplier() > 0) { + return prop.interpolate({ + inputRange : [0, this.props.openMenuOffset - this.props.hiddenMenuOffset], + outputRange: [0, 100] + }); + } else { + // When the menu is on the right, the whole interpolation reverses + // in order to maintain the 0 to 100 scale intact. Again, the client + // should only care about the open ratio, not about directions. + return prop.interpolate({ + inputRange : [this.props.hiddenMenuOffset - this.props.openMenuOffset, 0], + outputRange: [100, 0] + }); + } + } + /** * Get menu actions to expose it to * menu and children components @@ -274,7 +299,14 @@ class SideMenu extends Component { * If menu is ready to be rendered */ if (this.state.shouldRenderMenu) { - menu = {this.props.menu}; + let menuProps = { + openPercentage: this.interpolateToPercentage(this.state.left) + }; + + // Attach the openPercentage Prop to the given menu + menu = + {React.cloneElement(this.props.menu, menuProps)} + ; } return (