@@ -35,7 +35,9 @@ export interface PressProps extends PressEvents {
35
35
* still pressed, onPressStart will be fired again. If set to `true`, the press is canceled
36
36
* when the pointer leaves the target and onPressStart will not be fired if the pointer returns.
37
37
*/
38
- shouldCancelOnPointerExit ?: boolean
38
+ shouldCancelOnPointerExit ?: boolean ,
39
+ /** Whether text selection should be enabled on the pressable element. */
40
+ allowTextSelectionOnPress ?: boolean
39
41
}
40
42
41
43
export interface PressHookProps extends PressProps {
@@ -99,6 +101,7 @@ export function usePress(props: PressHookProps): PressResult {
99
101
isPressed : isPressedProp ,
100
102
preventFocusOnPress,
101
103
shouldCancelOnPointerExit,
104
+ allowTextSelectionOnPress,
102
105
// eslint-disable-next-line @typescript-eslint/no-unused-vars
103
106
ref : _ , // Removing `ref` from `domProps` because TypeScript is dumb,
104
107
...domProps
@@ -217,7 +220,9 @@ export function usePress(props: PressHookProps): PressResult {
217
220
state . activePointerId = null ;
218
221
state . pointerType = null ;
219
222
removeAllGlobalListeners ( ) ;
220
- restoreTextSelection ( ) ;
223
+ if ( ! allowTextSelectionOnPress ) {
224
+ restoreTextSelection ( state . target ) ;
225
+ }
221
226
}
222
227
} ;
223
228
@@ -329,7 +334,10 @@ export function usePress(props: PressHookProps): PressResult {
329
334
focusWithoutScrolling ( e . currentTarget ) ;
330
335
}
331
336
332
- disableTextSelection ( ) ;
337
+ if ( ! allowTextSelectionOnPress ) {
338
+ disableTextSelection ( state . target ) ;
339
+ }
340
+
333
341
triggerPressStart ( e , state . pointerType ) ;
334
342
335
343
addGlobalListener ( document , 'pointermove' , onPointerMove , false ) ;
@@ -404,7 +412,9 @@ export function usePress(props: PressHookProps): PressResult {
404
412
state . activePointerId = null ;
405
413
state . pointerType = null ;
406
414
removeAllGlobalListeners ( ) ;
407
- restoreTextSelection ( ) ;
415
+ if ( ! allowTextSelectionOnPress ) {
416
+ restoreTextSelection ( state . target ) ;
417
+ }
408
418
}
409
419
} ;
410
420
@@ -535,7 +545,10 @@ export function usePress(props: PressHookProps): PressResult {
535
545
focusWithoutScrolling ( e . currentTarget ) ;
536
546
}
537
547
538
- disableTextSelection ( ) ;
548
+ if ( ! allowTextSelectionOnPress ) {
549
+ disableTextSelection ( state . target ) ;
550
+ }
551
+
539
552
triggerPressStart ( e , state . pointerType ) ;
540
553
541
554
addGlobalListener ( window , 'scroll' , onScroll , true ) ;
@@ -588,7 +601,9 @@ export function usePress(props: PressHookProps): PressResult {
588
601
state . activePointerId = null ;
589
602
state . isOverTarget = false ;
590
603
state . ignoreEmulatedMouseEvents = true ;
591
- restoreTextSelection ( ) ;
604
+ if ( ! allowTextSelectionOnPress ) {
605
+ restoreTextSelection ( state . target ) ;
606
+ }
592
607
removeAllGlobalListeners ( ) ;
593
608
} ;
594
609
@@ -625,13 +640,17 @@ export function usePress(props: PressHookProps): PressResult {
625
640
}
626
641
627
642
return pressProps ;
628
- } , [ addGlobalListener , isDisabled , preventFocusOnPress , removeAllGlobalListeners ] ) ;
643
+ } , [ addGlobalListener , isDisabled , preventFocusOnPress , removeAllGlobalListeners , allowTextSelectionOnPress ] ) ;
629
644
630
645
// Remove user-select: none in case component unmounts immediately after pressStart
631
646
// eslint-disable-next-line arrow-body-style
632
647
useEffect ( ( ) => {
633
- return ( ) => restoreTextSelection ( ) ;
634
- } , [ ] ) ;
648
+ return ( ) => {
649
+ if ( ! allowTextSelectionOnPress ) {
650
+ restoreTextSelection ( ref . current . target ) ;
651
+ }
652
+ } ;
653
+ } , [ allowTextSelectionOnPress ] ) ;
635
654
636
655
return {
637
656
isPressed : isPressedProp || isPressed ,
0 commit comments