Skip to content

Commit dd5ce79

Browse files
committed
Fix selector order on theme
1 parent ca807e5 commit dd5ce79

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

.changeset/violet-masks-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Fix selector order in theme

libs/css/src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ use std::sync::Mutex;
1010

1111
static SELECTOR_ORDER_MAP: Lazy<HashMap<String, u8>> = Lazy::new(|| {
1212
let mut map = HashMap::new();
13-
map.insert("&:disabled".to_string(), 5);
14-
map.insert("&:selected".to_string(), 4);
15-
map.insert("&:active".to_string(), 3);
16-
map.insert("&:focus".to_string(), 2);
17-
map.insert("&:hover".to_string(), 1);
13+
for (idx, selector) in [
14+
"hover",
15+
"focus-visible",
16+
"focus",
17+
"active",
18+
"selected",
19+
"disabled",
20+
]
21+
.into_iter()
22+
.enumerate()
23+
{
24+
map.insert(format!("&:{}", selector), idx as u8);
25+
}
1826
map
1927
});
2028

@@ -41,18 +49,30 @@ impl PartialOrd for StyleSelector {
4149
}
4250
}
4351

52+
fn get_selector_order(selector: &str) -> u8 {
53+
// & count
54+
let t = if selector.chars().filter(|c| c == &'&').count() == 1 {
55+
selector
56+
.split('&')
57+
.last()
58+
.map(|a| format!("&{}", a))
59+
.unwrap_or(selector.to_string())
60+
} else {
61+
selector.to_string()
62+
};
63+
64+
*SELECTOR_ORDER_MAP
65+
.get(&t)
66+
.unwrap_or(if t.starts_with("&") { &0 } else { &99 })
67+
}
68+
4469
impl Ord for StyleSelector {
4570
fn cmp(&self, other: &Self) -> Ordering {
4671
match (self, other) {
4772
(StyleSelector::Media(a), StyleSelector::Media(b)) => a.cmp(b),
48-
(StyleSelector::Selector(a), StyleSelector::Selector(b)) => SELECTOR_ORDER_MAP
49-
.get(a)
50-
.unwrap_or(if a.starts_with("&") { &0 } else { &99 })
51-
.cmp(SELECTOR_ORDER_MAP.get(b).unwrap_or(if b.starts_with("&") {
52-
&0
53-
} else {
54-
&99
55-
})),
73+
(StyleSelector::Selector(a), StyleSelector::Selector(b)) => {
74+
get_selector_order(a).cmp(&get_selector_order(b))
75+
}
5676
(StyleSelector::Media(_), StyleSelector::Selector(_)) => Ordering::Greater,
5777
(StyleSelector::Selector(_), StyleSelector::Media(_)) => Ordering::Less,
5878
}

libs/sheet/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,25 @@ mod tests {
515515
sheet.add_property("test", "mx", 0, "51px", Some(&"themeLight".into()), None);
516516
sheet.add_property("test", "mx", 0, "42px", None, None);
517517
assert_debug_snapshot!(sheet.create_css());
518+
519+
let mut sheet = StyleSheet::default();
520+
sheet.add_property(
521+
"test",
522+
"mx",
523+
0,
524+
"50px",
525+
Some(&["themeLight", "active"].into()),
526+
None,
527+
);
528+
sheet.add_property(
529+
"test",
530+
"mx",
531+
0,
532+
"50px",
533+
Some(&["themeLight", "hover"].into()),
534+
None,
535+
);
536+
assert_debug_snapshot!(sheet.create_css());
518537
}
519538

520539
#[test]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: libs/sheet/src/lib.rs
3+
expression: sheet.create_css()
4+
---
5+
":root[data-theme=light] .test:hover{margin-left:50px;margin-right:50px;}:root[data-theme=light] .test:active{margin-left:50px;margin-right:50px;}"

0 commit comments

Comments
 (0)