logoalt Hacker News

nicoburns • today at 3:29 AM • 0 replies • view on HN

With Dioxus Native:

    use dioxus::native::{Config, LogicalSize, WindowAttributes};
    use dioxus::prelude::*;

    fn main() {
        let window = WindowAttributes::default().with_surface_size(LogicalSize::new(500.0, 500.0));
        let config = Config::new().with_window_attributes(window);
        dioxus::native::launch_cfg(app, vec![], vec![Box::new(config)]);
    }

    fn app() -> Element {
        rsx! {
            Stylesheet { href: asset!("/assets/tailwind.css") }
            HelloWorld { text: "World" }
        }
    }

    #[component]
    fn HelloWorld(text: String) -> Element {
        rsx! {
            div { class: "flex flex-col gap-3 bg-[#505050] size-[500px] justify-center items-center shadow-lg border border-[#0000ff] text-xl text-white",
                "Hello, {text}!"
                div { class: "flex gap-2",
                    div { class: "size-8 bg-[red]" }
                    div { class: "size-8 bg-[lime]" }
                    div { class: "size-8 bg-[blue]" }
                    div { class: "size-8 bg-[yellow]" }
                    div { class: "size-8 bg-black" }
                    div { class: "size-8 bg-white" }
                }
            }
        }
    }
You can also use standard CSS if you prefer it over tailwind. And if you wanted to deduplicate, the 6 divs can be written in a loop like so:

    for color in ["red", "lime", "blue", "yellow", "black", "white"] {
        div { width: "32px", height: "32px", background_color: color }
    }