Skip to content

Commit ef36c44

Browse files
committed
Fix shadow issue
1 parent f47038e commit ef36c44

33 files changed

+959
-133
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/css/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod optimize_value;
99
pub mod rm_css_comment;
1010
mod selector_separator;
1111
pub mod style_selector;
12+
pub mod theme_tokens;
1213
pub mod utils;
1314

1415
use std::collections::BTreeMap;

libs/css/src/theme_tokens.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::collections::BTreeMap;
2+
use std::sync::{LazyLock, RwLock};
3+
4+
#[derive(Default, Debug)]
5+
struct ThemeTokenRegistry {
6+
length: BTreeMap<String, Vec<u8>>,
7+
shadow: BTreeMap<String, Vec<u8>>,
8+
}
9+
10+
static TOKEN_REGISTRY: LazyLock<RwLock<ThemeTokenRegistry>> =
11+
LazyLock::new(|| RwLock::new(ThemeTokenRegistry::default()));
12+
13+
pub fn set_theme_token_levels(
14+
length: BTreeMap<String, Vec<u8>>,
15+
shadow: BTreeMap<String, Vec<u8>>,
16+
) {
17+
if let Ok(mut registry) = TOKEN_REGISTRY.write() {
18+
registry.length = length;
19+
registry.shadow = shadow;
20+
}
21+
}
22+
23+
/// Look up a `$token` in the length and shadow registries.
24+
/// Returns the responsive breakpoint levels if the token is defined
25+
/// with more than one level, regardless of which CSS property it's used on.
26+
pub fn get_responsive_theme_token(value: &str) -> Option<Vec<u8>> {
27+
let token = value.strip_prefix('$')?;
28+
let registry = TOKEN_REGISTRY.read().ok()?;
29+
30+
registry
31+
.length
32+
.get(token)
33+
.or_else(|| registry.shadow.get(token))
34+
.filter(|levels| levels.len() > 1)
35+
.cloned()
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn test_get_responsive_theme_token() {
44+
let mut length = BTreeMap::new();
45+
length.insert("containerX".to_string(), vec![0, 2]);
46+
let mut shadow = BTreeMap::new();
47+
shadow.insert("card".to_string(), vec![0, 3]);
48+
set_theme_token_levels(length, shadow);
49+
50+
assert_eq!(get_responsive_theme_token("$containerX"), Some(vec![0, 2]));
51+
assert_eq!(get_responsive_theme_token("$card"), Some(vec![0, 3]));
52+
assert_eq!(get_responsive_theme_token("$unknown"), None);
53+
assert_eq!(get_responsive_theme_token("noprefix"), None);
54+
}
55+
}

libs/extractor/src/extract_style/extract_static_style.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{Debug, Formatter};
2+
13
use css::{
24
optimize_multi_css_value::{check_multi_css_optimize, optimize_mutli_css_value},
35
optimize_value::optimize_value,
@@ -12,7 +14,14 @@ use crate::{
1214
utils::{convert_value, gcd},
1315
};
1416

15-
#[derive(Debug, PartialEq, Clone, Eq, Hash, Ord, PartialOrd)]
17+
#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash, Ord, PartialOrd, Default)]
18+
pub enum ThemeTokenResolution {
19+
#[default]
20+
CssVariable,
21+
FirstValue,
22+
}
23+
24+
#[derive(PartialEq, Clone, Eq, Hash, Ord, PartialOrd)]
1625
pub struct ExtractStaticStyle {
1726
/// property
1827
pub property: String,
@@ -26,6 +35,21 @@ pub struct ExtractStaticStyle {
2635
pub style_order: Option<u8>,
2736
/// CSS layer name (from vanilla-extract layer())
2837
pub layer: Option<String>,
38+
/// How theme tokens should be resolved when converting to CSS.
39+
pub theme_token_resolution: ThemeTokenResolution,
40+
}
41+
42+
impl Debug for ExtractStaticStyle {
43+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44+
f.debug_struct("ExtractStaticStyle")
45+
.field("property", &self.property)
46+
.field("value", &self.value)
47+
.field("level", &self.level)
48+
.field("selector", &self.selector)
49+
.field("style_order", &self.style_order)
50+
.field("layer", &self.layer)
51+
.finish()
52+
}
2953
}
3054

3155
impl ExtractStaticStyle {
@@ -55,6 +79,7 @@ impl ExtractStaticStyle {
5579
selector: selector.map(optimize_selector),
5680
style_order: None,
5781
layer: None,
82+
theme_token_resolution: ThemeTokenResolution::CssVariable,
5883
}
5984
}
6085

@@ -88,9 +113,15 @@ impl ExtractStaticStyle {
88113
selector,
89114
style_order: Some(0),
90115
layer: None,
116+
theme_token_resolution: ThemeTokenResolution::CssVariable,
91117
}
92118
}
93119

