( handler: React.MouseEventHandler<T>, )
| 29 | // navigating away on mouseup. This guard preempts that navigation when the |
| 30 | // click is the tail end of a text selection gesture. |
| 31 | export const withSelectionGuard = <T = HTMLAnchorElement>( |
| 32 | handler: React.MouseEventHandler<T>, |
| 33 | ): React.MouseEventHandler<T> => { |
| 34 | return (event) => { |
| 35 | const selection = globalThis?.window?.getSelection(); |
| 36 | const hasSelection = |
| 37 | !!selection && selection.anchorOffset !== selection.focusOffset; |
| 38 | if (hasSelection) { |
| 39 | event.preventDefault(); |
| 40 | return; |
| 41 | } |
| 42 | handler(event); |
| 43 | }; |
| 44 | }; |
no test coverage detected