Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-cherries-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/react": patch
---

Add selectors
5 changes: 5 additions & 0 deletions .changeset/nasty-badgers-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Add default value
5 changes: 5 additions & 0 deletions .changeset/sixty-wombats-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Fix conditional style issue
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
cobertura.xml
build
dist
node_modules
Expand Down
27 changes: 21 additions & 6 deletions libs/css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ impl Ord for StyleSelector {

impl From<&str> for StyleSelector {
fn from(value: &str) -> Self {
if value.contains(":") {
let t: Vec<_> = value.split(":").collect();
if value.contains("&") {
let t: Vec<_> = value.split("&").collect();
if let Prefix(v) = t[0].into() {
Dual(v, t[1].to_string())
if t[1].is_empty() {
Prefix(v)
} else {
Dual(v, t[1].to_string())
}
} else {
Postfix(t[1].to_string())
}
Expand All @@ -84,6 +88,8 @@ impl From<&str> for StyleSelector {
))
} else if value == "print" {
Media("print".to_string())
} else if value.ends_with(" ") {
Prefix(value.trim().to_string())
} else {
Postfix(to_kebab_case(value))
}
Expand Down Expand Up @@ -132,6 +138,7 @@ pub fn merge_selector(class_name: &str, selector: Option<&StyleSelector>) -> Str
pub enum SelectorSeparator {
Single,
Double,
None,
}
impl Display for SelectorSeparator {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Expand All @@ -141,6 +148,7 @@ impl Display for SelectorSeparator {
match self {
SelectorSeparator::Single => ":",
SelectorSeparator::Double => "::",
SelectorSeparator::None => "",
}
)
}
Expand All @@ -166,7 +174,9 @@ static DOUBLE_SEPARATOR: Lazy<HashSet<&str>> = Lazy::new(|| {
});

pub fn get_selector_separator(key: &str) -> SelectorSeparator {
if DOUBLE_SEPARATOR.contains(key) {
if key.starts_with(":") || key.is_empty() {
SelectorSeparator::None
} else if DOUBLE_SEPARATOR.contains(key) {
SelectorSeparator::Double
} else {
SelectorSeparator::Single
Expand Down Expand Up @@ -669,7 +679,7 @@ mod tests {
);

assert_eq!(
StyleSelector::from("themeDark:placeholder"),
StyleSelector::from("themeDark&placeholder"),
Dual(
":root[data-theme=dark]".to_string(),
"placeholder".to_string()
Expand All @@ -683,6 +693,11 @@ mod tests {
StyleSelector::from("themeLight"),
Prefix(":root[data-theme=light]".to_string())
);

assert_eq!(
StyleSelector::from("*[aria=disabled='true'] &:hover"),
Dual("*[aria=disabled='true']".to_string(), ":hover".to_string())
);
}

#[test]
Expand Down Expand Up @@ -712,7 +727,7 @@ mod tests {
);

assert_eq!(
merge_selector("cls", Some(&"themeDark:hover".into()),),
merge_selector("cls", Some(&"themeDark&hover".into()),),
":root[data-theme=dark] .cls:hover"
);
}
Expand Down
57 changes: 53 additions & 4 deletions libs/extractor/src/gen_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,59 @@ fn gen_style<'a>(
(None, None) => {
return vec![];
}
(Some(c), None) | (None, Some(c)) => {
gen_style(ast_builder, c)
.into_iter()
.for_each(|p| properties.push(p));
(None, Some(c)) => {
gen_style(ast_builder, c).into_iter().for_each(|p| {
if let ObjectPropertyKind::ObjectProperty(p) = p {
properties.push(ObjectPropertyKind::ObjectProperty(
ast_builder.alloc_object_property(
SPAN,
PropertyKind::Init,
p.key.clone_in(ast_builder.allocator),
Expression::ConditionalExpression(
ast_builder.alloc_conditional_expression(
SPAN,
condition.clone_in(ast_builder.allocator),
Expression::Identifier(
ast_builder
.alloc_identifier_reference(SPAN, "undefined"),
),
p.value.clone_in(ast_builder.allocator),
),
),
false,
false,
false,
),
))
}
});
}
(Some(c), None) => {
gen_style(ast_builder, c).into_iter().for_each(|p| {
if let ObjectPropertyKind::ObjectProperty(p) = p {
properties.push(ObjectPropertyKind::ObjectProperty(
ast_builder.alloc_object_property(
SPAN,
PropertyKind::Init,
p.key.clone_in(ast_builder.allocator),
Expression::ConditionalExpression(
ast_builder.alloc_conditional_expression(
SPAN,
condition.clone_in(ast_builder.allocator),
p.value.clone_in(ast_builder.allocator),
Expression::Identifier(
ast_builder
.alloc_identifier_reference(SPAN, "undefined"),
),
),
),
false,
false,
false,
),
))
}
});
}
(Some(c), Some(a)) => {
let collect_c = gen_style(ast_builder, c);
Expand Down
81 changes: 81 additions & 0 deletions libs/extractor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,32 @@ mod tests {
"test.tsx",
r#"import { Box } from "@devup-ui/core";
<Box margin={a === b ? undefined : 2} />;
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap());

reset_class_map();
assert_debug_snapshot!(extract(
"test.tsx",
r#"import { Box } from "@devup-ui/core";
<Box margin={a === b ? `${a}px` : undefined} />;
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap());

reset_class_map();
assert_debug_snapshot!(extract(
"test.tsx",
r#"import { Box } from "@devup-ui/core";
<Box margin={a === b ? null : `${b}px`} />;
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
Expand Down Expand Up @@ -2117,4 +2143,59 @@ import {Button} from '@devup/ui'
)
.unwrap());
}

#[test]
#[serial]
fn custom_selector() {
reset_class_map();
assert_debug_snapshot!(extract(
"test.js",
r#"import {Box} from '@devup-ui/core'
<Box selectors={{
"&[aria-diabled='true']": {
opacity: 0.5
}
}} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap());

reset_class_map();
assert_debug_snapshot!(extract(
"test.js",
r#"import {Box} from '@devup-ui/core'
<Box selectors={{
"*[aria-diabled='true'] &:hover": {
opacity: 0.5
}
}} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap());

reset_class_map();
assert_debug_snapshot!(extract(
"test.js",
r#"import {Box} from '@devup-ui/core'
<Box selectors={{
"*[aria-diabled='true'] &": {
opacity: 0.5
}
}} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: libs/extractor/src/lib.rs
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"*[aria-diabled='true'] &:hover\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
---
ExtractOutput {
styles: [
Static(
ExtractStaticStyle {
property: "opacity",
value: "0.5",
level: 0,
selector: Some(
Dual(
"*[aria-diabled='true']",
":hover",
),
),
basic: false,
},
),
],
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: libs/extractor/src/lib.rs
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"*[aria-diabled='true'] &\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
---
ExtractOutput {
styles: [
Static(
ExtractStaticStyle {
property: "opacity",
value: "0.5",
level: 0,
selector: Some(
Prefix(
"*[aria-diabled='true']",
),
),
basic: false,
},
),
],
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: libs/extractor/src/lib.rs
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"&[aria-diabled='true']\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
---
ExtractOutput {
styles: [
Static(
ExtractStaticStyle {
property: "opacity",
value: "0.5",
level: 0,
selector: Some(
Postfix(
"[aria-diabled='true']",
),
),
basic: false,
},
),
],
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: libs/extractor/src/lib.rs
expression: "extract(\"test.tsx\",\nr#\"import { Box } from \"@devup-ui/core\";\n<Box margin={a === b ? `${a}px` : undefined} />;\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
---
ExtractOutput {
styles: [
Dynamic(
ExtractDynamicStyle {
property: "margin",
level: 0,
identifier: "`${a}px`",
selector: None,
},
),
],
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={a === b ? \"d0\" : \"\"} style={{ \"--d1\": a === b ? `${a}px` : undefined }} />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: libs/extractor/src/lib.rs
expression: "extract(\"test.tsx\",\nr#\"import { Box } from \"@devup-ui/core\";\n<Box margin={a === b ? null : `${b}px`} />;\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
---
ExtractOutput {
styles: [
Dynamic(
ExtractDynamicStyle {
property: "margin",
level: 0,
identifier: "`${b}px`",
selector: None,
},
),
],
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={a === b ? \"\" : \"d0\"} style={{ \"--d1\": a === b ? undefined : `${b}px` }} />;\n",
}
Loading
Loading