-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathutils.ts
More file actions
33 lines (24 loc) · 994 Bytes
/
utils.ts
File metadata and controls
33 lines (24 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
export function copyEvent<T extends Event & Record<string, any>>(e: T) {
const newEvent = new (e.constructor as new(type: string) => T)(e.type)
let current = newEvent
// eslint-disable-next-line no-cond-assign
while (current = Object.getPrototypeOf(current)) {
const keys = Object.getOwnPropertyNames(current)
for (const k of keys) {
const item = newEvent[k]
if (typeof item === 'function') continue
Object.defineProperty(newEvent, k, { value: e[k] })
}
}
return newEvent
}
const rootPrefix = '__reactContainer$'
type Keys = `${typeof rootPrefix}${string}` | '_reactRootContainer'
type ReactNode = Partial<Record<Keys, unknown>> & HTMLElement
export function findReactRoot(element: HTMLElement) {
let current: ReactNode | null = element as ReactNode
while (current) {
if (current._reactRootContainer || Object.keys(current).some(key => key.startsWith(rootPrefix))) return current
current = current.parentElement as ReactNode
}
}