Skip to content
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

Add touchAction prop. #2787

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/components/Swipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
return (
<PanGestureHandler
activeOffsetX={[-dragOffsetFromRightEdge, dragOffsetFromLeftEdge]}
touchAction={'pan-y'}

Check failure on line 544 in src/components/Swipeable.tsx

View workflow job for this annotation

GitHub Actions / check

Curly braces are unnecessary here
{...this.props}
onGestureEvent={this.onGestureEvent}
onHandlerStateChange={this.onHandlerStateChange}>
Expand All @@ -551,6 +552,7 @@
{right}
<TapGestureHandler
enabled={rowState !== 0}
touchAction={'pan-y'}

Check failure on line 555 in src/components/Swipeable.tsx

View workflow job for this annotation

GitHub Actions / check

Curly braces are unnecessary here
onHandlerStateChange={this.onTapHandlerStateChange}>
<Animated.View
pointerEvents={rowState === 0 ? 'auto' : 'box-only'}
Expand Down
19 changes: 19 additions & 0 deletions src/handlers/gestureHandlerCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const commonProps = [
'activeCursor',
'mouseButton',
'enableContextMenu',
'touchAction',
] as const;

const componentInteractionProps = [
Expand Down Expand Up @@ -113,6 +114,23 @@ export type ActiveCursor =
| 'zoom-in'
| 'zoom-out';

export type TouchAction =
| 'auto'
| 'none'
| 'pan-x'
| 'pan-left'
| 'pan-right'
| 'pan-y'
| 'pan-up'
| 'pan-down'
| 'pinch-zoom'
| 'manipulation'
| 'inherit'
| 'initial'
| 'revert'
| 'revert-layer'
| 'unset';

//TODO(TS) events in handlers

export interface GestureEvent<ExtraEventPayloadT = Record<string, unknown>> {
Expand Down Expand Up @@ -156,6 +174,7 @@ export type CommonGestureConfig = {
activeCursor?: ActiveCursor;
mouseButton?: MouseButton;
enableContextMenu?: boolean;
touchAction?: TouchAction;
};

// Events payloads are types instead of interfaces due to TS limitation.
Expand Down
15 changes: 15 additions & 0 deletions src/handlers/gestures/GestureDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
HandlerStateChangeEvent,
scheduleFlushOperations,
UserSelect,
TouchAction,
} from '../gestureHandlerCommon';
import {
GestureStateManager,
Expand Down Expand Up @@ -614,11 +615,21 @@ const applyEnableContextMenuProp = (
}
};

const applyTouchActionProp = (
touchAction: TouchAction,
gesture: ComposedGesture | GestureType
): void => {
for (const g of gesture.toGestureArray()) {
g.config.touchAction = touchAction;
}
};

interface GestureDetectorProps {
gesture: ComposedGesture | GestureType;
children?: React.ReactNode;
userSelect?: UserSelect;
enableContextMenu?: boolean;
touchAction?: TouchAction;
}
interface GestureDetectorState {
firstRender: boolean;
Expand All @@ -644,6 +655,10 @@ export const GestureDetector = (props: GestureDetectorProps) => {
applyEnableContextMenuProp(props.enableContextMenu, gestureConfig);
}

if (props.touchAction !== undefined) {
applyTouchActionProp(props.touchAction, gestureConfig);
}
Comment on lines +658 to +660
Copy link
Member

Choose a reason for hiding this comment

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

We may want to figure out a better approach to do this, that's outside of the scope of this PR though 😄


const gesture = gestureConfig.toGestureArray();
const useReanimatedHook = gesture.some((g) => g.shouldUseReanimated);

Expand Down
8 changes: 7 additions & 1 deletion src/web/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { UserSelect, ActiveCursor } from '../handlers/gestureHandlerCommon';
import {
UserSelect,
ActiveCursor,
TouchAction,
} from '../handlers/gestureHandlerCommon';
import { Directions } from '../Directions';
import { State } from '../State';
import { PointerType } from '../PointerType';
Expand All @@ -23,6 +27,7 @@ type ConfigArgs =
| boolean
| HitSlop
| UserSelect
| TouchAction
| ActiveCursor
| Directions
| Handler[]
Expand All @@ -40,6 +45,7 @@ export interface Config extends Record<string, ConfigArgs> {
activeCursor?: ActiveCursor;
mouseButton?: MouseButton;
enableContextMenu?: boolean;
touchAction?: TouchAction;

activateAfterLongPress?: number;
failOffsetXStart?: number;
Expand Down
8 changes: 4 additions & 4 deletions src/web/tools/GestureHandlerWebDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export class GestureHandlerWebDelegate
this.gestureHandler = handler;
this.view = findNodeHandle(viewRef) as unknown as HTMLElement;

this.view.style['touchAction'] = 'none';
//@ts-ignore This one disables default events on Safari
this.view.style['WebkitTouchCallout'] = 'none';

const config = handler.getConfig();

this.addContextMenuListeners(config);
Expand All @@ -48,6 +44,10 @@ export class GestureHandlerWebDelegate
this.view.style['userSelect'] = config.userSelect;
}

this.view.style['touchAction'] = config.touchAction ?? 'none';
//@ts-ignore This one disables default events on Safari
this.view.style['WebkitTouchCallout'] = 'none';
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved

this.eventManagers.push(new PointerEventManager(this.view));
this.eventManagers.push(new TouchEventManager(this.view));

Expand Down
Loading