-
Notifications
You must be signed in to change notification settings - Fork 240
/
cssPointerEvents.ts
74 lines (63 loc) 路 1.96 KB
/
cssPointerEvents.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {PointerEventsCheckLevel} from '../../options'
import {Config} from '../../setup'
import {ApiLevel, getLevelRef} from '..'
import {getWindow} from '../misc/getWindow'
export function hasPointerEvents(element: Element): boolean {
const window = getWindow(element)
for (
let el: Element | null = element;
el?.ownerDocument;
el = el.parentElement
) {
const pointerEvents = window.getComputedStyle(el).pointerEvents
if (pointerEvents && !['inherit', 'unset'].includes(pointerEvents)) {
return pointerEvents !== 'none'
}
}
return true
}
const PointerEventsCheck = Symbol('Last check for pointer-events')
declare global {
interface Element {
[PointerEventsCheck]?: {
[k in ApiLevel]?: object
} & {
result: boolean
}
}
}
export function assertPointerEvents(config: Config, element: Element) {
const lastCheck = element[PointerEventsCheck]
const needsCheck =
config.pointerEventsCheck !== PointerEventsCheckLevel.Never &&
(!lastCheck ||
(hasBitFlag(
config.pointerEventsCheck,
PointerEventsCheckLevel.EachApiCall,
) &&
lastCheck[ApiLevel.Call] !== getLevelRef(config, ApiLevel.Call)) ||
(hasBitFlag(
config.pointerEventsCheck,
PointerEventsCheckLevel.EachTrigger,
) &&
lastCheck[ApiLevel.Trigger] !== getLevelRef(config, ApiLevel.Trigger)))
if (!needsCheck) {
return
}
const result = hasPointerEvents(element)
element[PointerEventsCheck] = {
[ApiLevel.Call]: getLevelRef(config, ApiLevel.Call),
[ApiLevel.Trigger]: getLevelRef(config, ApiLevel.Trigger),
result,
}
if (!result) {
throw new Error(
'Unable to perform pointer interaction as the element has or inherits pointer-events set to "none".',
)
}
}
// With the eslint rule and prettier the bitwise operation isn't nice to read
function hasBitFlag(conf: number, flag: number) {
// eslint-disable-next-line no-bitwise
return (conf & flag) > 0
}