Skip to content

Commit 4f9e82b

Browse files
committed
chore: split css between styles and core styles
1 parent 892e84e commit 4f9e82b

File tree

7 files changed

+93
-59
lines changed

7 files changed

+93
-59
lines changed

rollup-plugins/replace-before-save-file.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,36 @@ export default function replaceBeforeSaveFile(replaceObject = {}) {
4141
regex = new RegExp(key, 'g')
4242

4343
if (bundle[file].code.includes(key)) {
44-
if (replaceObject[key].includes('file:')) {
44+
if (key.includes('css') && replaceObject[key].includes('file:')) {
4545
const [, fileName] = replaceObject[key].split(':')
4646
const fileContent = await readFile(`./dist/${fileName}`, 'utf8')
4747

48-
if (options.file.includes('.min')) {
49-
bundle[file].code = bundle[file].code.replace(
50-
regex,
51-
`\`${cssMinifier(fileContent)}\``,
52-
)
53-
} else {
54-
bundle[file].code = bundle[file].code.replace(regex, `\`${fileContent}\``)
48+
const splittedCSSContent = fileContent.split('/** end - core styles **/')
49+
50+
if (key.includes('core-css')) {
51+
if (options.file.includes('.min')) {
52+
bundle[file].code = bundle[file].code.replace(
53+
regex,
54+
`\`${cssMinifier(splittedCSSContent[0])}\``,
55+
)
56+
} else {
57+
bundle[file].code = bundle[file].code.replace(
58+
regex,
59+
`\`${splittedCSSContent[0]}\``,
60+
)
61+
}
62+
} else if (!key.includes('core-css')) {
63+
if (options.file.includes('.min')) {
64+
bundle[file].code = bundle[file].code.replace(
65+
regex,
66+
`\`${cssMinifier(splittedCSSContent[1])}\``,
67+
)
68+
} else {
69+
bundle[file].code = bundle[file].code.replace(
70+
regex,
71+
`\`${splittedCSSContent[1]}\``,
72+
)
73+
}
5574
}
5675
} else {
5776
bundle[file].code = bundle[file].code.replace(regex, replaceObject[key])

rollup.config.prod.js

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const config = allBuildFormats.map(
100100
// writen in our build process before the javascript files.
101101
"'react-tooltip-css-placeholder'": 'file:react-tooltip.css',
102102
'"react-tooltip-css-placeholder"': 'file:react-tooltip.css',
103+
"'react-tooltip-core-css-placeholder'": 'file:react-tooltip.css',
104+
'"react-tooltip-core-css-placeholder"': 'file:react-tooltip.css',
103105
}),
104106
]
105107

src/components/Tooltip/Tooltip.tsx

+18-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useTooltip } from 'components/TooltipProvider'
55
import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
66
import { getScrollParent } from 'utils/get-scroll-parent'
77
import { computeTooltipPosition } from 'utils/compute-positions'
8+
import coreStyles from './core-styles.module.css'
89
import styles from './styles.module.css'
910
import type { IPosition, ITooltip, PlacesType } from './TooltipTypes'
1011

@@ -583,28 +584,34 @@ const Tooltip = ({
583584
role="tooltip"
584585
className={classNames(
585586
'react-tooltip',
586-
styles['tooltip'],
587+
coreStyles['tooltip'],
587588
styles[variant],
588589
className,
589590
`react-tooltip__place-${actualPlacement}`,
590591
{
591-
[styles['show']]: canShow,
592-
[styles['fixed']]: positionStrategy === 'fixed',
593-
[styles['clickable']]: clickable,
592+
[coreStyles['show']]: canShow,
593+
[coreStyles['fixed']]: positionStrategy === 'fixed',
594+
[coreStyles['clickable']]: clickable,
594595
},
595596
)}
596597
style={{ ...externalStyles, ...inlineStyles }}
597598
ref={tooltipRef}
598599
>
599600
{content}
600601
<WrapperElement
601-
className={classNames('react-tooltip-arrow', styles['arrow'], classNameArrow, {
602-
/**
603-
* changed from dash `no-arrow` to camelcase because of:
604-
* https://github.com/indooorsman/esbuild-css-modules-plugin/issues/42
605-
*/
606-
[styles['noArrow']]: noArrow,
607-
})}
602+
className={classNames(
603+
'react-tooltip-arrow',
604+
coreStyles['arrow'],
605+
styles['arrow'],
606+
classNameArrow,
607+
{
608+
/**
609+
* changed from dash `no-arrow` to camelcase because of:
610+
* https://github.com/indooorsman/esbuild-css-modules-plugin/issues/42
611+
*/
612+
[coreStyles['noArrow']]: noArrow,
613+
},
614+
)}
608615
style={inlineArrowStyles}
609616
ref={tooltipArrowRef}
610617
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.tooltip {
2+
visibility: hidden;
3+
width: max-content;
4+
position: absolute;
5+
top: 0;
6+
left: 0;
7+
padding: 8px 16px;
8+
border-radius: 3px;
9+
font-size: 90%;
10+
pointer-events: none;
11+
opacity: 0;
12+
transition: opacity 0.3s ease-out;
13+
will-change: opacity, visibility;
14+
}
15+
16+
.fixed {
17+
position: fixed;
18+
}
19+
20+
.arrow {
21+
position: absolute;
22+
background: inherit;
23+
}
24+
25+
.noArrow {
26+
display: none;
27+
}
28+
29+
.clickable {
30+
pointer-events: auto;
31+
}
32+
33+
.show {
34+
visibility: visible;
35+
opacity: var(--rt-opacity);
36+
}
37+
38+
/** end - core styles **/

src/components/Tooltip/styles.module.css

-34
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
1-
.tooltip {
2-
visibility: hidden;
3-
width: max-content;
4-
position: absolute;
5-
top: 0;
6-
left: 0;
7-
padding: 8px 16px;
8-
border-radius: 3px;
9-
font-size: 90%;
10-
pointer-events: none;
11-
opacity: 0;
12-
transition: opacity 0.3s ease-out;
13-
will-change: opacity, visibility;
14-
}
15-
16-
.fixed {
17-
position: fixed;
18-
}
19-
201
.arrow {
21-
position: absolute;
22-
background: inherit;
232
width: 8px;
243
height: 8px;
254
transform: rotate(45deg);
265
}
276

28-
.noArrow {
29-
display: none;
30-
}
31-
32-
.clickable {
33-
pointer-events: auto;
34-
}
35-
36-
.show {
37-
visibility: visible;
38-
opacity: var(--rt-opacity);
39-
}
40-
417
/** Types variant **/
428
.dark {
439
background: var(--rt-color-dark);

src/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import type {
1616
import type { ITooltipController } from './components/TooltipController/TooltipControllerTypes'
1717
import type { ITooltipWrapper } from './components/TooltipProvider/TooltipProviderTypes'
1818

19-
// this content will be replaced in build time with the `react-tooltip.css` builded content
19+
// those content will be replaced in build time with the `react-tooltip.css` builded content
20+
const TooltipCoreStyles = 'react-tooltip-core-css-placeholder'
2021
const TooltipStyles = 'react-tooltip-css-placeholder'
2122

23+
injectStyle(TooltipCoreStyles, 'react-tooltip-core-styles')
2224
injectStyle(TooltipStyles)
2325

2426
export { TooltipController as Tooltip } from './components/TooltipController'

src/utils/handle-style.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const REACT_TOOLTIP_STYLES_ID = 'react-tooltip-styles'
22

3-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4-
function injectStyle(css: string, ref?: any) {
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, default-param-last
4+
function injectStyle(css: string, id = REACT_TOOLTIP_STYLES_ID, ref?: any) {
55
if (!ref) {
66
// eslint-disable-next-line no-param-reassign
77
ref = {}
@@ -15,7 +15,7 @@ function injectStyle(css: string, ref?: any) {
1515
const head = document.head || document.getElementsByTagName('head')[0]
1616
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1717
const style: any = document.createElement('style')
18-
style.id = REACT_TOOLTIP_STYLES_ID
18+
style.id = id
1919
style.type = 'text/css'
2020

2121
if (insertAt === 'top') {
@@ -35,8 +35,8 @@ function injectStyle(css: string, ref?: any) {
3535
}
3636
}
3737

38-
function removeStyle() {
39-
const style = document.getElementById(REACT_TOOLTIP_STYLES_ID)
38+
function removeStyle(id = REACT_TOOLTIP_STYLES_ID) {
39+
const style = document.getElementById(id)
4040
style?.remove()
4141
}
4242

0 commit comments

Comments
 (0)