Skip to content

Commit

Permalink
feat: add the inset plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 3, 2022
1 parent eec214c commit 6d192b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions crates/tailwind-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod plugin {
Shadow,
Transition,
Placeholder,
Inset(Option<Inset>),
Delay,
Duration,
Divide,
Expand Down Expand Up @@ -98,6 +99,12 @@ mod plugin {
Sr,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Inset {
X,
Y,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Grid {
Cols,
Expand Down
4 changes: 4 additions & 0 deletions src/parse/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum PluginType {
}

pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result<ObjectLit, &'a str> {
use tailwind_parse::Inset;
use tailwind_parse::Max;
use tailwind_parse::Min;
use tailwind_parse::Plugin::*;
Expand Down Expand Up @@ -93,6 +94,9 @@ pub fn parse_literal<'a>(theme: &TailwindTheme, lit: Literal<'a>) -> Result<Obje
Max(Max::H) => Required(plugin::max_h),
Max(Max::W) => Required(plugin::max_w),
Fill => Required(plugin::fill),
Inset(None) => Required(plugin::inset),
Inset(Some(Inset::X)) => Required(plugin::inset_x),
Inset(Some(Inset::Y)) => Required(plugin::inset_y),
};

match (plugin, lit.value) {
Expand Down
23 changes: 20 additions & 3 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro_rules! lookup_plugin_opt {
};
}

macro_rules! merge_plugins {
macro_rules! merge_plugins_opt {
($def:ident, $closure_a:expr, $closure_b:expr) => {
pub fn $def(rest: Option<&str>, theme: &TailwindTheme) -> Option<ObjectLit> {
match ($closure_a(rest, theme), $closure_b(rest, theme)) {
Expand All @@ -53,6 +53,19 @@ macro_rules! merge_plugins {
};
}

macro_rules! merge_plugins {
($def:ident, $closure_a:expr, $closure_b:expr) => {
pub fn $def(rest: &str, theme: &TailwindTheme) -> Option<ObjectLit> {
match ($closure_a(rest, theme), $closure_b(rest, theme)) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(b), None) => Some(b),
(Some(a), Some(b)) => Some(merge_literals(a, b)),
}
}
};
}

fn simple_lookup(hashmap: &HashMap<&str, &str>, search: &str, output: &str) -> Option<ObjectLit> {
hashmap.get(search).map(|val| to_lit(&[(output, val)]))
}
Expand Down Expand Up @@ -358,8 +371,12 @@ lookup_plugin_opt!(border_t, border_width, "borderTopWidth");
lookup_plugin_opt!(border_l, border_width, "borderLeftWidth");
lookup_plugin_opt!(border_r, border_width, "borderRightWidth");
lookup_plugin_opt!(border_b, border_width, "borderBottomWidth");
merge_plugins!(border_x, border_l, border_r);
merge_plugins!(border_y, border_t, border_b);
merge_plugins_opt!(border_x, border_l, border_r);
merge_plugins_opt!(border_y, border_t, border_b);

merge_plugins!(inset_x, left, right);
merge_plugins!(inset_y, top, bottom);
merge_plugins!(inset, inset_x, inset_y);

pub fn from(rest: &str, theme: &TailwindTheme) -> Option<ObjectLit> {
theme.colors.get(rest).map(|c| {
Expand Down

0 comments on commit 6d192b5

Please sign in to comment.