Skip to content

Commit 80fc7d2

Browse files
committed
feat: update handle-style utils
1 parent 0db3eee commit 80fc7d2

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import type { ITooltipWrapper } from './components/TooltipProvider/TooltipProvid
2020
const TooltipCoreStyles = 'react-tooltip-core-css-placeholder'
2121
const TooltipStyles = 'react-tooltip-css-placeholder'
2222

23-
injectStyle(TooltipCoreStyles, 'react-tooltip-core-styles')
24-
injectStyle(TooltipStyles)
23+
injectStyle({ css: TooltipCoreStyles, type: 'core' })
24+
injectStyle({ css: TooltipStyles })
2525

2626
export { TooltipController as Tooltip } from './components/TooltipController'
2727
export { TooltipProvider, TooltipWrapper } from './components/TooltipProvider'

src/utils/handle-style.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1-
const REACT_TOOLTIP_STYLES_ID = 'react-tooltip-styles'
1+
// This is the ID for the core styles of ReactTooltip
2+
const REACT_TOOLTIP_CORE_STYLES_ID = 'react-tooltip-core-styles'
3+
// This is the ID for the visual styles of ReactTooltip
4+
const REACT_TOOLTIP_BASE_STYLES_ID = 'react-tooltip-base-styles'
25

36
// eslint-disable-next-line @typescript-eslint/no-explicit-any, default-param-last
4-
function injectStyle(css: string, id = REACT_TOOLTIP_STYLES_ID, ref?: any) {
7+
function injectStyle({
8+
css,
9+
id = REACT_TOOLTIP_BASE_STYLES_ID,
10+
type = 'base',
11+
ref,
12+
}: {
13+
css: string
14+
id?: string
15+
type?: string
16+
ref?: any
17+
}) {
18+
if (type === 'core' && process.env.REACT_TOOLTIP_DISABLE_CORE_STYLES) {
19+
return
20+
}
21+
22+
if (type !== 'core' && process.env.REACT_TOOLTIP_DISABLE_BASE_STYLES) {
23+
return
24+
}
25+
26+
if (type === 'core') {
27+
// eslint-disable-next-line no-param-reassign
28+
id = REACT_TOOLTIP_CORE_STYLES_ID
29+
}
30+
531
if (!ref) {
632
// eslint-disable-next-line no-param-reassign
733
ref = {}
834
}
935
const { insertAt } = ref
1036

11-
if (!css || typeof document === 'undefined' || document.getElementById(REACT_TOOLTIP_STYLES_ID)) {
37+
if (!css || typeof document === 'undefined' || document.getElementById(id)) {
1238
return
1339
}
1440

@@ -35,7 +61,18 @@ function injectStyle(css: string, id = REACT_TOOLTIP_STYLES_ID, ref?: any) {
3561
}
3662
}
3763

38-
function removeStyle(id = REACT_TOOLTIP_STYLES_ID) {
64+
function removeStyle({
65+
type = 'base',
66+
id = REACT_TOOLTIP_BASE_STYLES_ID,
67+
}: {
68+
type?: string
69+
id?: string
70+
}) {
71+
if (type === 'core') {
72+
// eslint-disable-next-line no-param-reassign
73+
id = REACT_TOOLTIP_CORE_STYLES_ID
74+
}
75+
3976
const style = document.getElementById(id)
4077
style?.remove()
4178
}

0 commit comments

Comments
 (0)