Skip to content

xinuxZ/css-in-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

91 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CSS-in-Rust ๐ŸŽจ

ไธ€ไธช้ซ˜ๆ€ง่ƒฝใ€็ฑปๅž‹ๅฎ‰ๅ…จ็š„Rust CSS-in-JS่งฃๅ†ณๆ–นๆกˆ๏ผŒไธ“ไธบ็ŽฐไปฃWebๅผ€ๅ‘่ฎพ่ฎกใ€‚

โœจ ็‰นๆ€ง

๐Ÿš€ ๆ ธๅฟƒๅŠŸ่ƒฝ

  • ็ฑปๅž‹ๅฎ‰ๅ…จ: ๅˆฉ็”จRust็š„็ฑปๅž‹็ณป็ปŸ็กฎไฟCSS็š„ๆญฃ็กฎๆ€ง
  • ้ซ˜ๆ€ง่ƒฝ็ผ“ๅญ˜: ๆ™บ่ƒฝLRU็ผ“ๅญ˜ๆœบๅˆถ๏ผŒๆ”ฏๆŒๅคš็ง็ผ“ๅญ˜็ญ–็•ฅ
  • ๆต่งˆๅ™จๅ…ผๅฎนๆ€ง: ่‡ชๅŠจๆฃ€ๆต‹ๅ’Œ็”ŸๆˆCSSๅ›ž้€€ๆ–นๆกˆ
  • ๅ†…ๅญ˜็ฎก็†: ๅ…ˆ่ฟ›็š„ๅ†…ๅญ˜ๅŽ‹ๅŠ›็›‘ๆŽงๅ’Œ่‡ชๅŠจๆธ…็†
  • ๆ€ง่ƒฝ็›‘ๆŽง: ่ฏฆ็ป†็š„ๆ€ง่ƒฝๆŒ‡ๆ ‡ๅ’Œ็ผ“ๅญ˜ๅ‘ฝไธญ็އ็ปŸ่ฎก

๐ŸŒ ๆต่งˆๅ™จๆ”ฏๆŒ

  • ๅ…ผๅฎนๆ€งๆฃ€ๆต‹: ๆ”ฏๆŒChromeใ€Firefoxใ€Safariใ€Edge็ญ‰ไธปๆตๆต่งˆๅ™จ
  • ่‡ชๅŠจๅ›ž้€€: ไธบไธๆ”ฏๆŒ็š„CSS็‰นๆ€ง่‡ชๅŠจ็”Ÿๆˆๅ›ž้€€ไปฃ็ 
  • ๅŽ‚ๅ•†ๅ‰็ผ€: ๆ™บ่ƒฝๆทปๅŠ ๅ’Œไผ˜ๅŒ–ๅŽ‚ๅ•†ๅ‰็ผ€
  • ็‰นๆ€งๆฃ€ๆต‹: ๆ”ฏๆŒFlexboxใ€Gridใ€Container Queries็ญ‰็ŽฐไปฃCSS็‰นๆ€ง

โšก ๆ€ง่ƒฝไผ˜ๅŒ–

  • ๆ™บ่ƒฝ็ผ“ๅญ˜: ๆ”ฏๆŒLRUใ€LFUใ€TTLใ€่‡ช้€‚ๅบ”็ญ‰ๅคš็ง็ผ“ๅญ˜็ญ–็•ฅ
  • CSSๅŽ‹็ผฉ: ่‡ชๅŠจ็งป้™ค็ฉบ็™ฝๅ’Œๆณจ้‡Š
  • ๅ‰็ผ€ไผ˜ๅŒ–: ๆ นๆฎ็›ฎๆ ‡ๆต่งˆๅ™จ็งป้™คไธๅฟ…่ฆ็š„ๅŽ‚ๅ•†ๅ‰็ผ€
  • ๅ†…ๅญ˜็›‘ๆŽง: ๅฎžๆ—ถ็›‘ๆŽงๅ†…ๅญ˜ไฝฟ็”จๆƒ…ๅ†ต๏ผŒ่‡ชๅŠจๆ‰ง่กŒๆธ…็†็ญ–็•ฅ

๐Ÿ“ฆ ้กน็›ฎ็ป“ๆž„

css-in-rust/
โ”œโ”€โ”€ crates/
โ”‚   โ”œโ”€โ”€ css-in-rust-core/          # ๆ ธๅฟƒๅŠŸ่ƒฝๅบ“
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ cache/              # ็ผ“ๅญ˜็ณป็ปŸ
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ entity.rs       # ็ผ“ๅญ˜ๅฎžไฝ“ๅ’ŒLRUๅฎž็Žฐ
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ manager.rs      # ็ผ“ๅญ˜็ฎก็†ๅ™จ
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ strategy.rs     # ้ซ˜็บง็ผ“ๅญ˜็ญ–็•ฅ
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ mod.rs
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ integration.rs      # ้›†ๆˆ็ณป็ปŸ
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ lib.rs
โ”‚   โ”‚   โ””โ”€โ”€ Cargo.toml
โ”‚   โ””โ”€โ”€ css-in-rust-utils/          # ๅทฅๅ…ทๅบ“
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ browser.rs          # ๆต่งˆๅ™จๅ…ผๅฎนๆ€งๆฃ€ๆต‹
โ”‚       โ”‚   โ””โ”€โ”€ lib.rs
โ”‚       โ””โ”€โ”€ Cargo.toml
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ integrated_system_demo.rs   # ๅฎŒๆ•ดๅŠŸ่ƒฝๆผ”็คบ
โ”œโ”€โ”€ Cargo.toml                      # ๅทฅไฝœๅŒบ้…็ฝฎ
โ””โ”€โ”€ README.md

