@@ -3,6 +3,7 @@ import * as ReactRedux from 'react-redux';
33import classNames from 'classnames' ;
44import noop from 'lodash/noop' ;
55import { getIsCurrentFileVersion } from '../store' ;
6+ import { MOUSE_PRIMARY } from '../constants' ;
67import { Rect } from '../@types/model' ;
78import './HighlightTarget.scss' ;
89
@@ -20,6 +21,13 @@ const HighlightTarget = (props: Props, ref: React.Ref<HighlightTargetRef>): JSX.
2021 const { annotationId, className, onHover = noop , onSelect = noop , shapes } = props ;
2122 const isCurrentFileVersion = ReactRedux . useSelector ( getIsCurrentFileVersion ) ;
2223
24+ const handleClick = ( event : React . MouseEvent < HighlightTargetRef > ) : void => {
25+ // These are needed to prevent the anchor link from being followed and updating the url location
26+ event . preventDefault ( ) ;
27+ event . stopPropagation ( ) ;
28+ event . nativeEvent . stopImmediatePropagation ( ) ;
29+ } ;
30+
2331 const handleFocus = ( ) : void => {
2432 onSelect ( annotationId ) ;
2533 } ;
@@ -32,6 +40,26 @@ const HighlightTarget = (props: Props, ref: React.Ref<HighlightTargetRef>): JSX.
3240 onHover ( null ) ;
3341 } ;
3442
43+ const handleMouseDown = ( event : React . MouseEvent < HighlightTargetRef > ) : void => {
44+ if ( event . buttons !== MOUSE_PRIMARY ) {
45+ return ;
46+ }
47+ const activeElement = document . activeElement as HTMLElement ;
48+
49+ onSelect ( annotationId ) ;
50+
51+ event . preventDefault ( ) ; // Prevents focus from leaving the anchor immediately in some browsers
52+ event . nativeEvent . stopImmediatePropagation ( ) ; // Prevents document event handlers from executing
53+
54+ // IE11 won't apply the focus to the SVG anchor, so this workaround attempts to blur the existing
55+ // active element.
56+ if ( activeElement && activeElement !== event . currentTarget && activeElement . blur ) {
57+ activeElement . blur ( ) ;
58+ }
59+
60+ event . currentTarget . focus ( ) ;
61+ } ;
62+
3563 return (
3664 // eslint-disable-next-line jsx-a11y/anchor-is-valid
3765 < a
@@ -42,7 +70,9 @@ const HighlightTarget = (props: Props, ref: React.Ref<HighlightTargetRef>): JSX.
4270 data-resin-target = "highlightText"
4371 data-testid = { `ba-AnnotationTarget-${ annotationId } ` }
4472 href = "#"
73+ onClick = { handleClick }
4574 onFocus = { handleFocus }
75+ onMouseDown = { handleMouseDown }
4676 onMouseEnter = { handleMouseEnter }
4777 onMouseLeave = { handleMouseLeave }
4878 role = "button"
0 commit comments