Skip to content

Commit 5831c7e

Browse files
fabriziomosconmastermoo
authored andcommitted
Add prop useNativeFeedback
- introduce props useNativeFeedback default to true - document props activeOpacity and make a constant for the default value - convert tabs into spaces in shared.js to be consistent with the other files
1 parent 50ec4a1 commit 5831c7e

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

ActionButton.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component, PropTypes } from 'react';
2-
import { StyleSheet, Text, View, Animated, TouchableOpacity, TouchableNativeFeedback, Platform } from 'react-native';
2+
import { StyleSheet, Text, View, Animated, TouchableOpacity, Platform } from 'react-native';
33
import ActionButtonItem from './ActionButtonItem';
4-
import { shadowStyle, alignItemsMap, Touchable, isAndroid, touchableBackground } from './shared';
4+
import { shadowStyle, alignItemsMap, getTouchableComponent, isAndroid, touchableBackground, DEFAULT_ACTIVE_OPACITY } from './shared';
55

66
export default class ActionButton extends Component {
77
constructor(props) {
@@ -94,21 +94,23 @@ export default class ActionButton extends Component {
9494
width: this.props.size,
9595
height: this.props.size,
9696
borderRadius: this.props.size / 2,
97-
}
97+
};
9898

9999
const buttonStyle = {
100100
width: this.props.size,
101101
height: this.props.size,
102102
borderRadius: this.props.size / 2,
103103
alignItems: 'center',
104104
justifyContent: 'center',
105-
}
105+
};
106+
107+
const Touchable = getTouchableComponent(this.props.useNativeFeedback);
106108

