#create a html element which is const or thread local.
1 messages · Page 1 of 1 (latest)
whats the point the svg is const?
you can include svg as bytes as static, using include_bytes! macro
or you can create a shared state, and read svg once you need it and store it there, and access that shared state when you need it
using include bytes, the svg will be compiled into the source code, that may increase your wasm size
i have done it partially ref: https://github.com/sak96/chain_reaction/blob/e2cc40a664e3de9d76155bdf0a71a1c5d8c01857/src/bin/web/cells.rs#L41
but i have to convert that constant into html element using following:
let parsed_html = Html::from_html_unchecked(AttrValue::from(content));
it always logs: Unexpected value 2rem parsing height attribute.
which is clogging the console.
the issue is not with reuse.
instead i want to avoid logs.
width in rem was not working.
i tried lazy_static and thread_locals but
some issue with borrow checker with embedding the value in html! macro.
use yew::prelude::*;
const ONE: &str = r#"
<svg viewBox="0 0 100 100" width="2rem" height="2rem">
<circle class="cell" cx="45" cy="45" r="25" />
</svg>
"#;
const TWO: &str = r#"
<svg viewBox="0 0 100 100" width="2rem" height="2rem">
<circle class="cell" cx="25" cy="45" r="25" />
<circle class="cell" cx="75" cy="45" r="25" />
</svg>
"#;
#[function_component(App)]
pub fn app() -> Html {
let x = use_state(|| false);
let parsed_html = Html::from_html_unchecked(AttrValue::from(if *x { ONE } else { TWO }));
html! {
<button onclick={
let x = x.clone();
Callback::from(move |_| {
x.set(!*x)
})
}>
{parsed_html}
</button>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
example to tamper with.
check browser console log when you click on button,
i have no idea. so somehow you should be able to create a html node from string, and insert it to the dom tree
have you found any solution for this?
ok so it works
let svg_raw: &str = r#"
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Sorry, your browser does not support inline SVG.
</svg>
"#;
let svg = Html::from_html_unchecked(svg_raw.into());
html! {
<>
{svg}
</>
}
yeah it works. but the node needs to be created everything
if ther was a way to reuse it would have been nice
good question may be not.