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

activeCursor prop #2550

Merged
merged 13 commits into from
Jul 20, 2023
4 changes: 4 additions & 0 deletions docs/docs/api/gestures/base-gesture-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ Adds a gesture that should be recognized simultaneously with this one.
Adds a relation requiring another gesture to fail, before this one can activate.

**IMPORTANT:** Note that this method only marks the relation between gestures, without [composing them](../../gesture-composition).[`GestureDetector`](gesture-detector) will not recognize the `otherGestures` and it needs to be added to another detector in order to be recognized.

### `activeCursor(value)` (**web only**)

This parameter allows to specify which cursor should be used when gesture activates. Supports all CSS cursor values (e.g. `"grab"`, `"zoom-in"`). Default value is set to `"auto"`.
2 changes: 1 addition & 1 deletion docs/docs/api/gestures/gesture-detector.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Starting with Reanimated-2.3.0-beta.4 Gesture Handler will provide a [StateManag

### `userSelect` (**web only**)

This parameter allows to specify which `userSelect` property should be applied to underlying view. Possible values are `"none" | "auto" | "text"`. Defaults to `"none"`.
This parameter allows to specify which `userSelect` property should be applied to underlying view. Possible values are `"none" | "auto" | "text"`. Default value is set to `"none"`.
6 changes: 5 additions & 1 deletion docs/docs/gesture-handlers/api/common-gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Specifying `width` or `height` is useful if we only want the gesture to activate

### `userSelect` (**web only**)

This parameter allows to specify which `userSelect` property should be applied to underlying view. Possible values are `"none" | "auto" | "text"`. Defaults to `"none"`.
This parameter allows to specify which `userSelect` property should be applied to underlying view. Possible values are `"none" | "auto" | "text"`. Default value is set to `"none"`.

### `activeCursor` (**web only**)

This parameter allows to specify which cursor should be used when gesture activates. Supports all CSS cursor values (e.g. `"grab"`, `"zoom-in"`). Default value is set to `"auto"`.

### `onGestureEvent`

Expand Down
9 changes: 9 additions & 0 deletions src/components/DrawerLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
GestureEvent,
HandlerStateChangeEvent,
UserSelect,
ActiveCursor,
} from '../handlers/gestureHandlerCommon';
import {
PanGestureHandler,
Expand Down Expand Up @@ -165,6 +166,13 @@
* Values: 'none'|'text'|'auto'
*/
userSelect?: UserSelect;

/**
* @default 'auto'
* Defines which cursor property should be used when gesture activates.
* Values: see CSS cursor values
*/
activeCursor?: ActiveCursor;
}

export type DrawerLayoutState = {
Expand Down Expand Up @@ -311,7 +319,7 @@
);

const dragOffsetFromOnStartPosition = startPositionX.interpolate({
inputRange: [drawerWidth! - 1, drawerWidth!, drawerWidth! + 1],

Check warning on line 322 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion

Check warning on line 322 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion

Check warning on line 322 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
outputRange: [0, 0, 1],
});
translationX = Animated.add(
Expand All @@ -321,7 +329,7 @@
}

this.openValue = Animated.add(translationX, drawerTranslation).interpolate({
inputRange: [0, drawerWidth!],

Check warning on line 332 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
outputRange: [0, 1],
extrapolate: 'clamp',
});
Expand All @@ -333,7 +341,7 @@
ev: NativeSyntheticEvent<PanGestureHandlerEventPayload>
) => void;
} = {
useNativeDriver: props.useNativeAnimations!,

Check warning on line 344 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
};

