|
1 | 1 | // 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 */ |
3 | 3 |
|
4 | 4 | import {getOwnerWindow, isShadowRoot} from '../domHelpers'; |
5 | 5 | import {shadowDOM} from '@react-stately/flags'; |
@@ -62,20 +62,20 @@ export const getActiveElement = (doc: Document = document): Element | null => { |
62 | 62 | // Type helper to extract the target element type from an event |
63 | 63 | type EventTargetType<T> = T extends SyntheticEvent<infer E, any> ? E : EventTarget; |
64 | 64 |
|
| 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. |
65 | 67 | /** |
66 | 68 | * ShadowDOM safe version of event.target. |
67 | 69 | */ |
68 | 70 | 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>; |
76 | 76 | } |
77 | 77 | } |
78 | | - return target as EventTargetType<T>; |
| 78 | + return event.target as EventTargetType<T>; |
79 | 79 | } |
80 | 80 |
|
81 | 81 | /** |
|
0 commit comments