forked from mastermoo/react-native-action-button
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionButtonItem.js
More file actions
128 lines (109 loc) · 3.7 KB
/
Copy pathActionButtonItem.js
File metadata and controls
128 lines (109 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { Component, PropTypes } from 'react';
import { StyleSheet, Text, View, Animated,
TouchableNativeFeedback, TouchableWithoutFeedback, Dimensions, Platform } from 'react-native';
import { shadowStyle, alignItemsMap, getTouchableComponent, isAndroid, touchableBackground, DEFAULT_ACTIVE_OPACITY } from './shared';
const { width: WIDTH } = Dimensions.get('window');
const SHADOW_SPACE = 10;
const TEXT_HEIGHT = 22;
const TextTouchable = isAndroid ? TouchableNativeFeedback : TouchableWithoutFeedback;
export default class ActionButtonItem extends Component {
static get defaultProps() {
return {
active: true,
spaceBetween: 15,
useNativeFeedback: true,
activeOpacity: DEFAULT_ACTIVE_OPACITY,
};
}
static get propTypes() {
return {
active: PropTypes.bool,
useNativeFeedback: PropTypes.bool,
activeOpacity: PropTypes.number,
}
}
render() {
const { size, position, verticalOrientation, hideShadow, spacing } = this.props;
if (!this.props.active) return null;
const animatedViewStyle = {
height: size + SHADOW_SPACE + spacing,
marginBottom: -SHADOW_SPACE,
paddingHorizontal: this.props.offsetX,
alignItems: alignItemsMap[position],
// backgroundColor: this.props.buttonColor,
opacity: this.props.anim,
transform: [
{
translateY: this.props.anim.interpolate({
inputRange: [0, 1],
outputRange: [verticalOrientation === 'down' ? -40 : 40, 0]
}),
}
],
};
const buttonStyle = {
justifyContent: 'center',
alignItems: 'center',
width: size,
height: size,
borderRadius: size / 2,
backgroundColor: this.props.buttonColor || this.props.btnColor,
};
if (position !== 'center') buttonStyle[position] = (this.props.parentSize-size)/2;
const Touchable = getTouchableComponent(this.props.useNativeFeedback);
return (
<Animated.View pointerEvents="box-none" style={animatedViewStyle}>
<Touchable
background={touchableBackground}
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
onPress={this.props.onPress}>
<View
style={[buttonStyle, !hideShadow && shadowStyle, this.props.style]}
>
{this.props.children}
</View>
</Touchable>
{this._renderTitle()}
</Animated.View>
);
}
_renderTitle() {
if (!this.props.title) return null;
const { textContainerStyle, hideShadow, offsetX, parentSize, size, position, spaceBetween } = this.props;
const offsetTop = Math.max((size / 2) - (TEXT_HEIGHT/2), 0);
const positionStyles = { top: offsetTop };
if (position !== 'center') {
positionStyles[position] = offsetX + (parentSize-size)/2 + size + spaceBetween;
} else {
positionStyles.right = WIDTH/2 + size/2 + spaceBetween;
}
const textStyles = [styles.textContainer, positionStyles, textContainerStyle, !hideShadow && shadowStyle];
return (
<TextTouchable
background={touchableBackground}
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
onPress={this.props.onPress}>
<View style={textStyles}>
<Text style={[styles.text, this.props.textStyle]}>{this.props.title}</Text>
</View>
</TextTouchable>
);
}
}
const styles = StyleSheet.create({
textContainer: {
position: 'absolute',
paddingVertical: (isAndroid ? 2 : 3),
paddingHorizontal: 8,
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#eee',
backgroundColor: 'white',
height: TEXT_HEIGHT
},
text: {
flex: 1,
fontSize: 12,
color: '#444',
}
});