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.
1. Shared const Styles
Section titled “1. Shared const Styles”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)}2. Composing Variants with .mix(...)
Section titled “2. Composing Variants with .mix(...)”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"),);use iced::widget::{button, text};use iss::typed::*;
let btn_base = IssStyle::new() .px(4) .py(2) .rounded_lg() .border() .border_color(IssColor::TRANSPARENT) .cursor_pointer();
let btn_danger = IssStyle::new() .bg(palette::ROSE_600) .hover(|s| s.bg(palette::ROSE_500)) .active(|s| s.bg(palette::ROSE_700));
let btn_ghost = IssStyle::new() .bg_transparent() .border_color(palette::ZINC_700) .hover(|s| s.bg(palette::ZINC_800));
let actions = row_tw( [ button(text("Delete").tw(IssStyle::new().text_sm().font_medium().text_color(palette::WHITE))) .on_press(Message::Delete) .tw(btn_base.clone().mix(btn_danger)), button(text("Cancel").tw(IssStyle::new().text_sm().font_medium().text_color(palette::ZINC_200))) .on_press(Message::Cancel) .tw(btn_base.mix(btn_ghost)), ], IssStyle::new().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"))}4. Reusable Styled Helper Functions
Section titled “4. Reusable Styled Helper Functions”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")))}