diff --git a/crates/tailwind-parse/src/lib.rs b/crates/tailwind-parse/src/lib.rs index 2618604..a7c17ae 100644 --- a/crates/tailwind-parse/src/lib.rs +++ b/crates/tailwind-parse/src/lib.rs @@ -70,7 +70,7 @@ mod plugin { Outline, Mix, Flex(Option), - Grid, + Grid(Option), Col, Grow, Shrink, @@ -98,6 +98,17 @@ mod plugin { Sr, } + #[derive(Copy, Clone, Eq, PartialEq, Debug)] + pub enum Grid { + Cols, + Rows, + FlowRow, + FlowCol, + FlowDense, + FlowRowDense, + FlowColDense, + } + #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum Flex { Row, diff --git a/src/parse/literal.rs b/src/parse/literal.rs index 20a8e40..dbc206b 100644 --- a/src/parse/literal.rs +++ b/src/parse/literal.rs @@ -25,6 +25,7 @@ pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result return plugin::text_transform(tt, lit.value, theme).ok_or(lit.full), TextDecoration(td) => return plugin::text_decoration(td, lit.value, theme).ok_or(lit.full), Flex(f) => return plugin::flex(f, lit.value, theme).ok_or(lit.full), + Grid(g) => return plugin::grid(g, lit.value, theme).ok_or(lit.full), // all other plugins Text => Required(plugin::text), @@ -44,7 +45,6 @@ pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result Required(plugin::to), Outline => Optional(plugin::outline), Mix => Required(plugin::mix), - Grid => Required(plugin::grid), Col => Required(plugin::col), Grow => Optional(plugin::grow), Shrink => Optional(plugin::shrink), diff --git a/src/parse/nom.rs b/src/parse/nom.rs index a0d3c14..80aa47f 100644 --- a/src/parse/nom.rs +++ b/src/parse/nom.rs @@ -104,6 +104,15 @@ pub enum SubjectValue<'a> { Css(&'a str), } +impl<'a> SubjectValue<'a> { + pub fn as_str(&self) -> &str { + match self { + SubjectValue::Value(s) => s, + SubjectValue::Css(s) => s, + } + } +} + impl<'a> Subject<'a> { pub fn parse(s: NomSpan<'a>) -> IResult, Self, nom::error::Error>> { let plugin = Plugin::parse; diff --git a/src/plugin.rs b/src/plugin.rs index 01848a3..91fed18 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -11,7 +11,7 @@ use swc_core::{ ecma::ast::{Expr, Ident, KeyValueProp, Lit, ObjectLit, Prop, PropName, PropOrSpread, Str}, }; use tailwind_parse::{ - Border, Display, Flex, Position, Rounded, TextDecoration, TextTransform, Visibility, + Border, Display, Flex, Grid, Position, Rounded, TextDecoration, TextTransform, Visibility, }; macro_rules! lookup_plugin { @@ -468,25 +468,28 @@ pub fn placeholder(rest: &str, theme: &TailwindTheme) -> Option { }) } -pub fn grid(rest: &str, theme: &TailwindTheme) -> Option { - let (cmd, rest) = rest.split_once('-')?; - match cmd { - "cols" => simple_lookup(&theme.grid_template_columns, rest, "gridTemplateColumns"), - "rows" => simple_lookup(&theme.grid_template_rows, rest, "gridTemplateRows"), - "flow" => { - let x = match rest { - "row" => "row", - "col" => "column", - "dense" => "dense", - "row-dense" => "row dense", - "col-dense" => "column dense", - _ => return None, - }; - - Some(to_lit(&[("gridAutoFlow", x)])) +pub fn grid( + g: Option, + rest: Option, + theme: &TailwindTheme, +) -> Option { + let rest = rest.as_ref().map(|s| s.as_str()); + let pair = match (g, rest) { + (Some(Grid::Cols), Some(rest)) => { + return simple_lookup(&theme.grid_template_columns, rest, "gridTemplateColumns") } - _ => None, - } + (Some(Grid::Rows), Some(rest)) => { + return simple_lookup(&theme.grid_template_rows, rest, "gridTemplateRows") + } + (Some(Grid::FlowRow), None) => [("gridAutoFlow", "row")], + (Some(Grid::FlowRowDense), None) => [("gridAutoFlow", "row dense")], + (Some(Grid::FlowCol), None) => [("gridAutoFlow", "col")], + (Some(Grid::FlowColDense), None) => [("gridAutoFlow", "col dense")], + (Some(Grid::FlowDense), None) => [("gridAutoFlow", "dense")], + (None, None) => [("display", "grid")], + _ => return None, + }; + Some(to_lit(&pair)) } pub fn col(rest: &str, theme: &TailwindTheme) -> Option {