dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Cannot load a stylesheet using with_custom_head in dioxus 0.6

Open photino opened this issue 1 year ago • 2 comments

Problem

In dioxus 0.5, we can use the method with_custom_head to load a stylesheet like:

<link rel="stylesheet" href="public/bulma.min.css" type="text/css">

But it does not work in dioxus 0.6. Any ideas on how to load a local stylesheet using with_custom_head (not using asset!)?

If we use asset!, it seems that it can not run with cargo run.

Steps To Reproduce

Steps to reproduce the behavior:

  • Call the with_custom_head method to load a stylesheet in a local dir.

Expected behavior

The with_custom_head method should work as dioxus 0.5.

Screenshots

Environment:

  • Dioxus version: 0.6
  • Rust version: 1.83
  • OS info: Windows
  • App platform: desktop

Questionnaire

I'm interested in fixing this myself but don't know where to start

photino avatar Dec 13 '24 09:12 photino

Only absolute paths and asset!() paths are supported by dioxus desktop 0.6 and only asset! paths will be bundled. You can resolve the path to an absolute path before formatting it in the head link:

use std::path::PathBuf;
use dioxus::prelude::*;

fn main() {
    let asset = "public/hello.css";
    let asset = PathBuf::from(asset).canonicalize().unwrap();
    let asset = asset.display();
    LaunchBuilder::new()
        .with_cfg(
            dioxus::desktop::Config::new()
                .with_custom_head(format!(r#"<link rel="stylesheet" href="{asset}" type="text/css">"#)) 
        )
        .launch(|| {
            rsx! { "hello world" }
        });
}

ealmloff avatar Jan 02 '25 20:01 ealmloff

Thanks! When I use an absolute file, it throws an error like:

image

photino avatar Jan 03 '25 02:01 photino

See also in https://github.com/DioxusLabs/dioxus/issues/1814#issuecomment-2282861303

It works in Windows 😄

Image

photino avatar Feb 23 '25 09:02 photino