Skip to content

Reusable Styles & .mix()

As an Iced app grows, repeating long utility strings across every view() function gets noisy. ISS lets you extract shared const styles, compose variants with .mix(...), and wrap recurring patterns in small Rust helper functions.

Every tw!("...") call evaluates to a Copy TwStyle value that you can store in const items and reuse across modules.

use iced::widget::{button, container, text};
use iss::{col_tw, tw, TwExt, TwStyle};
const CARD: TwStyle = tw!("p-5 bg-zinc-900 border border-zinc-800 rounded-2xl shadow-lg");
const HEADING: TwStyle = tw!("text-base font-semibold text-white");
const MUTED: TwStyle = tw!("text-xs text-zinc-400");
const BTN_PRIMARY: TwStyle = tw!(
"px-4 py-2 bg-violet-600 hover:bg-violet-500 active:bg-violet-700 rounded-lg cursor-pointer"
);
fn panel<'a>(on_save: Message) -> iced::Element<'a, Message> {
container(col_tw(
[
text("API Keys").tw(HEADING),
text("Rotate credentials without restarting services.").tw(MUTED),
button(text("Generate key").tw(tw!("text-sm font-medium text-white")))
.on_press(on_save)
.tw(BTN_PRIMARY),
],
tw!("gap-3"),
))
.tw(CARD)
}

Both TwStyle and IssStyle implement .mix(other) (also aliased as .merge(other)). Properties in other override matching properties in the base style using last-write-wins precedence while preserving everything else (padding, radius, cursor, borders).

use iced::widget::{button, text};
use iss::{row_tw, tw, TwExt, TwStyle};
const BTN_BASE: TwStyle = tw!("px-4 py-2 rounded-lg border border-transparent cursor-pointer");
const BTN_PRIMARY: TwStyle = tw!("bg-violet-600 hover:bg-violet-500 active:bg-violet-700");
const BTN_DANGER: TwStyle = tw!("bg-rose-600 hover:bg-rose-500 active:bg-rose-700");
const BTN_GHOST: TwStyle = tw!("bg-transparent border-zinc-700 hover:bg-zinc-800");
let actions = row_tw(
[
button(text("Save").tw(tw!("text-sm font-medium text-white")))
.on_press(Message::Save)
.tw(BTN_BASE.mix(BTN_PRIMARY)),
button(text("Delete").tw(tw!("text-sm font-medium text-white")))
.on_press(Message::Delete)
.tw(BTN_BASE.mix(BTN_DANGER)),
button(text("Cancel").tw(tw!("text-sm font-medium text-zinc-200")))
.on_press(Message::Cancel)
.tw(BTN_BASE.mix(BTN_GHOST)),
],
tw!("gap-2"),
);

You can also chain multiple .mix(...) calls conditionally:

let mut style = BTN_BASE.mix(BTN_PRIMARY);
if is_compact {
style = style.mix(tw!("px-2.5 py-1 rounded-md"));
}
if is_full_width {
style = style.mix(tw!("w-full"));
}

3. Mixing tw!() with Dynamic Runtime Values

Section titled “3. Mixing tw!() with Dynamic Runtime Values”

Static tw!() styles compose directly with dynamic IssStyle values computed from application state (such as a live progress bar percentage or a user-picked color).

use iced::widget::{container, Space};
use iss::typed::*;
fn progress_bar<'a, M: 'a>(progress: f32, accent: IssColor) -> iced::Element<'a, M> {
let pct = (progress.clamp(0.0, 1.0)) * 100.0;
let fill_style = tw!("h-full rounded-full")
.mix(IssStyle::new().w_len(IssLength::Percent(pct)).bg(accent));
container(container(Space::new(0, 0)).tw(fill_style))
.tw(tw!("w-full h-2 rounded-full bg-zinc-800 overflow-hidden"))
}

Wrap recurring UI primitives into plain Rust functions instead of duplicating widget trees:

use iced::widget::{button, text};
use iced::Element;
use iss::{tw, TwExt, TwStyle};
const BTN_BASE: TwStyle = tw!(
"px-4 py-2 rounded-lg bg-violet-600 hover:bg-violet-500 active:bg-violet-700 cursor-pointer"
);
pub fn primary_button<'a, M: Clone + 'a>(label: &'a str, on_press: M) -> Element<'a, M> {
button(text(label).tw(tw!("text-sm font-semibold text-white")))
.on_press(on_press)
.tw(BTN_BASE)
}
pub fn secondary_button<'a, M: Clone + 'a>(label: &'a str, on_press: M) -> Element<'a, M> {
button(text(label).tw(tw!("text-sm font-medium text-zinc-200")))
.on_press(on_press)
.tw(BTN_BASE.mix(tw!("bg-zinc-800 hover:bg-zinc-700 active:bg-zinc-900 border border-zinc-700")))
}