107109
return (
108110
<View style={{ paddingHorizontal: this.props.offsetX }}>
109111
<Touchable
110112
background={touchableBackground}
111-
activeOpacity={0.85}
113+
activeOpacity={this.props.activeOpacity}
112114
onLongPress={this.props.onLongPress}
113115
onPress={() => {
114116
this.props.onPress()
@@ -243,6 +245,8 @@ ActionButton.propTypes = {
243245
degrees: PropTypes.number,
244246
verticalOrientation: PropTypes.oneOf(['up', 'down']),
245247
backgroundTappable: PropTypes.bool,
248+
useNativeFeedback: PropTypes.bool,
249+
activeOpacity: PropTypes.number,
246250
};
247251

248252
ActionButton.defaultProps = {
@@ -262,6 +266,8 @@ ActionButton.defaultProps = {
262266
size: 56,
263267
verticalOrientation: 'up',
264268
backgroundTappable: false,
269+
useNativeFeedback: true,
270+
activeOpacity: DEFAULT_ACTIVE_OPACITY,
265271
};
266272

267273
const styles = StyleSheet.create({

ActionButtonItem.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import React, { Component, PropTypes } from 'react';
22
import { StyleSheet, Text, View, Animated,
33
TouchableNativeFeedback, TouchableWithoutFeedback, Dimensions, Platform } from 'react-native';
4-
import { shadowStyle, alignItemsMap, Touchable, isAndroid, touchableBackground } from './shared';
4+
import { shadowStyle, alignItemsMap, getTouchableComponent, isAndroid, touchableBackground, DEFAULT_ACTIVE_OPACITY } from './shared';
55

66
const { width: WIDTH } = Dimensions.get('window');
77
const SHADOW_SPACE = 10;
8+
const TEXT_HEIGHT = 22;
9+
810
const TextTouchable = isAndroid ? TouchableNativeFeedback : TouchableWithoutFeedback;
911

1012
export default class ActionButtonItem extends Component {
1113
static get defaultProps() {
1214
return {
1315
active: true,
14-
spaceBetween: 15
16+
spaceBetween: 15,
17+
useNativeFeedback: true,
18+
activeOpacity: DEFAULT_ACTIVE_OPACITY,
1519
};
1620
}
1721

1822
static get propTypes() {
1923
return {
2024
active: PropTypes.bool,
25+
useNativeFeedback: PropTypes.bool,
26+
activeOpacity: PropTypes.number,
2127
}
2228
}
2329

@@ -51,15 +57,17 @@ export default class ActionButtonItem extends Component {
5157
height: size,
5258
borderRadius: size / 2,
5359
backgroundColor: this.props.buttonColor || this.props.btnColor,
54-
}
60+
};
5561

5662
if (position !== 'center') buttonStyle[position] = (this.props.parentSize-size)/2;
5763

64+
const Touchable = getTouchableComponent(this.props.useNativeFeedback);
65+
5866
return (
5967
<Animated.View pointerEvents="box-none" style={animatedViewStyle}>
6068
<Touchable
6169
background={touchableBackground}
62-
activeOpacity={this.props.activeOpacity || 0.85}
70+
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
6371
onPress={this.props.onPress}>
6472
<View
6573
style={[buttonStyle, !hideShadow && shadowStyle, this.props.style]}
@@ -90,7 +98,7 @@ export default class ActionButtonItem extends Component {
9098
return (
9199
<TextTouchable
92100
background={touchableBackground}
93-
activeOpacity={this.props.activeOpacity || 0.85}
101+
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
94102
onPress={this.props.onPress}>
95103
<View style={textStyles}>
96104
<Text style={[styles.text, this.props.textStyle]}>{this.props.title}</Text>
@@ -100,7 +108,6 @@ export default class ActionButtonItem extends Component {
100108
}
101109
}
102110

103-
const TEXT_HEIGHT = 22;
104111

105112
const styles = StyleSheet.create({
106113
textContainer: {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Take a look at [this gist](https://gist.github.com/mmazzarolo/cfd467436f9d110e94
110110
| onReset | function | null | use this to set the callback that will be called after the button reset's it's items
111111
| verticalOrientation | string | "up" | direction action buttons should expand. One of: `up` or `down`
112112
| backgroundTappable | boolean | false | make background tappable in active state of ActionButton
113+
| useNativeFeedback | boolean | true | whether to use TouchableNativeFeedback on Android
114+
| activeOpacity | number | 0.85 | activeOpacity props of TouchableOpacity
113115

114116
##### ActionButton.Item:
115117
| Property | Type | Default | Description |
@@ -122,3 +124,5 @@ Take a look at [this gist](https://gist.github.com/mmazzarolo/cfd467436f9d110e94
122124
| textContainerStyle | style | null | use this to set the textstyle of the button's item text container
123125
| textStyle | style | null | use this to set the textstyle of the button's item text
124126
| spaceBetween | number | 15 | use this to set the space between the Button and the text container
127+
| useNativeFeedback | boolean | true | whether to use TouchableNativeFeedback on Android
128+
| activeOpacity | number | 0.85 | activeOpacity props of TouchableOpacity

shared.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import { Platform, TouchableOpacity, TouchableNativeFeedback } from 'react-native';
22

3+
export const DEFAULT_ACTIVE_OPACITY = 0.85;
4+
35
export const shadowStyle = {
4-
shadowOpacity: 0.35,
5-
shadowOffset: {
6-
width: 0, height: 5,
7-
},
8-
shadowColor: '#000',
9-
shadowRadius: 3,
10-
elevation: 5,
6+
shadowOpacity: 0.35,
7+
shadowOffset: {
8+
width: 0, height: 5,
9+
},
10+
shadowColor: '#000',
11+
shadowRadius: 3,
12+
elevation: 5,
1113
};
1214

1315
export const alignItemsMap = {
14-
center: 'center',
15-
left: 'flex-start',
16-
right: 'flex-end'
16+
center: 'center',
17+
left: 'flex-start',
18+
right: 'flex-end'
1719
};
1820

19-
20-
export const Touchable = Platform.OS === 'ios' ? TouchableOpacity : TouchableNativeFeedback;
21-
2221
export const isAndroid = Platform.OS === 'android';
2322

24-
export const touchableBackground = isAndroid ? Platform['Version'] >= 21 ? TouchableNativeFeedback.Ripple('rgba(255,255,255,0.75)') : TouchableNativeFeedback.SelectableBackground() : undefined;
23+
export function getTouchableComponent(useNativeFeedback) {
24+
if (useNativeFeedback === true && isAndroid === true) {
25+
return TouchableNativeFeedback;
26+
}
27+
return TouchableOpacity;
28+
}
29+
30+
export const touchableBackground = isAndroid
31+
? Platform['Version'] >= 21
32+
? TouchableNativeFeedback.Ripple('rgba(255,255,255,0.75)', false)
33+
: TouchableNativeFeedback.SelectableBackground()
34+
: undefined;

0 commit comments

Comments
 (0)