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

feat: add userSelect style equivalent to selectable #34575

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -133,6 +133,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
textShadowOffset: true,
textShadowRadius: true,
textTransform: true,
userSelect: true,
writingDirection: true,

/**
Expand Down
1 change: 1 addition & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ export type ____TextStyle_InternalCore = $ReadOnly<{
textDecorationStyle?: 'solid' | 'double' | 'dotted' | 'dashed',
textDecorationColor?: ____ColorValue_Internal,
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
userSelect?: 'auto' | 'text' | 'none' | 'contain' | 'all',
writingDirection?: 'auto' | 'ltr' | 'rtl',
}>;

Expand Down
20 changes: 20 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ const Text: React.AbstractComponent<
: processColor(restProps.selectionColor);

let style = restProps.style;

let _selectable = restProps.selectable;
if (style && style.userSelect !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Style needs to be flattened as it could be an array

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@necolas i have pushed the changes

_selectable =
style.userSelect !== null
? // $FlowFixMe
userSelectToSelectableMap[style.userSelect]
: restProps.selectable;
}

if (__DEV__) {
if (PressabilityDebug.isEnabled() && onPress != null) {
style = StyleSheet.compose(restProps.style, {
Expand Down Expand Up @@ -182,6 +192,7 @@ const Text: React.AbstractComponent<
{...eventHandlersForText}
isHighlighted={isHighlighted}
isPressable={isPressable}
selectable={_selectable}
numberOfLines={numberOfLines}
selectionColor={selectionColor}
style={style}
Expand All @@ -193,6 +204,7 @@ const Text: React.AbstractComponent<
{...restProps}
{...eventHandlersForText}
disabled={_disabled}
selectable={_selectable}
accessible={_accessible}
accessibilityState={_accessibilityState}
allowFontScaling={allowFontScaling !== false}
Expand Down Expand Up @@ -222,4 +234,12 @@ function useLazyInitialization(newValue: boolean): boolean {
return oldValue;
}

const userSelectToSelectableMap = {
auto: true,
text: true,
none: false,
contain: true,
all: true,
};

module.exports = Text;
10 changes: 10 additions & 0 deletions packages/rn-tester/js/examples/Text/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,14 @@ exports.examples = [
return <TextBaseLineLayoutExample />;
},
},
{
title: 'Selectable Text',
render: function (): React.Node {
return (
<View>
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
</View>
);
},
},
];
10 changes: 10 additions & 0 deletions packages/rn-tester/js/examples/Text/TextExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,4 +1227,14 @@ exports.examples = [
);
},
},
{
title: 'Selectable Text',
render: function (): React.Node {
return (
<View>
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
</View>
);
},
},
];