Skip to content

Commit 5193f2c

Browse files
committed
Support globalCss
1 parent 41a90b6 commit 5193f2c

File tree

6 files changed

+101
-20
lines changed

6 files changed

+101
-20
lines changed

libs/extractor/src/extractor/extract_style_from_expression.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,14 @@ pub fn extract_style_from_expression<'a>(
591591
});
592592

593593
let selector = selector.clone().map(|s| {
594-
if let Some(params) = params
595-
&& let StyleSelector::Selector(selector) = s
596-
{
597-
StyleSelector::Selector(format!("{}({})", selector, params))
594+
if let Some(params) = params {
595+
if let StyleSelector::Selector(selector) = s {
596+
StyleSelector::Selector(format!("{}({})", selector, params))
597+
} else if let StyleSelector::Global(selector, file) = s {
598+
StyleSelector::Global(format!("{}({})", selector, params), file)
599+
} else {
600+
s
601+
}
598602
} else {
599603
s
600604
}

libs/extractor/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7397,5 +7397,30 @@ keyframes({
73977397
)
73987398
.unwrap()
73997399
));
7400+
7401+
reset_class_map();
7402+
assert_debug_snapshot!(ToBTreeSet::from(
7403+
extract(
7404+
"test.tsx",
7405+
r#"import {globalCss} from '@devup-ui/core'
7406+
globalCss({
7407+
"_is": {
7408+
params: ["test", variable],
7409+
_hover: {
7410+
bg: "blue"
7411+
},
7412+
bg: "red"
7413+
}
7414+
})
7415+
"#,
7416+
ExtractOption {
7417+
package: "@devup-ui/core".to_string(),
7418+
css_dir: "@devup-ui/core".to_string(),
7419+
single_css: false,
7420+
import_main_css: false
7421+
}
7422+
)
7423+
.unwrap()
7424+
));
74007425
}
74017426
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {globalCss} from '@devup-ui/core'\n globalCss({\n \"_is\": {\n params: [\"test\", variable],\n _hover: {\n bg: \"blue\"\n },\n bg: \"red\"\n }\n })\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: false, import_main_css: false\n}).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "blue",
11+
level: 0,
12+
selector: Some(
13+
Global(
14+
"*:is(test):hover",
15+
"test.tsx",
16+
),
17+
),
18+
style_order: Some(
19+
0,
20+
),
21+
},
22+
),
23+
Static(
24+
ExtractStaticStyle {
25+
property: "background",
26+
value: "red",
27+
level: 0,
28+
selector: Some(
29+
Global(
30+
"*:is(test)",
31+
"test.tsx",
32+
),
33+
),
34+
style_order: Some(
35+
0,
36+
),
37+
},
38+
),
39+
},
40+
code: "import \"@devup-ui/core/devup-ui-0.css\";\n;\n",
41+
}

packages/eslint-plugin/src/rules/no-useless-responsive/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const noUselessResponsive = createRule({
9898
if (
9999
devupContext &&
100100
node.key.type === AST_NODE_TYPES.Identifier &&
101-
node.key.name === 'imports'
101+
['imports', 'params', 'fontFaces'].includes(node.key.name)
102102
) {
103103
devupContext = null
104104
}

packages/react/src/types/props/selector/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ResponsiveValue } from '../../responsive-value'
44
import type { DevupTheme } from '../../theme'
55
import type { DevupProps } from '../index'
66

7-
type CamelCase<S extends string> =
7+
export type CamelCase<S extends string> =
88
S extends Lowercase<S>
99
? S extends `${infer F}-${infer RF}${infer R}`
1010
? `${F}${Uppercase<RF>}${CamelCase<R>}`
@@ -20,10 +20,7 @@ export type DevupThemeSelectorProps = keyof DevupTheme extends undefined
2020
Record<`_theme${PascalCase<keyof DevupTheme>}`, SelectorProps<DevupProps>>
2121
>
2222

23-
export type NormalizedSelector<T> = Exclude<
24-
T,
25-
`:-${string}` | `::-${string}` | `${string}()`
26-
>
23+
export type NormalizedSelector<T> = Exclude<T, `:-${string}` | `::-${string}`>
2724
export type SimpleSelector = NormalizedSelector<SimplePseudos>
2825

2926
export type AdvancedSelector = NormalizedSelector<AdvancedPseudos>

packages/react/src/utils/global-css.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
import type { DevupCommonProps } from '../types/props'
22
import type {
3+
AdvancedSelector,
4+
CamelCase,
35
DevupThemeSelectorProps,
46
ExtractSelector,
57
SimpleSelector,
68
} from '../types/props/selector'
79
import type { DevupSelectorProps } from '../types/props/selector'
810

9-
type GlobalCssKeys =
10-
| `*${SimpleSelector | ''}`
11-
| `${keyof HTMLElementTagNameMap}${SimpleSelector | ''}`
12-
| `${keyof SVGElementTagNameMap}${SimpleSelector | ''}`
13-
| `_${ExtractSelector<SimpleSelector>}`
14-
| (string & {})
11+
type GlobalCssKeys<T extends string> =
12+
| `*${T}`
13+
| `${keyof HTMLElementTagNameMap}${T}`
14+
| `${keyof SVGElementTagNameMap}${T}`
15+
| `_${CamelCase<ExtractSelector<T>>}`
1516

1617
type GlobalCssProps = {
17-
[K in GlobalCssKeys]?:
18-
| DevupCommonProps
19-
| DevupSelectorProps
20-
| DevupThemeSelectorProps
18+
[K in GlobalCssKeys<AdvancedSelector>]?: DevupCommonProps &
19+
DevupSelectorProps &
20+
DevupThemeSelectorProps & {
21+
params: string[]
22+
}
23+
} & {
24+
[K in GlobalCssKeys<
25+
Exclude<AdvancedSelector, SimpleSelector>
26+
>]?: DevupCommonProps &
27+
DevupSelectorProps &
28+
DevupThemeSelectorProps & {
29+
params?: string[]
30+
}
31+
} & {
32+
[K in GlobalCssKeys<SimpleSelector> | (string & {})]?: DevupCommonProps &
33+
DevupSelectorProps &
34+
DevupThemeSelectorProps
2135
}
2236

2337
interface FontFaceProps {

0 commit comments

Comments
 (0)