Skip to content

Feat/add haptics to Drawer #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions demo/src/screens/componentScreens/DrawerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class DrawerScreen extends Component {
this.setState({unread: !this.state.unread});
}

triggerLeftToggleHaptic = () => {
// console.warn('haptic trigger here');
}

showItem = () => {
this.setState({hideItem: false});
};
Expand Down Expand Up @@ -230,7 +226,6 @@ class DrawerScreen extends Component {
fullSwipeLeft,
onWillFullSwipeLeft: this.deleteItem,
onToggleSwipeLeft: this.toggleReadState,
leftToggleHapticTrigger: this.triggerLeftToggleHaptic,
testID: 'drawer'
};
if (showRightItems) {
Expand Down
2 changes: 2 additions & 0 deletions generatedTypes/components/drawer/Swipeable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare type Props = {
animationOptions?: Object;
containerStyle?: Object;
childrenContainerStyle?: Object;
disableHaptic?: boolean;
};
declare type StateType = {
dragX: Animated.Value;
Expand All @@ -53,6 +54,7 @@ export default class Swipeable extends Component<Props, StateType> {
fullRightThreshold: number;
};
constructor(props: Props);
_triggerHaptic: () => false | void;
_handleDrag: (e: any) => void;
getTransX: () => Animated.AnimatedInterpolation;
getShowLeftAction: () => Animated.Value | Animated.AnimatedInterpolation;
Expand Down
4 changes: 4 additions & 0 deletions generatedTypes/components/drawer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ interface Props {
* Haptic trigger function to use onToggleSwipeLeft
*/
leftToggleHapticTrigger?: Function;
/**
* Whether to disable the haptic
*/
disableHaptic?: boolean;
/**
* Style
*/
Expand Down
13 changes: 11 additions & 2 deletions src/components/drawer/Swipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, {Component} from 'react';
import {Animated, StyleSheet, View, I18nManager} from 'react-native';
import {PanGestureHandler, TapGestureHandler, State} from 'react-native-gesture-handler';
import {Constants} from '../../helpers';
import {HapticService, HapticType} from '../../services';


const DRAG_TOSS = 0.05;
Expand Down Expand Up @@ -56,7 +57,8 @@ type Props = {
useNativeAnimations: boolean,
animationOptions?: Object,
containerStyle?: Object,
childrenContainerStyle?: Object
childrenContainerStyle?: Object,
disableHaptic?: boolean
};

type StateType = {
Expand Down Expand Up @@ -111,6 +113,10 @@ export default class Swipeable extends Component<Props, StateType> {
});
}

_triggerHaptic = () => {
return !this.props.disableHaptic && HapticService.triggerHaptic(HapticType.impactMedium, 'Drawer');
}

_handleDrag = (e) => {
const {onToggleSwipeLeft} = this.props;

Expand All @@ -123,7 +129,8 @@ export default class Swipeable extends Component<Props, StateType> {
if (!this.dragThresholdReached && x >= threshold && x < threshold + 10) {
// move item right
this.dragThresholdReached = true;
onToggleSwipeLeft({rowWidth, leftWidth, dragX: x, triggerHaptic: true});
this._triggerHaptic();
onToggleSwipeLeft({rowWidth, leftWidth, dragX: x});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the haptic optional, not all users will want to use it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not get the haptic method from the HapticService, i.e. HapticMethods.impactMedium?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be able to do that only when I'll fix the HapticMethods in the HapticService as we talked about in our dev sync.
I'm waiting for this PR to be merged to do so

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Add the other PR as a dependency and we'll merge this after it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the dependency merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :)

}
if (this.dragThresholdReached && x < threshold - 10) {
// move item left
Expand Down Expand Up @@ -260,8 +267,10 @@ export default class Swipeable extends Component<Props, StateType> {
// Swipe left toggle
toValue = rowWidth * LEFT_TOGGLE_THRESHOLD;
} else if (!onToggleSwipeLeft && fullSwipeLeft && translationX > rowWidth * fullLeftThreshold) {
this._triggerHaptic();
toValue = rowWidth;
} else if (fullSwipeRight && translationX < -rowWidth * fullRightThreshold) {
this._triggerHaptic();
toValue = -rowWidth;
} else if (translationX > leftThreshold) {
if (!onToggleSwipeLeft || onToggleSwipeLeft && translationX < rowWidth * LEFT_TOGGLE_THRESHOLD) {
Expand Down
11 changes: 7 additions & 4 deletions src/components/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Constants} from '../../helpers';
import {Colors} from '../../style';
import View from '../view';
import Swipeable, {SwipeableProps} from './Swipeable';
import {LogService} from '../../services';

const DEFAULT_BG = Colors.primary;
const DEFAULT_BOUNCINESS = 0;
Expand Down Expand Up @@ -101,6 +102,10 @@ interface Props {
* Haptic trigger function to use onToggleSwipeLeft
*/
leftToggleHapticTrigger?: Function;
/**
* Whether to disable the haptic
*/
disableHaptic?: boolean;
/**
* Style
*/
Expand Down Expand Up @@ -214,9 +219,6 @@ class Drawer extends PureComponent<Props> {

private onToggleSwipeLeft = (options?: any) => {
if (this.props.onToggleSwipeLeft) {
if (options?.triggerHaptic) {
_.invoke(this.props, 'leftToggleHapticTrigger');
}
this.animateItem(options);
}
};
Expand Down Expand Up @@ -382,7 +384,8 @@ class Drawer extends PureComponent<Props> {
};

render() {
const {children, style, leftItem, rightItems, onToggleSwipeLeft, ...others} = this.props;
const {children, style, leftItem, rightItems, onToggleSwipeLeft, leftToggleHapticTrigger, ...others} = this.props;
leftToggleHapticTrigger && LogService.deprecationWarn({component: 'Drawer', oldProp: 'leftToggleHapticTrigger'});

return (
<Swipeable
Expand Down