Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Tailwind Config Override: Compile Time Env Value #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/start-axum/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
TW_PREFIX = "tw-"
68 changes: 23 additions & 45 deletions example/start-axum/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/start-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ leptos_router = { version = "0.6", features = ["nightly"] }
tokio = { version = "1", features = ["rt-multi-thread"], optional = true }
tower = { version = "0.4", optional = true }
tower-http = { version = "0.5", features = ["fs"], optional = true }
wasm-bindgen = "=0.2.89"
wasm-bindgen = "=0.2.92"
thiserror = "1"
tracing = { version = "0.1", optional = true }
http = "1"
tailwind_fuse = { path = "../../public", features = ["variant"]}
tailwind_fuse = { path = "../../fuse", features = ["variant", "debug"]}

[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
Expand Down
9 changes: 7 additions & 2 deletions example/start-axum/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::{
button::{BtnSize, BtnVariant, Button},
error_template::{AppError, ErrorTemplate},
};
use leptos::*;
use leptos::{logging::log, *};
use leptos_meta::*;
use leptos_router::*;
use tailwind_fuse::{merge::MergeOptions, *};

#[component]
pub fn App() -> impl IntoView {
Expand Down Expand Up @@ -39,8 +40,12 @@ fn HomePage() -> impl IntoView {
let (count, set_count) = create_signal(0);
let on_click = move |_| set_count.update(|count| *count += 1);

let options = MergeOptions::default();
log!("MergeOptions {options:?}");

view! {
<div class="flex items-center gap-4 p-10">
// flex-row should be removed.
<div class=tw_merge!("tw-flex tw-items-center tw-gap-4 tw-p-10 tw-flex-row", "tw-flex-col")>
<Button on:click=on_click size=BtnSize::Lg>
"Click Me: "
{count}
Expand Down
1 change: 1 addition & 0 deletions example/start-axum/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
prefix: "tw-",
content: {
relative: true,
files: ["*.html", "./src/**/*.rs"],
Expand Down
4 changes: 2 additions & 2 deletions fuse/benches/merge.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use divan::Bencher;
use tailwind_fuse::merge::{tw_merge, tw_merge_slice};
use tailwind_fuse::merge::{tw_merge, tw_merge_slice_options};

fn main() {
divan::main();
Expand Down Expand Up @@ -28,7 +28,7 @@ fn tailwind_merge(bencher: Bencher, len: usize) {
fn tailwind_merge_slice(bencher: Bencher, len: usize) {
bencher
.with_inputs(|| generate_random_classes(len))
.bench_values(|class| tw_merge_slice(&class));
.bench_values(|class| tw_merge_slice_options(&class, Default::default()));
}

// create a vec with the a length of len and fill it with random data
Expand Down
3 changes: 2 additions & 1 deletion fuse/src/core/merge/merge_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::core::merge::get_collisions::get_collisions;

/// Merges all the Tailwind classes, resolving conflicts.
/// Can supply custom options, collision_id_fn and collisions_fn.
pub fn tw_merge_with_override(
#[inline]
pub fn tw_merge_override(
class: &[&str],
options: MergeOptions,
collision_id_fn: impl CollisionIdFn,
Expand Down
42 changes: 17 additions & 25 deletions fuse/src/core/merge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) mod get_collisions;
pub(crate) mod merge_impl;
mod validators;

pub use merge_impl::tw_merge_with_override;
pub use merge_impl::tw_merge_override;

/// Merges all the Tailwind classes, resolving conflicts.
///
Expand All @@ -21,8 +21,9 @@ macro_rules! tw_merge {
}

/// Merges all the Tailwind classes, resolving conflicts.
#[inline]
pub fn tw_merge(class: impl AsRef<str>) -> String {
tw_merge_slice(&[class.as_ref()])
tw_merge_slice_options(&[class.as_ref()], Default::default())
}

/// Merges all the Tailwind classes, resolving conflicts, with the provided options.
Expand All @@ -40,33 +41,27 @@ pub fn tw_merge(class: impl AsRef<str>) -> String {
/// tw_merge_with_options(class, OPTIONS)
/// }
/// ```
pub fn tw_merge_with_options(class: impl AsRef<str>, options: MergeOptions) -> String {
merge_impl::tw_merge_with_override(
#[inline]
pub fn tw_merge_options(class: impl AsRef<str>, options: MergeOptions) -> String {
merge_impl::tw_merge_override(
&[class.as_ref()],
options,
noop_collision_id_fn,
noop_collision_fn,
|_: &[&str], _: Option<&str>| None,
|_: &str| None,
)
}

/// Merges all the Tailwind classes, resolving conflicts.
pub fn tw_merge_slice(class: &[&str]) -> String {
merge_impl::tw_merge_with_override(
#[inline]
pub fn tw_merge_slice_options(class: &[&str], options: MergeOptions) -> String {
merge_impl::tw_merge_override(
class,
Default::default(),
noop_collision_id_fn,
noop_collision_fn,
options,
|_: &[&str], _: Option<&str>| None,
|_: &str| None,
)
}

fn noop_collision_id_fn(_: &[&str], _: Option<&str>) -> Option<&'static str> {
None
}

fn noop_collision_fn(_: &str) -> Option<Vec<&'static str>> {
None
}

/// Configuration for merging Tailwind classes.
#[derive(Clone, Copy, Debug)]
pub struct MergeOptions {
Expand All @@ -86,15 +81,12 @@ pub struct MergeOptions {

impl Default for MergeOptions {
fn default() -> Self {
DEFAULT_OPTIONS
let prefix = option_env!("TW_PREFIX").unwrap_or("");
let separator = option_env!("TW_SEPARATOR").unwrap_or(":");
MergeOptions { prefix, separator }
}
}

const DEFAULT_OPTIONS: MergeOptions = MergeOptions {
prefix: "",
separator: ":",
};

impl From<MergeOptions> for crate::ast::AstParseOptions<'static> {
fn from(options: MergeOptions) -> Self {
crate::ast::AstParseOptions {
Expand Down
2 changes: 1 addition & 1 deletion fuse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod variant {

impl TailwindFuse for TailwindMerge {
fn fuse_classes(&self, class: &[&str]) -> String {
crate::merge::tw_merge_slice(class)
crate::merge::tw_merge_slice_options(class, Default::default())
}
}

Expand Down
8 changes: 4 additions & 4 deletions fuse/tests/override.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use tailwind_fuse::merge::{tw_merge_with_options, tw_merge_with_override, MergeOptions};
use tailwind_fuse::merge::{tw_merge_options, tw_merge_override, MergeOptions};

#[test]
fn test_collisions() {
pub fn tw_merge(class: &str) -> String {
tw_merge_with_override(
tw_merge_override(
&[class],
Default::default(),
collision_id_fn,
Expand Down Expand Up @@ -47,11 +47,11 @@ fn test_override_config() {
};

let class = "hover|lg|tw-bg-blue-100 hover|lg|tw-bg-red-500";
let result = tw_merge_with_options(class, config);
let result = tw_merge_options(class, config);
assert_eq!("hover|lg|tw-bg-red-500", result);

let class = "tw-bg-blue-100 bg-red-500";
let result = tw_merge_with_options(class, config);
let result = tw_merge_options(class, config);
assert_eq!(
class, result,
"No conflict because non-prefix is not considered tailwind class"
Expand Down
Loading