Skip to content

Commit 1a2b8f8

Browse files
authored
fix: getEventTarget returned the wrong target (#9639)
* fix: getEventTarget returned the wrong target * fix lint
1 parent 7009388 commit 1a2b8f8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

packages/@react-aria/utils/src/shadowdom/DOMFunctions.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Source: https://github.com/microsoft/tabster/blob/a89fc5d7e332d48f68d03b1ca6e344489d1c3898/src/Shadowdomize/DOMFunctions.ts#L16
2-
/* eslint-disable rsp-rules/no-non-shadow-contains */
2+
/* eslint-disable rsp-rules/no-non-shadow-contains, rsp-rules/safe-event-target */
33

44
import {getOwnerWindow, isShadowRoot} from '../domHelpers';
55
import {shadowDOM} from '@react-stately/flags';
@@ -62,20 +62,20 @@ export const getActiveElement = (doc: Document = document): Element | null => {
6262
// Type helper to extract the target element type from an event
6363
type EventTargetType<T> = T extends SyntheticEvent<infer E, any> ? E : EventTarget;
6464

65+
// Possibly we can improve the types for this using https://github.com/adobe/react-spectrum/pull/8991/changes#diff-2d491c0c91701d28d08e1cf9fcadbdb21a030b67ab681460c9934140f29127b8R68 but it was more changes than I
66+
// wanted to make to fix the function.
6567
/**
6668
* ShadowDOM safe version of event.target.
6769
*/
6870
export function getEventTarget<T extends Event | SyntheticEvent>(event: T): EventTargetType<T> {
69-
// For React synthetic events, use the native event
70-
let nativeEvent: Event = 'nativeEvent' in event ? (event as SyntheticEvent).nativeEvent : event as Event;
71-
let target = nativeEvent.target!;
72-
73-
if (shadowDOM() && (target as HTMLElement).shadowRoot) {
74-
if (nativeEvent.composedPath) {
75-
return nativeEvent.composedPath()[0] as EventTargetType<T>;
71+
if (shadowDOM() && (event.target instanceof Element) && event.target.shadowRoot) {
72+
if ('composedPath' in event) {
73+
return (event.composedPath()[0] ?? null) as EventTargetType<T>;
74+
} else if ('composedPath' in event.nativeEvent) {
75+
return (event.nativeEvent.composedPath()[0] ?? null) as EventTargetType<T>;
7676
}
7777
}
78-
return target as EventTargetType<T>;
78+
return event.target as EventTargetType<T>;
7979
}
8080

8181
/**

0 commit comments

Comments
 (0)