-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ mod scroll_views; | |
mod sidebar; | ||
mod slider; | ||
mod snackbar; | ||
mod svg; | ||
mod switch; | ||
mod table; | ||
mod tabs; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {})) | ||
} |