if (this.props.onDrawerSlide) {
Expand Down Expand Up @@ -411,7 +419,7 @@

if (drawerType === 'front') {
dragOffsetBasedOnStart =
gestureStartX > drawerWidth! ? gestureStartX - drawerWidth! : 0;

Check warning on line 422 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion

Check warning on line 422 in src/components/DrawerLayout.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
}

const startOffsetX =
Expand Down Expand Up @@ -691,6 +699,7 @@
<PanGestureHandler
// @ts-ignore could be fixed in handler types
userSelect={this.props.userSelect}
activeCursor={this.props.activeCursor}
ref={this.setPanGestureRef}
hitSlop={hitSlop}
activeOffsetX={gestureOrientation * minSwipeDistance!}
Expand Down
39 changes: 39 additions & 0 deletions src/handlers/gestureHandlerCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const commonProps = [
'hitSlop',
'cancelsTouchesInView',
'userSelect',
'activeCursor',
] as const;

const componentInteractionProps = ['waitFor', 'simultaneousHandlers'] as const;
Expand Down Expand Up @@ -64,6 +65,43 @@ export type HitSlop =
| 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';

//TODO(TS) events in handlers

Expand Down Expand Up @@ -105,6 +143,7 @@ export type CommonGestureConfig = {
shouldCancelWhenOutside?: boolean;
hitSlop?: HitSlop;
userSelect?: UserSelect;
activeCursor?: ActiveCursor;
};

// Events payloads are types instead of interfaces due to TS limitation.
Expand Down
6 changes: 6 additions & 0 deletions src/handlers/gestures/gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GestureTouchEvent,
GestureStateChangeEvent,
GestureUpdateEvent,
ActiveCursor,
} from '../gestureHandlerCommon';
import { getNextHandlerTag } from '../handlersRegistry';
import { GestureStateManagerType } from './gestureStateManager';
Expand Down Expand Up @@ -250,6 +251,11 @@ export abstract class BaseGesture<
return this;
}

activeCursor(activeCursor: ActiveCursor) {
this.config.activeCursor = activeCursor;
return this;
}

runOnJS(runOnJS: boolean) {
this.config.runOnJS = runOnJS;
return this;
Expand Down
41 changes: 37 additions & 4 deletions src/web/handlers/GestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,18 @@ export default abstract class GestureHandler {
this.currentState === State.ACTIVE ||
this.currentState === State.BEGAN
) {
// Here the order of this if statement and moveToState call is important.
// At this point we can use currentState as previuos state, because immediately after changing cursor we call moveToState method.
// By checking whether previous state was ACTIVE, we can decide if we should reset the cursor or not.
if (
this.config.activeCursor &&
this.config.activeCursor !== 'auto' &&
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
this.currentState === State.ACTIVE
) {
this.view.style.cursor = 'auto';
}

this.moveToState(State.FAILED, sendIfDisabled);
this.view.style.cursor = 'auto';
}

this.resetProgress();
Expand All @@ -184,8 +194,17 @@ export default abstract class GestureHandler {
this.currentState === State.BEGAN
) {
this.onCancel();

// Same as above - order matters
if (
this.config.activeCursor &&
this.config.activeCursor !== 'auto' &&
this.currentState === State.ACTIVE
) {
this.view.style.cursor = 'auto';
}

this.moveToState(State.CANCELLED, sendIfDisabled);
this.view.style.cursor = 'auto';
}
}

Expand All @@ -195,7 +214,13 @@ export default abstract class GestureHandler {
this.currentState === State.BEGAN
) {
this.moveToState(State.ACTIVE);
this.view.style.cursor = 'grab';

if (
(!this.view.style.cursor || this.view.style.cursor === 'auto') &&
this.config.activeCursor
) {
this.view.style.cursor = this.config.activeCursor;
}
}
}

Expand All @@ -204,8 +229,16 @@ export default abstract class GestureHandler {
this.currentState === State.BEGAN ||
this.currentState === State.ACTIVE
) {
// Same as above - order matters
if (
this.config.activeCursor &&
this.config.activeCursor !== 'auto' &&
this.currentState === State.ACTIVE
) {
this.view.style.cursor = 'auto';
}

this.moveToState(State.END);
this.view.style.cursor = 'auto';
}

this.resetProgress();
Expand Down
4 changes: 3 additions & 1 deletion src/web/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserSelect } from '../handlers/gestureHandlerCommon';
import { UserSelect, ActiveCursor } from '../handlers/gestureHandlerCommon';
import { Directions } from '../Directions';
import { State } from '../State';

Expand All @@ -22,6 +22,7 @@ type ConfigArgs =
| boolean
| HitSlop
| UserSelect
| ActiveCursor
| Directions
| Handler[]
| null
Expand All @@ -34,6 +35,7 @@ export interface Config extends Record<string, ConfigArgs> {
hitSlop?: HitSlop;
shouldCancelWhenOutside?: boolean;
userSelect?: UserSelect;
activeCursor?: ActiveCursor;

activateAfterLongPress?: number;
failOffsetXStart?: number;
Expand Down
Loading