Skip to content

Commit

Permalink
feat: make auto-rows plugin support arbitrary CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Feb 20, 2023
1 parent 1e20218 commit 41ed335
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions crates/tailwind-parse/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ macro_rules! array_map_plugin {
};
}

macro_rules! array_map_plugin_arbitrary {
($def:ident, $options:expr, $target:expr) => {
pub fn $def<'a>(value: &SubjectValue, _theme: &'a TailwindTheme) -> PluginResult<'a> {
match value {
SubjectValue::Value(Value(v)) => $options
.iter()
.find(|(x, _)| x == v)
.map(|(_, y)| to_lit(&[($target, y)]))
.ok_or_else(|| {
let sort = eddie::Levenshtein::new();
$options
.iter()
.map(|(x, _)| x)
.sorted_by_key(|val| sort.distance(v, val))
.copied()
.take(5)
.collect()
}),
SubjectValue::Css(Css(v)) => Ok(to_lit(&[($target, v)])),
}
}
};
}

macro_rules! lookup_plugin_arbitrary {
($def:ident, $map:tt, $target:expr) => {
pub fn $def<'a>(value: &SubjectValue, theme: &'a TailwindTheme) -> PluginResult<'a> {
Expand Down Expand Up @@ -162,6 +186,7 @@ macro_rules! merge_plugins_arbitrary_opt {
}

pub(crate) use array_map_plugin;
pub(crate) use array_map_plugin_arbitrary;
pub(crate) use array_plugin;
pub(crate) use lookup_plugin;
pub(crate) use lookup_plugin_arbitrary;
Expand Down
2 changes: 1 addition & 1 deletion crates/tailwind-parse/src/eval/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ lookup_plugin_arbitrary!(basis, flex_basis, "flexBasis");
lookup_plugin_arbitrary_opt!(grow, flex_grow, "flexGrow");
lookup_plugin_arbitrary_opt!(shrink, flex_shrink, "flexShrink");
lookup_plugin_arbitrary!(top, height, "top");
array_map_plugin!(
array_map_plugin_arbitrary!(
auto_rows,
[
("auto", "auto"),
Expand Down
2 changes: 1 addition & 1 deletion crates/tailwind-parse/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> Literal<'a> {
Content(c) => OptionalAbitraryBox(StdBox::new(move |v, t| plugin::content(c, v, t))),

Auto(Auto::Cols) => Required(plugin::auto_cols),
Auto(Auto::Rows) => Required(plugin::auto_rows),
Auto(Auto::Rows) => RequiredArbitrary(plugin::auto_rows),

// all other plugins
Text => RequiredArbitrary(plugin::text),
Expand Down

0 comments on commit 41ed335

Please sign in to comment.