-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathhandle-style.ts
90 lines (79 loc) · 2.19 KB
/
handle-style.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// This is the ID for the core styles of ReactTooltip
const REACT_TOOLTIP_CORE_STYLES_ID = 'react-tooltip-core-styles'
// This is the ID for the visual styles of ReactTooltip
const REACT_TOOLTIP_BASE_STYLES_ID = 'react-tooltip-base-styles'
function injectStyle({
css,
id = REACT_TOOLTIP_BASE_STYLES_ID,
type = 'base',
ref,
}: {
css: string
id?: string
type?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ref?: any
}) {
if (
type === 'core' &&
typeof process !== 'undefined' && // this validation prevents docs from breaking even with `process?`
(process?.env?.REACT_TOOLTIP_DISABLE_CORE_STYLES ||
process?.env?.REACT_APP_REACT_TOOLTIP_DISABLE_CORE_STYLES)
) {
return
}
if (
type !== 'core' &&
typeof process !== 'undefined' && // this validation prevents docs from breaking even with `process?`
(process?.env?.REACT_TOOLTIP_DISABLE_BASE_STYLES ||
process?.env?.REACT_APP_REACT_TOOLTIP_DISABLE_BASE_STYLES)
) {
return
}
if (type === 'core') {
// eslint-disable-next-line no-param-reassign
id = REACT_TOOLTIP_CORE_STYLES_ID
}
if (!ref) {
// eslint-disable-next-line no-param-reassign
ref = {}
}
const { insertAt } = ref
if (!css || typeof document === 'undefined' || document.getElementById(id)) {
return
}
const head = document.head || document.getElementsByTagName('head')[0]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const style: any = document.createElement('style')
style.id = id
style.type = 'text/css'
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild)
} else {
head.appendChild(style)
}
} else {
head.appendChild(style)
}
if (style.styleSheet) {
style.styleSheet.cssText = css
} else {
style.appendChild(document.createTextNode(css))
}
}
function removeStyle({
type = 'base',
id = REACT_TOOLTIP_BASE_STYLES_ID,
}: {
type?: string
id?: string
} = {}) {
if (type === 'core') {
// eslint-disable-next-line no-param-reassign
id = REACT_TOOLTIP_CORE_STYLES_ID
}
const style = document.getElementById(id)
style?.remove()
}
export { injectStyle, removeStyle }