Skip to content

Commit

Permalink
feat: add additional functions to grid plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 3, 2022
1 parent eaf3ef2 commit b81146c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
13 changes: 12 additions & 1 deletion crates/tailwind-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod plugin {
Outline,
Mix,
Flex(Option<Flex>),
Grid,
Grid(Option<Grid>),
Col,
Grow,
Shrink,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/parse/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result<Obje
TextTransform(tt) => 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),
Expand All @@ -44,7 +45,6 @@ pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result<Obje
To => 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),
Expand Down
9 changes: 9 additions & 0 deletions src/parse/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NomSpan<'a>, Self, nom::error::Error<NomSpan<'a>>> {
let plugin = Plugin::parse;
Expand Down
41 changes: 22 additions & 19 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -468,25 +468,28 @@ pub fn placeholder(rest: &str, theme: &TailwindTheme) -> Option<ObjectLit> {
})
}

pub fn grid(rest: &str, theme: &TailwindTheme) -> Option<ObjectLit> {
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<Grid>,
rest: Option<SubjectValue>,
theme: &TailwindTheme,
) -> Option<ObjectLit> {
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<ObjectLit> {
Expand Down

0 comments on commit b81146c

Please sign in to comment.