๐Ÿš€ ๅฟซ้€Ÿๅผ€ๅง‹

ๅฎ‰่ฃ…

ๅฐ†ไปฅไธ‹ๅ†…ๅฎนๆทปๅŠ ๅˆฐไฝ ็š„ Cargo.toml:

[dependencies]
css-in-rust-core = { path = "crates/css-in-rust-core" }
css-in-rust-utils = { path = "crates/css-in-rust-utils" }

Basic Usage

use css_in_rust::styled;
use dioxus::prelude::*;

#[styled]
struct ButtonStyles {
    base: &'static str = "
        padding: 8px 16px;
        border-radius: 4px;
        border: none;
        cursor: pointer;
        font-weight: 500;
        transition: all 0.2s ease;
    ",
    variants: ButtonVariants {
        primary: "background: #007bff; color: white;",
        secondary: "background: #6c757d; color: white;",
    },
}

#[derive(Props)]
struct ButtonProps {
    variant: ButtonVariant,
    children: Element,
}

fn Button(cx: Scope<ButtonProps>) -> Element {
    let styles = use_style_register(cx, ButtonStyles::new());
    
    render! {
        button {
            class: "{styles.base} {styles.variant(props.variant)}",
            {&props.children}
        }
    }
}

fn App(cx: Scope) -> Element {
    render! {
        div {
            Button {
                variant: ButtonVariant::Primary,
                "Click me!"
            }
        }
    }
}

Theme Integration

use css_in_rust_theme::{ThemeProvider, use_theme};

fn App(cx: Scope) -> Element {
    render! {
        ThemeProvider {
            theme: create_theme(),
            Router {}
        }
    }
}

fn ThemedComponent(cx: Scope) -> Element {
    let theme = use_theme(cx);
    let styles = use_style_register(cx, |
        color: theme.colors.primary,
        padding: theme.spacing.md,
        border_radius: theme.radii.sm,
    |);
    
    render! {
        div { class: "{styles}", "Themed content" }
    }
}

Architecture

CSS-in-Rust is built with a modular architecture:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Application Layer                        โ”‚
โ”‚                  (Dioxus Components)                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                  Integration Layer                          โ”‚
โ”‚              (Dioxus Hooks & Providers)                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                    API Layer                               โ”‚
โ”‚               (CSS-in-Rust Macros)                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                   Core Engine                              โ”‚
โ”‚            (Style Processing & Caching)                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                 Foundation Layer                           โ”‚
โ”‚              (Lightning CSS + Utilities)                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Crates

  • css-in-rust-core: Core style processing engine
  • css-in-rust-macro: Procedural macros for compile-time CSS generation
  • css-in-rust-theme: Theme system and design tokens
  • css-in-rust-dioxus: Dioxus integration with hooks and providers
  • css-in-rust-cli: Development tools and utilities

Performance

CSS-in-Rust is designed for maximum performance:

  • Compile-time CSS generation: Zero runtime CSS parsing
  • Lightning CSS optimization: Automatic vendor prefixing and minification
  • Efficient caching: Multi-level caching strategy
  • Small bundle size: <50KB core library
  • SSR optimized: Critical CSS extraction and hydration

Comparison

Feature CSS-in-Rust styled-components emotion
Runtime Performance โญโญโญโญโญ โญโญโญ โญโญโญโญ
Type Safety โญโญโญโญโญ โญโญ โญโญ
Bundle Size โญโญโญโญโญ โญโญโญ โญโญโญโญ
Developer Experience โญโญโญโญ โญโญโญโญโญ โญโญโญโญ
SSR Support โญโญโญโญโญ โญโญโญโญ โญโญโญโญ

Examples

Explore our examples directory for comprehensive usage examples:

  • Basic Styling: Simple component styling
  • Theme System: Advanced theming with design tokens
  • Responsive Design: Mobile-first responsive components
  • SSR Application: Server-side rendering setup
  • Performance Demo: Benchmarking and optimization

Documentation

Development Status

๐Ÿšง This project is currently in active development (Sprint 0/20)

Current Status

  • Project setup and architecture design
  • Rust workspace initialization
  • Core type system implementation
  • Lightning CSS integration
  • Dioxus hooks development
  • Theme system implementation
  • SSR support
  • Development tools
  • Documentation and examples
  • Performance optimization
  • Testing and release

See our detailed iteration plan for the complete roadmap.

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

# Clone the repository
git clone https://github.com/css-in-rust/css-in-rust.git
cd css-in-rust

# Install Rust toolchain
rustup component add rustfmt clippy
rustup target add wasm32-unknown-unknown

# Install development tools
cargo install cargo-watch wasm-pack trunk

# Run tests
cargo test

# Run examples
cd examples/basic
trunk serve

License

This project is licensed under either of

at your option.

Acknowledgments


Built with โค๏ธ for the Rust and Dioxus community

About

CSS in Rust

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages