react-native-gesture-handler
Version:
Declarative API exposing native platform touch and gesture system to React Native
214 lines (194 loc) • 5.61 kB
text/typescript
// Previous types exported gesture handlers as classes which creates an interface and variable, both named the same as class.
// Without those types, we'd introduce breaking change, forcing users to prefix every handler type specification with typeof
// e.g. React.createRef<TapGestureHandler> -> React.createRef<typeof TapGestureHandler>.
// See https://www.typescriptlang.org/docs/handbook/classes.html#constructor-functions for reference.
import * as React from 'react';
import { State } from '../State';
import { TouchEventType } from '../TouchEventType';
import { ValueOf } from '../typeUtils';
import { PointerType } from '../PointerType';
const commonProps = [
'id',
'enabled',
'shouldCancelWhenOutside',
'hitSlop',
'cancelsTouchesInView',
'userSelect',
'activeCursor',
'mouseButton',
'enableContextMenu',
'touchAction',
] as const;
const componentInteractionProps = [
'waitFor',
'simultaneousHandlers',
'blocksHandlers',
] as const;
export const baseGestureHandlerProps = [
...commonProps,
...componentInteractionProps,
'onBegan',
'onFailed',
'onCancelled',
'onActivated',
'onEnded',
'onGestureEvent',
'onHandlerStateChange',
] as const;
export const baseGestureHandlerWithDetectorProps = [
...commonProps,
'needsPointerData',
'manualActivation',
];
export interface GestureEventPayload {
handlerTag: number;
numberOfPointers: number;
state: ValueOf<typeof State>;
pointerType: PointerType;
}
export interface HandlerStateChangeEventPayload extends GestureEventPayload {
oldState: ValueOf<typeof State>;
}
export type HitSlop =
| number
| null
| undefined
| Partial<
Record<
'left' | 'right' | 'top' | 'bottom' | 'vertical' | 'horizontal',
number | undefined
>
>
| Record<'width' | 'left', number>
| Record<'width' | 'right', number>
| Record<'height' | 'top', number>
| Record<'height' | 'bottom', number>;
export type UserSelect = 'none' | 'auto' | 'text';
export type ActiveCursor =
| 'auto'
| 'default'
| 'none'
| 'context-menu'
| 'help'
| 'pointer'
| 'progress'
| 'wait'
| 'cell'
| 'crosshair'
| 'text'
| 'vertical-text'
| 'alias'
| 'copy'
| 'move'
| 'no-drop'
| 'not-allowed'
| 'grab'
| 'grabbing'
| 'e-resize'
| 'n-resize'
| 'ne-resize'
| 'nw-resize'
| 's-resize'
| 'se-resize'
| 'sw-resize'
| 'w-resize'
| 'ew-resize'
| 'ns-resize'
| 'nesw-resize'
| 'nwse-resize'
| 'col-resize'
| 'row-resize'
| 'all-scroll'
| 'zoom-in'
| 'zoom-out';
export enum MouseButton {
LEFT = 1,
RIGHT = 2,
MIDDLE = 4,
BUTTON_4 = 8,
BUTTON_5 = 16,
ALL = 31,
}
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>> {
nativeEvent: Readonly<GestureEventPayload & ExtraEventPayloadT>;
}
export interface HandlerStateChangeEvent<
ExtraEventPayloadT = Record<string, unknown>,
> {
nativeEvent: Readonly<HandlerStateChangeEventPayload & ExtraEventPayloadT>;
}
export type TouchData = {
id: number;
x: number;
y: number;
absoluteX: number;
absoluteY: number;
};
export type GestureTouchEvent = {
handlerTag: number;
numberOfTouches: number;
state: ValueOf<typeof State>;
eventType: TouchEventType;
allTouches: TouchData[];
changedTouches: TouchData[];
pointerType: PointerType;
};
export type GestureUpdateEvent<GestureEventPayloadT = Record<string, unknown>> =
GestureEventPayload & GestureEventPayloadT;
export type GestureStateChangeEvent<
GestureStateChangeEventPayloadT = Record<string, unknown>,
> = HandlerStateChangeEventPayload & GestureStateChangeEventPayloadT;
export type CommonGestureConfig = {
enabled?: boolean | undefined;
shouldCancelWhenOutside?: boolean | undefined;
hitSlop?: HitSlop | undefined;
userSelect?: UserSelect | undefined;
activeCursor?: ActiveCursor | undefined;
mouseButton?: MouseButton | undefined;
enableContextMenu?: boolean | undefined;
touchAction?: TouchAction | undefined;
};
// Events payloads are types instead of interfaces due to TS limitation.
// See https://github.com/microsoft/TypeScript/issues/15300 for more info.
export type BaseGestureHandlerProps<
ExtraEventPayloadT extends Record<string, unknown> = Record<string, unknown>,
> = CommonGestureConfig & {
id?: string | undefined;
waitFor?: React.Ref<unknown> | React.Ref<unknown>[] | undefined;
simultaneousHandlers?: React.Ref<unknown> | React.Ref<unknown>[] | undefined;
blocksHandlers?: React.Ref<unknown> | React.Ref<unknown>[] | undefined;
testID?: string | undefined;
cancelsTouchesInView?: boolean | undefined;
// TODO(TS) - fix event types
onBegan?: ((event: HandlerStateChangeEvent) => void) | undefined;
onFailed?: ((event: HandlerStateChangeEvent) => void) | undefined;
onCancelled?: ((event: HandlerStateChangeEvent) => void) | undefined;
onActivated?: ((event: HandlerStateChangeEvent) => void) | undefined;
onEnded?: ((event: HandlerStateChangeEvent) => void) | undefined;
// TODO(TS) consider using NativeSyntheticEvent
onGestureEvent?:
| ((event: GestureEvent<ExtraEventPayloadT>) => void)
| undefined;
onHandlerStateChange?:
| ((event: HandlerStateChangeEvent<ExtraEventPayloadT>) => void)
| undefined;
// Implicit `children` prop has been removed in @types/react^18.0.0
children?: React.ReactNode;
};