diff --git a/crates/components/src/lib.rs b/crates/components/src/lib.rs index 6a7a0e42d..a34e9b21a 100644 --- a/crates/components/src/lib.rs +++ b/crates/components/src/lib.rs @@ -29,6 +29,7 @@ mod scroll_views; mod sidebar; mod slider; mod snackbar; +mod svg; mod switch; mod table; mod tabs; diff --git a/crates/components/src/svg.rs b/crates/components/src/svg.rs new file mode 100644 index 000000000..08d201a0e --- /dev/null +++ b/crates/components/src/svg.rs @@ -0,0 +1,42 @@ +/// Generate a Dioxus component rendering the specified SVG. +/// +/// Example: +/// +/// ```no_run +/// # use freya::prelude::*; +/// +/// import_svg!(Ferris, "../../../examples/ferris.svg", "100%", "100%"); +/// +/// fn app() -> Element { +/// rsx!(Ferris {}) +/// } +/// +/// fn another_app() -> Element { +/// rsx!(Ferris { +/// width: "150", +/// height: "40%", +/// }) +/// } +/// ``` +#[macro_export] +macro_rules! import_svg { + ($component_name:ident, $path:expr, $width: expr, $height: expr) => { + use dioxus::prelude::component; + // Generate a function with the name derived from the file name + #[allow(non_snake_case)] + #[component] + pub fn $component_name( + #[props(default = $width.to_string())] width: String, + #[props(default = $height.to_string())] height: String, + ) -> freya::prelude::Element { + use freya::prelude::*; + let svg_content = include_str!($path); + + rsx!(svg { + width, + height, + svg_content + }) + } + }; +} diff --git a/examples/import_svg.rs b/examples/import_svg.rs new file mode 100644 index 000000000..bbaad50d7 --- /dev/null +++ b/examples/import_svg.rs @@ -0,0 +1,7 @@ +use freya::prelude::*; + +import_svg!(Ferris, "./ferris.svg", "100%", "100%"); + +fn main() { + launch(|| rsx!(Ferris {})) +}