Skip to content

Commit 9d8e298

Browse files
committed
Add selector order
1 parent b40d274 commit 9d8e298

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

.changeset/cuddly-waves-grin.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+
Add selector order

apps/landing/src/app/page.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,29 @@ export default function HomePage() {
3131
href={URL_PREFIX + '/docs/overview'}
3232
>
3333
<Flex
34+
_active={{
35+
bg: '$negativeBase',
36+
}}
37+
_hover={{
38+
bg: '$title',
39+
}}
3440
alignItems="center"
3541
bg="$text"
3642
borderRadius="100px"
3743
gap="20px"
3844
p="16px 40px"
3945
>
40-
<Box bg="$secondary" borderRadius="100%" boxSize="10px" />
46+
<Box
47+
_active={{
48+
bg: '$third',
49+
}}
50+
_hover={{
51+
bg: '$primary',
52+
}}
53+
bg="$secondary"
54+
borderRadius="100%"
55+
boxSize="10px"
56+
/>
4157
<Flex alignItems="center" gap="10px">
4258
<Text color="$base" typography="buttonL">
4359
Get started

libs/css/src/lib.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,56 @@ use std::fmt;
55
use std::fmt::{Display, Formatter};
66
use std::sync::Mutex;
77

8-
#[derive(Debug, PartialEq, Clone, Hash, Eq, Ord, PartialOrd)]
8+
static SELECTOR_ORDER_MAP: Lazy<HashMap<String, u8>> = Lazy::new(|| {
9+
let mut map = HashMap::new();
10+
map.insert("disabled".to_string(), 5);
11+
map.insert("selected".to_string(), 4);
12+
map.insert("active".to_string(), 3);
13+
map.insert("focus".to_string(), 2);
14+
map.insert("hover".to_string(), 1);
15+
map
16+
});
17+
18+
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
919
pub enum StyleSelector {
1020
Postfix(String),
1121
Prefix(String),
1222
Dual(String, String),
1323
}
1424

25+
impl PartialOrd for StyleSelector {
26+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
27+
Some(self.cmp(other))
28+
}
29+
}
30+
31+
impl Ord for StyleSelector {
32+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
33+
match (self, other) {
34+
(Postfix(a), Postfix(b)) => SELECTOR_ORDER_MAP
35+
.get(a)
36+
.unwrap_or(&0)
37+
.cmp(SELECTOR_ORDER_MAP.get(b).unwrap_or(&0)),
38+
(Prefix(a), Prefix(b)) => a.cmp(b),
39+
(Dual(p1, a), Dual(p2, b)) => {
40+
if p1 == p2 {
41+
SELECTOR_ORDER_MAP
42+
.get(a)
43+
.unwrap_or(&0)
44+
.cmp(SELECTOR_ORDER_MAP.get(b).unwrap_or(&0))
45+
} else {
46+
p1.cmp(p2)
47+
}
48+
}
49+
(Postfix(_), _) => std::cmp::Ordering::Less,
50+
(Prefix(_), Postfix(_)) => std::cmp::Ordering::Greater,
51+
(Prefix(_), _) => std::cmp::Ordering::Less,
52+
(Dual(_, _), Postfix(_)) => std::cmp::Ordering::Greater,
53+
(Dual(_, _), Prefix(_)) => std::cmp::Ordering::Greater,
54+
}
55+
}
56+
}
57+
1558
impl From<&str> for StyleSelector {
1659
fn from(value: &str) -> Self {
1760
if let Some(s) = value.strip_prefix("group") {

libs/sheet/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ mod tests {
285285
sheet.add_property("test", "mx", 2, "40px", None, false);
286286
sheet.add_property("test", "my", 2, "40px", None, false);
287287
assert_debug_snapshot!(sheet.create_css());
288+
289+
let mut sheet = StyleSheet::default();
290+
sheet.add_property("test", "bg", 0, "red", Some(&"hover".into()), false);
291+
sheet.add_property("test", "bg", 0, "blue", Some(&"active".into()), false);
292+
assert_debug_snapshot!(sheet.create_css());
288293
}
289294

290295
#[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+
".test:hover{background:red}.test:active{background:blue}"

0 commit comments

Comments
 (0)