Skip to content

Commit

Permalink
Global Override OnceLock (#21)
Browse files Browse the repository at this point in the history
* attempt global override with compile time env value

* set_tw_options OnceLock

* Remove unused .cargo/config.toml

* fix doc test

* fix clippy error, use copied

* improved docs

* update readme

* small readme update

* remove prefix from example

* example fixes

* fix broken doc link

* remove inline annotation
  • Loading branch information
nicoburniske authored Apr 12, 2024
1 parent 2853fb5 commit 8f1968d
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 132 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ assert_eq!(
)
```

### Custom Tailwind Prefix/Separator

Use [`merge::set_merge_options`] to set global options for [`tw_merge!`] and variant macros.

This can only be set once. Subsequent calls will be ignored.

```rust
use tailwind_fuse::{*, merge::*};

const OPTIONS: MergeOptions = MergeOptions {
prefix: "tw-",
separator: ":",
};

// Before setting options, the default (no prefix) is used
assert_eq!(
"tw-bg-black tw-bg-white",
tw_merge!("tw-bg-black", "tw-bg-white"),
);

set_merge_options(OPTIONS);

assert_eq!(
"tw-bg-white",
tw_merge!("tw-bg-black", "tw-bg-white"),
);

```


## Usage: Variants

Expand Down
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
2 changes: 1 addition & 1 deletion example/start-axum/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn HomePage() -> impl IntoView {
let on_click = move |_| set_count.update(|count| *count += 1);

view! {
<div class="flex items-center gap-4 p-10">
<div class="flex items-center gap-4 p-10 flex-row">
<Button on:click=on_click size=BtnSize::Lg>
"Click Me: "
{count}
Expand Down
22 changes: 10 additions & 12 deletions example/start-axum/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ use tailwind_fuse::*;

#[component]
pub fn Button(
#[prop(into, optional)] size: Option<BtnSize>,
#[prop(into, optional)] variant: Option<BtnVariant>,
#[prop(into, optional)] class: Option<String>,
/// Arbitrary additional attributes.
#[prop(attrs)]
attributes: Vec<(&'static str, Attribute)>,
#[prop(into, optional)] variant: MaybeSignal<BtnVariant>,
#[prop(into, optional)] size: MaybeSignal<BtnSize>,
#[prop(into, optional)] class: MaybeSignal<String>,
#[prop(attrs)] attributes: Vec<(&'static str, Attribute)>,
children: Children,
) -> impl IntoView {
let btn = Btn {
size: size.unwrap_or_default(),
variant: variant.unwrap_or_default(),
};

let class = class.map_or(btn.to_class(), |c| btn.with_class(c));
let class = create_memo(move |_| {
let variant = variant.get();
let size = size.get();
let button = Btn { variant, size };
button.with_class(class.get())
});

view! {
<button {..attributes} class=class>
Expand Down
3 changes: 1 addition & 2 deletions example/start-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod fileserv;
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use crate::app::*;
console_error_panic_hook::set_once();
leptos::mount_to_body(App);
leptos::mount_to_body(crate::app::App);
}
2 changes: 1 addition & 1 deletion fuse/src/core/join.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Joins the given classes into a single string.
///
/// Items can be of type &[`str`] or [`String`].
/// Items can be anything that implements [`crate::AsTailwindClass`].
///
/// If you want to handle conflicts use [`crate::tw_merge!`].
///
Expand Down
50 changes: 50 additions & 0 deletions fuse/src/core/merge/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::sync::OnceLock;

/// Configuration for merging Tailwind classes.
/// If you want to set global options use [`set_merge_options`].
#[derive(Clone, Copy, Debug)]
pub struct MergeOptions {
/// Custom prefix for modifiers in Tailwind classes
///
/// Default is empty string
///
/// <https://tailwindcss.com/docs/configuration#prefix>
pub prefix: &'static str,
/// Custom separator for modifiers in Tailwind classes
///
/// Default is `:`
///
/// <https://tailwindcss.com/docs/configuration#separator>
pub separator: &'static str,
}

impl Default for MergeOptions {
fn default() -> Self {
MERGE_OVERRIDE
.get()
.copied()
.unwrap_or(DEFAULT_MERGE_OPTIONS)
}
}

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

impl From<MergeOptions> for crate::ast::AstParseOptions<'static> {
fn from(options: MergeOptions) -> Self {
crate::ast::AstParseOptions {
prefix: options.prefix,
separator: options.separator,
}
}
}

pub(crate) static MERGE_OVERRIDE: OnceLock<MergeOptions> = OnceLock::new();

/// Set global options for merging Tailwind classes.
/// Useful for getting all the macros to work with custom options.
pub fn set_merge_options(options: MergeOptions) {
let _ = MERGE_OVERRIDE.set(options);
}
2 changes: 1 addition & 1 deletion fuse/src/core/merge/merge_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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(
pub fn tw_merge_override(
class: &[&str],
options: MergeOptions,
collision_id_fn: impl CollisionIdFn,
Expand Down
Loading

0 comments on commit 8f1968d

Please sign in to comment.