Skip to content

Commit

Permalink
feat: add support raw function
Browse files Browse the repository at this point in the history
  • Loading branch information
kavinvalli committed Mar 7, 2023
1 parent 8ba31b9 commit b8a77dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/typewind/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type Typewind = ${types.join(' & ')} & {
// [arbitraryVariant: string]: (style: Property) => Property;
} & {
variant<T extends \`&\${string}\` | \`@\${string}\`>(variant: T, style: Property | string): Property;
};
raw(style: string): Property;
}
declare const tw: Typewind;
Expand Down
4 changes: 4 additions & 0 deletions packages/typewind/src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const createTw: any = () => {
return thisTw;
}

if (name === 'raw') {
return (style: string) => spreadModifier('', style);
}

if (name === 'variant') {
return (modifier: string, classes: any) =>
spreadModifier(`[${modifier}]:`, classes);
Expand Down
19 changes: 16 additions & 3 deletions packages/typewind/swc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,20 @@ fn analyse_expr(v: &mut TransformVisitor, wr: &mut Vec<String>, e: &Expr) {
.as_ref(),
);

if style == "variant" {
if style == "raw" {
let style = match &args
.get(0)
.expect("style not specified")
.expr
.as_lit()
.expect("Only literal values allowed")
{
Lit::Str(str) => str.value.to_string(),
_ => panic!("Only string literal values allowed"),
};

wr.push(style);
} else if style == "variant" {
let prefix = match &args
.get(0)
.expect("variant not specified")
Expand Down Expand Up @@ -214,7 +227,7 @@ test!(
|_| as_folder(TransformVisitor::new()),
boo,
// Input codes
r#"let style = tw.flex.$lg(tw.bg_black$['20']).md(tw.important(tw.works).text_["18px"].text_["red-200"])"#,
r#"let style = tw.flex.$lg(tw.bg_black$['20']).md(tw.important(tw.works).text_["18px"].text_["red-200"]).variant('&:nth-child(3)', tw.underline).raw("s-1/2")"#,
// Output codes after transformed with plugin
r#"let style = "flex @lg:bg-black/20 md:text-red-200 md:text-[18px] md:!works""#
r#"let style = "flex @lg:bg-black/20 md:text-red-200 md:text-[18px] md:!works [&:nth-child(3)]:underline s-1/2""#
);

1 comment on commit b8a77dd

@vercel
Copy link

@vercel vercel bot commented on b8a77dd Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.