Skip to content
This repository was archived by the owner on Sep 13, 2020. It is now read-only.

Expose the Left Animated Property to the given menu Component #145

Open
wants to merge 1 commit 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
36 changes: 34 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ class SideMenu extends Component {

return (
<Animated.View
style={[styles.frontView, { width, height, }, this.props.animationStyle(this.state.left), ]}
style={[styles.frontView, { width, height, },
this.props.animationStyle(this.state.left, this.interpolateToPercentage(this.state.left)),]}
ref={(sideMenu) => this.sideMenu = sideMenu}
{...this.responder.panHandlers}>
{this.props.children}
Expand All @@ -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
Expand Down Expand Up @@ -274,7 +299,14 @@ class SideMenu extends Component {
* If menu is ready to be rendered
*/
if (this.state.shouldRenderMenu) {
menu = <View style={styles.menu}>{this.props.menu}</View>;
let menuProps = {
openPercentage: this.interpolateToPercentage(this.state.left)
};

// Attach the openPercentage Prop to the given menu
menu = <View style={styles.menu}>
{React.cloneElement(this.props.menu, menuProps)}
</View>;
}

return (
Expand Down