📚 Table of Contents
- What is Twill?
- Key Features
- Installation
- Quick Start
- Design Tokens
- Style Builder
- Components
- Backend Support
- Examples
- API Reference
- mdBook Docs
- Contributing
- License
✨ What is Twill?
Twill is a styling library for Rust that brings the best ideas from Tailwind CSS to native GUI development:
- Design Tokens — type-safe base values (colors, spacing, sizes)
- Utility-first — composable atomic styles
- Component Variants — pre-built component variants
But implements them through Rust types instead of CSS classes!
use ;
let button_style = new
.padding
.bg
.text_color
.rounded
.to_css;
// Result: "padding: 0.5rem 1rem; background-color: #3b82f6; color: #f8fafc; border-radius: 0.375rem"
🚀 Key Features
| Feature | Description |
|---|---|
| ✅ Type-safe | Impossible to specify invalid colors or sizes |
| ✅ IDE Autocomplete | All available options suggested by your IDE |
| ✅ Compile-time checks | Style errors caught at compile time |
| ✅ Composable | Styles can be combined and reused |
| ✅ Multi-backend | CSS, egui, iced, slint support |
| ✅ Zero runtime cost | All styles computed at compile time |
📦 Installation
Add to your Cargo.toml:
[]
= "0.1"
# Optional: enable backend support
= { = "0.1", = ["egui"] } # For egui
= { = "0.1", = ["iced"] } # For iced
= { = "0.1", = ["slint"] } # For slint
🎯 Quick Start
Basic Style Builder
use ;
// Create a card style
let card = new
.padding
.bg
.rounded
.shadow;
println!;
// "padding: 1.5rem; background-color: #ffffff; border-radius: 0.5rem; box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1)"
Flex Layouts
// Centered column
let centered = centered_col
.gap
.padding;
// Flex row
let row = flex_row
.gap
.justify;
Pre-built Components
use ;
// Button variants
let primary = primary.to_css;
let outline = outline.to_css;
let destructive = destructive.to_css;
// Button sizes
let small = primary.sm.to_css;
let large = primary.lg.to_css;
let full_width = primary.full_width.to_css;
🎨 Design Tokens
Colors
Full Tailwind CSS color palette with type-safe scale values:
use ;
// Color families
slate // #64748b
gray // #6b7280
red // #ef4444
orange // #f97316
blue // #3b82f6
green // #22c55e
purple // #a855f7
pink // #ec4899
// Special colors
white // #ffffff
black // #000000
transparent // transparent
// Scale values: S50, S100, S200, S300, S400, S500, S600, S700, S800, S900, S950
Spacing
use Spacing;
S0 // 0
S1 // 0.25rem (4px)
S2 // 0.5rem (8px)
S4 // 1rem (16px)
S6 // 1.5rem (24px)
S8 // 2rem (32px)
S12 // 3rem (48px)
S16 // 4rem (64px)
// ... up to S96
Border Radius
use BorderRadius;
None // 0
Sm // 0.125rem
Md // 0.375rem
Lg // 0.5rem
Xl // 0.75rem
Full // 9999px
Shadows
use Shadow;
Sm // Small shadow
Md // Medium shadow
Lg // Large shadow
Xl // Extra large shadow
None // No shadow
Motion (Tailwind-aligned)
use ;
let motion = new
.transition_property
.transition_duration
.transition_ease
.animate;
Semantic Variables (Optional, shadcn-style)
use ;
let semantic_bg = Background.to_css; // "var(--background)"
let css_vars = shadcn_neutral.to_css_variables; // :root + .dark blocks
let bg_dark = shadcn_neutral
.resolve
.unwrap; // Color::gray(Scale::S950)
🔧 Style Builder
The Style struct provides a fluent API for composing styles:
use ;
let style = new
// Layout
.display
.position
.z_index
// Flex/Grid
.flex
.gap
// Spacing
.padding
.margin
// Size
.width
.height
// Background
.bg
.opacity
// Border
.rounded
.border
// Shadow
.shadow
// Typography
.text_size
.font_weight
.text_color;
🧩 Components
Button
use ;
// Variants
primary // Solid blue background
secondary // Gray background
outline // Transparent with border
ghost // Transparent, no border
destructive // Red background
link // Link style
// Sizes
primary.sm // Small
primary // Medium (default)
primary.lg // Large
primary.icon // Square icon button
// Modifiers
primary.disabled // 50% opacity
primary.full_width // Width: 100%
🔌 Backend Support
Twill supports multiple GUI frameworks through feature flags:
| Backend | Feature | Status | Description |
|---|---|---|---|
| CSS | — | ✅ | Default, outputs CSS strings |
| egui | egui |
✅ | Native Rust GUI |
| iced | iced |
✅ | Cross-platform GUI |
| slint | slint |
✅ | Declarative UI toolkit |
Using with egui
use ;
📝 Examples
Run the examples:
# Basic demo (outputs CSS)
# egui demo
# iced demo
# slint demo
📖 API Reference
Full API documentation is available at docs.rs/twill.
Core Traits
| Trait | Description |
|---|---|
ToCss |
Convert style to CSS string |
Merge |
Combine two styles |
ComputeValue |
Compute final value |
Main Types
| Type | Description |
|---|---|
Style |
Main style builder |
Button |
Button component |
Color |
Color values |
Spacing |
Spacing scale |
Padding |
Padding utilities |
Margin |
Margin utilities |
BorderRadius |
Border radius values |
📚 mdBook Docs
Documentation is built with mdbook from docs/ sources.
- Source:
docs/ - Config:
docs/book.toml - Build output:
mdbook-build/
Commands:
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
MIT License — see LICENSE
Copyright (c) 2024 FerrisMind