Skip to content

Commit ab364d2

Browse files
Daksh BhardwajOlimpiaZurek
Daksh Bhardwaj
authored andcommitted
feat: add userSelect style equivalent to selectable (facebook#34575)
Summary: This adds support for the `userSelect` style attribute, mapping the already existing selectable attribute as requested on facebook#34425. This PR also updates the TextExample.android and TestExample.ios on the RNTester in order to facilitate the manual QA of this. ## Changelog [General] [Added] - Add support for `userSelect` style Pull Request resolved: facebook#34575 Test Plan: - open the RNTester app and navigate to the Text page - Check the `Selectable Text` through the Selectable text section <Image src="https://user-images.githubusercontent.com/22423684/188112863-65acd145-76b0-47ba-8bc6-f72298077096.png" height="600" width="300" /> Reviewed By: yungsters Differential Revision: D39252798 Pulled By: jacdebug fbshipit-source-id: f7fabf20ee68778d75461f511c56f94d0d756d9c
1 parent a124503 commit ab364d2

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

Libraries/Components/View/ReactNativeStyleAttributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
134134
textShadowOffset: true,
135135
textShadowRadius: true,
136136
textTransform: true,
137+
userSelect: true,
137138
writingDirection: true,
138139

139140
/**

Libraries/StyleSheet/StyleSheetTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ export type ____TextStyle_InternalCore = $ReadOnly<{
632632
textDecorationStyle?: 'solid' | 'double' | 'dotted' | 'dashed',
633633
textDecorationColor?: ____ColorValue_Internal,
634634
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
635+
userSelect?: 'auto' | 'text' | 'none' | 'contain' | 'all',
635636
writingDirection?: 'auto' | 'ltr' | 'rtl',
636637
}>;
637638

Libraries/Text/Text.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {NativeText, NativeVirtualText} from './TextNativeComponent';
2020
import {type TextProps} from './TextProps';
2121
import * as React from 'react';
2222
import {useContext, useMemo, useState} from 'react';
23+
import flattenStyle from '../StyleSheet/flattenStyle';
2324

2425
/**
2526
* Text is the fundamental component for displaying text.
@@ -152,7 +153,13 @@ const Text: React.AbstractComponent<
152153
? null
153154
: processColor(restProps.selectionColor);
154155

155-
let style = restProps.style;
156+
let style = flattenStyle(restProps.style);
157+
158+
let _selectable = restProps.selectable;
159+
if (style?.userSelect != null) {
160+
_selectable = userSelectToSelectableMap[style.userSelect];
161+
}
162+
156163
if (__DEV__) {
157164
if (PressabilityDebug.isEnabled() && onPress != null) {
158165
style = StyleSheet.compose(restProps.style, {
@@ -182,6 +189,7 @@ const Text: React.AbstractComponent<
182189
{...eventHandlersForText}
183190
isHighlighted={isHighlighted}
184191
isPressable={isPressable}
192+
selectable={_selectable}
185193
numberOfLines={numberOfLines}
186194
selectionColor={selectionColor}
187195
style={style}
@@ -193,6 +201,7 @@ const Text: React.AbstractComponent<
193201
{...restProps}
194202
{...eventHandlersForText}
195203
disabled={_disabled}
204+
selectable={_selectable}
196205
accessible={_accessible}
197206
accessibilityState={_accessibilityState}
198207
allowFontScaling={allowFontScaling !== false}
@@ -222,4 +231,12 @@ function useLazyInitialization(newValue: boolean): boolean {
222231
return oldValue;
223232
}
224233

234+
const userSelectToSelectableMap = {
235+
auto: true,
236+
text: true,
237+
none: false,
238+
contain: true,
239+
all: true,
240+
};
241+
225242
module.exports = Text;

packages/rn-tester/js/examples/Text/TextExample.android.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,14 @@ exports.examples = [
982982
return <TextBaseLineLayoutExample />;
983983
},
984984
},
985+
{
986+
title: 'Selectable Text',
987+
render: function (): React.Node {
988+
return (
989+
<View>
990+
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
991+
</View>
992+
);
993+
},
994+
},
985995
];

packages/rn-tester/js/examples/Text/TextExample.ios.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,4 +1227,14 @@ exports.examples = [
12271227
);
12281228
},
12291229
},
1230+
{
1231+
title: 'Selectable Text',
1232+
render: function (): React.Node {
1233+
return (
1234+
<View>
1235+
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
1236+
</View>
1237+
);
1238+
},
1239+
},
12301240
];

0 commit comments

Comments
 (0)