Skip to content

Commit a1563e2

Browse files
authored
Feat/add haptics to Drawer (#1266)
* Feat/add haptics to Drawer * add disableHaptic prop * get impactMedium from HapticType * add method _triggerHaptic * remove HAPTIC_METHOD const
1 parent fd6a062 commit a1563e2

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

demo/src/screens/componentScreens/DrawerScreen.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ class DrawerScreen extends Component {
8484
this.setState({unread: !this.state.unread});
8585
}
8686

87-
triggerLeftToggleHaptic = () => {
88-
// console.warn('haptic trigger here');
89-
}
90-
9187
showItem = () => {
9288
this.setState({hideItem: false});
9389
};
@@ -230,7 +226,6 @@ class DrawerScreen extends Component {
230226
fullSwipeLeft,
231227
onWillFullSwipeLeft: this.deleteItem,
232228
onToggleSwipeLeft: this.toggleReadState,
233-
leftToggleHapticTrigger: this.triggerLeftToggleHaptic,
234229
testID: 'drawer'
235230
};
236231
if (showRightItems) {

generatedTypes/components/drawer/Swipeable.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ declare type Props = {
3434
animationOptions?: Object;
3535
containerStyle?: Object;
3636
childrenContainerStyle?: Object;
37+
disableHaptic?: boolean;
3738
};
3839
declare type StateType = {
3940
dragX: Animated.Value;
@@ -53,6 +54,7 @@ export default class Swipeable extends Component<Props, StateType> {
5354
fullRightThreshold: number;
5455
};
5556
constructor(props: Props);
57+
_triggerHaptic: () => false | void;
5658
_handleDrag: (e: any) => void;
5759
getTransX: () => Animated.AnimatedInterpolation;
5860
getShowLeftAction: () => Animated.Value | Animated.AnimatedInterpolation;

generatedTypes/components/drawer/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ interface Props {
8888
* Haptic trigger function to use onToggleSwipeLeft
8989
*/
9090
leftToggleHapticTrigger?: Function;
91+
/**
92+
* Whether to disable the haptic
93+
*/
94+
disableHaptic?: boolean;
9195
/**
9296
* Style
9397
*/

src/components/drawer/Swipeable.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, {Component} from 'react';
1111
import {Animated, StyleSheet, View, I18nManager} from 'react-native';
1212
import {PanGestureHandler, TapGestureHandler, State} from 'react-native-gesture-handler';
1313
import {Constants} from '../../helpers';
14+
import {HapticService, HapticType} from '../../services';
1415

1516

1617
const DRAG_TOSS = 0.05;
@@ -56,7 +57,8 @@ type Props = {
5657
useNativeAnimations: boolean,
5758
animationOptions?: Object,
5859
containerStyle?: Object,
59-
childrenContainerStyle?: Object
60+
childrenContainerStyle?: Object,
61+
disableHaptic?: boolean
6062
};
6163

6264
type StateType = {
@@ -111,6 +113,10 @@ export default class Swipeable extends Component<Props, StateType> {
111113
});
112114
}
113115

116+
_triggerHaptic = () => {
117+
return !this.props.disableHaptic && HapticService.triggerHaptic(HapticType.impactMedium, 'Drawer');
118+
}
119+
114120
_handleDrag = (e) => {
115121
const {onToggleSwipeLeft} = this.props;
116122

@@ -123,7 +129,8 @@ export default class Swipeable extends Component<Props, StateType> {
123129
if (!this.dragThresholdReached && x >= threshold && x < threshold + 10) {
124130
// move item right
125131
this.dragThresholdReached = true;
126-
onToggleSwipeLeft({rowWidth, leftWidth, dragX: x, triggerHaptic: true});
132+
this._triggerHaptic();
133+
onToggleSwipeLeft({rowWidth, leftWidth, dragX: x});
127134
}
128135
if (this.dragThresholdReached && x < threshold - 10) {
129136
// move item left
@@ -260,8 +267,10 @@ export default class Swipeable extends Component<Props, StateType> {
260267
// Swipe left toggle
261268
toValue = rowWidth * LEFT_TOGGLE_THRESHOLD;
262269
} else if (!onToggleSwipeLeft && fullSwipeLeft && translationX > rowWidth * fullLeftThreshold) {
270+
this._triggerHaptic();
263271
toValue = rowWidth;
264272
} else if (fullSwipeRight && translationX < -rowWidth * fullRightThreshold) {
273+
this._triggerHaptic();
265274
toValue = -rowWidth;
266275
} else if (translationX > leftThreshold) {
267276
if (!onToggleSwipeLeft || onToggleSwipeLeft && translationX < rowWidth * LEFT_TOGGLE_THRESHOLD) {

src/components/drawer/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Constants} from '../../helpers';
99
import {Colors} from '../../style';
1010
import View from '../view';
1111
import Swipeable, {SwipeableProps} from './Swipeable';
12+
import {LogService} from '../../services';
1213

1314
const DEFAULT_BG = Colors.primary;
1415
const DEFAULT_BOUNCINESS = 0;
@@ -101,6 +102,10 @@ interface Props {
101102
* Haptic trigger function to use onToggleSwipeLeft
102103
*/
103104
leftToggleHapticTrigger?: Function;
105+
/**
106+
* Whether to disable the haptic
107+
*/
108+
disableHaptic?: boolean;
104109
/**
105110
* Style
106111
*/
@@ -214,9 +219,6 @@ class Drawer extends PureComponent<Props> {
214219

215220
private onToggleSwipeLeft = (options?: any) => {
216221
if (this.props.onToggleSwipeLeft) {
217-
if (options?.triggerHaptic) {
218-
_.invoke(this.props, 'leftToggleHapticTrigger');
219-
}
220222
this.animateItem(options);
221223
}
222224
};
@@ -382,7 +384,8 @@ class Drawer extends PureComponent<Props> {
382384
};
383385

384386
render() {
385-
const {children, style, leftItem, rightItems, onToggleSwipeLeft, ...others} = this.props;
387+
const {children, style, leftItem, rightItems, onToggleSwipeLeft, leftToggleHapticTrigger, ...others} = this.props;
388+
leftToggleHapticTrigger && LogService.deprecationWarn({component: 'Drawer', oldProp: 'leftToggleHapticTrigger'});
386389

387390
return (
388391
<Swipeable

0 commit comments

Comments
 (0)