Skip to content

Add cursor prop to View and Touchables #760

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 19 commits into from
Aug 22, 2022
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
1 change: 1 addition & 0 deletions Libraries/Components/View/ReactNativeStyleAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
borderTopLeftRadius: true,
borderTopRightRadius: true,
borderTopStartRadius: true,
cursor: true,
opacity: true,

/**
Expand Down
1 change: 1 addition & 0 deletions Libraries/Components/View/ReactNativeViewViewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const ReactNativeViewConfig: ViewConfig = {
borderWidth: true,
bottom: true,
color: {process: require('../../StyleSheet/processColor')},
cursor: true,
decomposedMatrix: true,
direction: true,
display: true,
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Components/View/ReactNativeViewViewConfigMacOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const ReactNativeViewViewConfigMacOS = {
accessibilityTraits: true,
draggedTypes: true,
enableFocusRing: true,
nextKeyViewTag: true,
onBlur: true,
onClick: true,
onDoubleClick: true,
Expand All @@ -48,12 +49,11 @@ const ReactNativeViewViewConfigMacOS = {
onFocus: true,
onKeyDown: true,
onKeyUp: true,
validKeysDown: true,
validKeysUp: true,
nextKeyViewTag: true,
onMouseEnter: true,
onMouseLeave: true,
tooltip: true,
validKeysDown: true,
validKeysUp: true,
},
};

Expand Down
25 changes: 25 additions & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import type {NativeColorValue} from './PlatformColorValueTypes';

export type ____ColorValue_Internal = null | string | NativeColorValue;

export type CursorValue = ?(
| 'alias'
| 'auto'
| 'col-resize'
| 'context-menu'
| 'copy'
| 'crosshair'
| 'default'
| 'disappearing-item'
| 'e-resize'
| 'grab'
| 'grabbing'
| 'n-resize'
| 'no-drop'
| 'not-allowed'
| 'pointer'
| 'row-resize'
| 's-resize'
| 'text'
| 'vertical-text'
| 'w-resize'
);

export type ColorArrayValue = null | $ReadOnlyArray<____ColorValue_Internal>;
export type PointValue = {|
x: number,
Expand Down Expand Up @@ -584,6 +607,7 @@ export type ____ViewStyle_Internal = $ReadOnly<{|
borderTopWidth?: number | AnimatedNode,
opacity?: number | AnimatedNode,
elevation?: number,
cursor?: CursorValue,
|}>;

export type ____FontWeight_Internal =
Expand Down Expand Up @@ -638,6 +662,7 @@ export type ____TextStyle_Internal = $ReadOnly<{|
textDecorationColor?: ____ColorValue_Internal,
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
writingDirection?: 'auto' | 'ltr' | 'rtl',
cursor?: CursorValue,
|}>;

export type ____ImageStyle_Internal = $ReadOnly<{|
Expand Down
10 changes: 5 additions & 5 deletions Libraries/Text/RCTTextAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,27 @@ - (NSParagraphStyle *)effectiveParagraphStyle
alignment = NSTextAlignmentRight;
}
}

paragraphStyle.alignment = alignment;
isParagraphStyleUsed = YES;
}

if (_baseWritingDirection != NSWritingDirectionNatural) {
paragraphStyle.baseWritingDirection = _baseWritingDirection;
isParagraphStyleUsed = YES;
}

if (!isnan(_lineHeight)) {
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
isParagraphStyleUsed = YES;
}

if (isParagraphStyleUsed) {
return [paragraphStyle copy];
}

return nil;
}

Expand Down
7 changes: 7 additions & 0 deletions React/Base/macOS/RCTUIKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ - (void)setBackgroundColor:(NSColor *)backgroundColor
}
}

// We purposely don't use RCTCursor for the parameter type here because it would introduce an import cycle:
// RCTUIKit > RCTCursor > RCTConvert > RCTUIKit
- (void)setCursor:(NSInteger)cursor
{
// This method is required to be defined due to [RCTVirtualTextViewManager view] returning a RCTUIView.
}

@end

// RCTUIScrollView
Expand Down
39 changes: 39 additions & 0 deletions React/Views/RCTCursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTConvert.h>

typedef NS_ENUM(NSInteger, RCTCursor) {
RCTCursorAuto,
RCTCursorArrow,
RCTCursorIBeam,
RCTCursorCrosshair,
RCTCursorClosedHand,
RCTCursorOpenHand,
RCTCursorPointingHand,
RCTCursorResizeLeft,
RCTCursorResizeRight,
RCTCursorResizeLeftRight,
RCTCursorResizeUp,
RCTCursorResizeDown,
RCTCursorResizeUpDown,
RCTCursorDisappearingItem,
RCTCursorIBeamCursorForVerticalLayout,
RCTCursorOperationNotAllowed,
RCTCursorDragLink,
RCTCursorDragCopy,
RCTCursorContextualMenu,
};

@interface RCTConvert (RCTCursor)

+ (RCTCursor)RCTCursor:(id)json;
#if TARGET_OS_OSX // TODO(macOS GH#774)
+ (NSCursor *)NSCursor:(RCTCursor)rctCursor;
#endif // ]TODO(macOS GH#774)

@end
105 changes: 105 additions & 0 deletions React/Views/RCTCursor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTCursor.h>

@implementation RCTConvert (RCTCursor)

RCT_ENUM_CONVERTER(
RCTCursor,
(@{
@"alias" : @(RCTCursorDragLink),
@"auto" : @(RCTCursorAuto),
@"col-resize" : @(RCTCursorResizeLeftRight),
@"context-menu" : @(RCTCursorContextualMenu),
@"copy" : @(RCTCursorDragCopy),
@"crosshair" : @(RCTCursorCrosshair),
@"default" : @(RCTCursorArrow),
@"disappearing-item" : @(RCTCursorDisappearingItem),
@"e-resize" : @(RCTCursorResizeRight),
@"grab" : @(RCTCursorOpenHand),
@"grabbing" : @(RCTCursorClosedHand),
@"n-resize" : @(RCTCursorResizeUp),
@"no-drop" : @(RCTCursorOperationNotAllowed),
@"not-allowed" : @(RCTCursorOperationNotAllowed),
@"pointer" : @(RCTCursorPointingHand),
@"row-resize" : @(RCTCursorResizeUpDown),
@"s-resize" : @(RCTCursorResizeDown),
@"text" : @(RCTCursorIBeam),
@"vertical-text" : @(RCTCursorIBeamCursorForVerticalLayout),
@"w-resize" : @(RCTCursorResizeLeft),
}),
RCTCursorAuto,
integerValue)

#if TARGET_OS_OSX // TODO(macOS GH#774)
+ (NSCursor *)NSCursor:(RCTCursor)rctCursor
{
NSCursor *cursor;

switch (rctCursor) {
case RCTCursorArrow:
cursor = [NSCursor arrowCursor];
break;
case RCTCursorClosedHand:
cursor = [NSCursor closedHandCursor];
break;
case RCTCursorContextualMenu:
cursor = [NSCursor contextualMenuCursor];
break;
case RCTCursorCrosshair:
cursor = [NSCursor crosshairCursor];
break;
case RCTCursorDisappearingItem:
cursor = [NSCursor disappearingItemCursor];
break;
case RCTCursorDragCopy:
cursor = [NSCursor dragCopyCursor];
break;
case RCTCursorDragLink:
cursor = [NSCursor dragLinkCursor];
break;
case RCTCursorIBeam:
cursor = [NSCursor IBeamCursor];
break;
case RCTCursorIBeamCursorForVerticalLayout:
cursor = [NSCursor IBeamCursorForVerticalLayout];
break;
case RCTCursorOpenHand:
cursor = [NSCursor openHandCursor];
break;
case RCTCursorOperationNotAllowed:
cursor = [NSCursor operationNotAllowedCursor];
break;
case RCTCursorPointingHand:
cursor = [NSCursor pointingHandCursor];
break;
case RCTCursorResizeDown:
cursor = [NSCursor resizeDownCursor];
break;
case RCTCursorResizeLeft:
cursor = [NSCursor resizeLeftCursor];
break;
case RCTCursorResizeLeftRight:
cursor = [NSCursor resizeLeftRightCursor];
break;
case RCTCursorResizeRight:
cursor = [NSCursor resizeRightCursor];
break;
case RCTCursorResizeUp:
cursor = [NSCursor resizeUpCursor];
break;
case RCTCursorResizeUpDown:
cursor = [NSCursor resizeUpDownCursor];
break;
}

return cursor;
}
#endif // ]TODO(macOS GH#774)

@end
5 changes: 5 additions & 0 deletions React/Views/RCTView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#import <React/RCTEventDispatcherProtocol.h> // TODO(OSS Candidate ISS#2710739)
#import <React/RCTPointerEvents.h>

#if TARGET_OS_OSX // TODO(macOS GH#774)
#import <React/RCTCursor.h>
#endif // TODO(macOS GH#774)

#if !TARGET_OS_OSX // TODO(macOS GH#774)
extern const UIAccessibilityTraits SwitchAccessibilityTrait;
#endif // TODO(macOS GH#774)
Expand Down Expand Up @@ -123,6 +127,7 @@ extern const UIAccessibilityTraits SwitchAccessibilityTrait;
/**
* macOS Properties
*/
@property (nonatomic, assign) RCTCursor cursor;

@property (nonatomic, assign) CATransform3D transform3D;

Expand Down
Loading