Skip to content

Commit 84d61f8

Browse files
authored
Merge pull request #176 from oleksandr-danylchenko/fix-event-handlers-injection
Added proper floating props events handling prevention
2 parents e12fa5b + a53adb0 commit 84d61f8

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

packages/text-annotator-react/src/TextAnnotatorPopup/TextAnnotatorPopup.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { PointerEvent, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
1+
import { PointerEvent, ReactNode, useEffect, useMemo, useState } from 'react';
2+
23
import { useAnnotator, useSelection } from '@annotorious/react';
3-
import { isRevived, TextAnnotation, TextAnnotator } from '@recogito/text-annotator';
4-
import { isMobile } from './isMobile';
4+
import { isRevived, NOT_ANNOTATABLE_CLASS, TextAnnotation, TextAnnotator } from '@recogito/text-annotator';
5+
56
import {
67
autoUpdate,
78
flip,
@@ -16,6 +17,8 @@ import {
1617
useRole
1718
} from '@floating-ui/react';
1819

20+
import { isMobile } from './isMobile';
21+
1922
import './TextAnnotatorPopup.css';
2023

2124
interface TextAnnotationPopupProps {
@@ -106,12 +109,6 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
106109
};
107110
}, [update]);
108111

109-
// Prevent text-annotator from handling the irrelevant events triggered from the popup
110-
const getStopEventsPropagationProps = useCallback(
111-
() => ({ onPointerUp: (event: PointerEvent<HTMLDivElement>) => event.stopPropagation() }),
112-
[]
113-
);
114-
115112
// Don't shift focus to the floating element if selected via keyboard or on mobile.
116113
const initialFocus = useMemo(() => {
117114
return (event?.type === 'keyup' || event?.type === 'contextmenu' || isMobile()) ? -1 : 0;
@@ -128,11 +125,10 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
128125
returnFocus={false}
129126
initialFocus={initialFocus}>
130127
<div
131-
className="a9s-popup r6o-popup annotation-popup r6o-text-popup not-annotatable"
128+
className={`a9s-popup r6o-popup annotation-popup r6o-text-popup ${NOT_ANNOTATABLE_CLASS}`}
132129
ref={refs.setFloating}
133130
style={floatingStyles}
134-
{...getFloatingProps()}
135-
{...getStopEventsPropagationProps()}>
131+
{...getFloatingProps(getStopEventsPropagationProps())}>
136132
{props.popup({
137133
annotation: selected[0].annotation,
138134
editable: selected[0].editable,
@@ -148,3 +144,14 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
148144
) : null;
149145

150146
}
147+
148+
/**
149+
* Prevent text-annotator from handling the irrelevant events
150+
* triggered from the popup/toolbar/dialog
151+
*/
152+
const getStopEventsPropagationProps = <T extends HTMLElement = HTMLElement>() => ({
153+
onPointerUp: (event: PointerEvent<T>) => event.stopPropagation(),
154+
onPointerDown: (event: PointerEvent<T>) => event.stopPropagation(),
155+
onMouseDown: (event: MouseEvent<T>) => event.stopPropagation(),
156+
onMouseUp: (event: MouseEvent<T>) => event.stopPropagation()
157+
});

0 commit comments

Comments
 (0)