Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 41b6f25

Browse files
committed
feat(hoverifier): close overlay if a click happens outside a code view
Stops propagation of handled click events and registers a listener on window that closes the overlay
1 parent d6664ac commit 41b6f25

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/hoverifier.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
share,
1111
switchMap,
1212
takeUntil,
13+
tap,
1314
withLatestFrom,
1415
} from 'rxjs/operators'
1516
import { Key } from 'ts-key-enum'
@@ -253,18 +254,18 @@ export const createHoverifier = ({
253254
): event is MouseEventTrigger & { eventType: T } => event.eventType === type
254255
const allCodeMouseMoves = allPositionsFromEvents.pipe(filter(isEventType('mousemove')))
255256
const allCodeMouseOvers = allPositionsFromEvents.pipe(filter(isEventType('mouseover')))
256-
const allCodeClicks = allPositionsFromEvents.pipe(filter(isEventType('click')))
257+
const allCodeClicks = allPositionsFromEvents.pipe(
258+
filter(isEventType('click')),
259+
// Stop propagation of the click events we handle,
260+
// so that our window click listener can safely close the overlay
261+
// and not worry about bubbling events
262+
tap(({ event }) => event.stopPropagation())
263+
)
257264

258265
const allPositionJumps = new Subject<PositionJump & EventOptions>()
259266

260267
const subscription = new Subscription()
261268

262-
/**
263-
* click events on the code element, ignoring click events caused by the user selecting text.
264-
* Selecting text should not mess with the hover, hover pinning nor the URL.
265-
*/
266-
const codeClicksWithoutSelections = allCodeClicks.pipe(filter(() => window.getSelection().toString() === ''))
267-
268269
// Mouse is moving, don't show the tooltip
269270
subscription.add(
270271
merge(
@@ -299,7 +300,7 @@ export const createHoverifier = ({
299300
share()
300301
)
301302

302-
const codeClickTargets = codeClicksWithoutSelections.pipe(
303+
const codeClickTargets = allCodeClicks.pipe(
303304
filter(({ event }) => event.currentTarget !== null),
304305
map(({ event, ...rest }) => ({
305306
target: event.target as HTMLElement,
@@ -521,13 +522,13 @@ export const createHoverifier = ({
521522
})
522523
)
523524

524-
// When the close button is clicked, unpin, hide and reset the hover
525+
// When the close button is clicked, ESC is pressed or outside a code view is clicked unpin, hide and reset the hover
525526
subscription.add(
526527
merge(
527528
closeButtonClicks,
528-
fromEvent<KeyboardEvent>(window, 'keydown').pipe(filter(event => event.key === Key.Escape))
529-
).subscribe(event => {
530-
event.preventDefault()
529+
fromEvent<KeyboardEvent>(window, 'keydown').pipe(filter(event => event.key === Key.Escape)),
530+
fromEvent<MouseEvent>(window, 'click')
531+
).subscribe(() => {
531532
container.update({
532533
hoverOverlayIsFixed: false,
533534
hoverOverlayPosition: undefined,

0 commit comments

Comments
 (0)