120+
pub fn with_theme_token_resolution(mut self, resolution: ThemeTokenResolution) -> Self {
121+
self.theme_token_resolution = resolution;
122+
self
123+
}
124+
94125
/// Get the layer name
95126
pub fn layer(&self) -> Option<&str> {
96127
self.layer.as_deref()
@@ -115,6 +146,10 @@ impl ExtractStaticStyle {
115146
pub fn style_order(&self) -> Option<u8> {
116147
self.style_order
117148
}
149+
150+
pub fn theme_token_resolution(&self) -> ThemeTokenResolution {
151+
self.theme_token_resolution
152+
}
118153
}
119154

120155
impl ExtractStyleProperty for ExtractStaticStyle {

libs/extractor/src/extract_style/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub(super) mod extract_dynamic_style;
44
pub(super) mod extract_font_face;
55
pub(super) mod extract_import;
66
pub(super) mod extract_keyframes;
7-
pub(super) mod extract_static_style;
7+
pub mod extract_static_style;
88
pub mod extract_style_value;
99
pub mod style_property;
1010

libs/extractor/src/extractor/extract_global_style_from_expression.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
extract_style_value::ExtractStyleValue,
99
},
1010
extractor::{
11-
GlobalExtractResult, extract_style_from_expression::extract_style_from_expression,
11+
GlobalExtractResult,
12+
extract_style_from_expression::{LiteralHandling, extract_style_from_expression},
1213
},
1314
utils::{get_string_by_literal_expression, get_string_by_property_key},
1415
};
@@ -166,6 +167,7 @@ pub fn extract_global_style_from_expression<'a>(
166167
},
167168
file.to_string(),
168169
)),
170+
LiteralHandling::ExpandResponsiveThemeToken,
169171
);
170172

171173
// Filter out @layer property from styles and set layer on remaining styles

libs/extractor/src/extractor/extract_keyframes_from_expression.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
extract_style::{extract_keyframes::ExtractKeyframes, extract_style_value::ExtractStyleValue},
44
extractor::{
55
ExtractResult, KeyframesExtractResult,
6-
extract_style_from_expression::extract_style_from_expression,
6+
extract_style_from_expression::{LiteralHandling, extract_style_from_expression},
77
},
88
utils::get_string_by_property_key,
99
};
@@ -23,8 +23,14 @@ pub fn extract_keyframes_from_expression<'a>(
2323
if let ObjectPropertyKind::ObjectProperty(o) = p
2424
&& let Some(name) = get_string_by_property_key(&o.key)
2525
{
26-
let ExtractResult { styles, .. } =
27-
extract_style_from_expression(ast_builder, None, &mut o.value, 0, &None);
26+
let ExtractResult { styles, .. } = extract_style_from_expression(
27+
ast_builder,
28+
None,
29+
&mut o.value,
30+
0,
31+
&None,
32+
LiteralHandling::ExpandResponsiveThemeToken,
33+
);
2834

2935
let mut styles = styles
3036
.into_iter()

0 commit comments

Comments
 (0)