Skip to content

Commit

Permalink
feat: import_svg macro (#790)
Browse files Browse the repository at this point in the history
* feat: `import_svg` macro

* feat: Allow to optionally specify width and height overrides in the generated component

* docs: Add docs for `import_svg`

* fix: Update `import_svg` doc example
  • Loading branch information
marc2332 authored Aug 3, 2024
1 parent 8177800 commit 741ef65
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod scroll_views;
mod sidebar;
mod slider;
mod snackbar;
mod svg;
mod switch;
mod table;
mod tabs;
Expand Down
42 changes: 42 additions & 0 deletions crates/components/src/svg.rs
Original file line number Diff line number Diff line change
@@ -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
})
}
};
}
7 changes: 7 additions & 0 deletions examples/import_svg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use freya::prelude::*;

import_svg!(Ferris, "./ferris.svg", "100%", "100%");

fn main() {
launch(|| rsx!(Ferris {}))
}

0 comments on commit 741ef65

Please sign in to comment.