diff --git a/Cargo.toml b/Cargo.toml index 52b3ee0..67b8269 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ license = "MIT" leptos = { version = "0.5.1", features = ["csr"] } web-sys = { version = "0.3.62", features = ["DomRect"] } wasm-bindgen = "0.2.85" -icondata = { version = "0.0.7", features = [ +icondata = { version = "0.1.0", features = [ "AiCloseOutlined", "AiCheckOutlined", "AiLeftOutlined", @@ -28,6 +28,7 @@ icondata = { version = "0.0.7", features = [ "AiPlusOutlined", "AiMinusOutlined", ] } +icondata_core = "0.0.2" [workspace] members = ["demo"] diff --git a/demo/Cargo.toml b/demo/Cargo.toml index 850d549..a34fb70 100644 --- a/demo/Cargo.toml +++ b/demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] leptos = { version = "0.5.1", features = ["csr"] } melt-ui = { path = "../" } -icondata = { version = "0.0.7", features = [ +icondata = { version = "0.1.0", features = [ "AiCloseOutlined", "AiCheckOutlined", ] } diff --git a/demo/src/app.rs b/demo/src/app.rs index 20a1c39..18ad8d2 100644 --- a/demo/src/app.rs +++ b/demo/src/app.rs @@ -32,6 +32,7 @@ pub fn App() -> impl IntoView { + diff --git a/demo/src/pages/components.rs b/demo/src/pages/components.rs index 3c19165..9c2efb8 100644 --- a/demo/src/pages/components.rs +++ b/demo/src/pages/components.rs @@ -96,6 +96,10 @@ fn gen_menu_data() -> Vec { value: "divider".into(), label: "Divider".into(), }, + MenuItemOption { + value: "icon".into(), + label: "Icon".into(), + }, ], }, MenuGroupOption { diff --git a/demo/src/pages/icon/mod.rs b/demo/src/pages/icon/mod.rs new file mode 100644 index 0000000..d129415 --- /dev/null +++ b/demo/src/pages/icon/mod.rs @@ -0,0 +1,34 @@ +use crate::components::{Demo, DemoCode}; +use leptos::*; +use melt_ui::*; +use prisms::highlight_str; + +#[component] +pub fn IconPage() -> impl IntoView { + view! { +
+

"Icon"

+ + + + + + + + + + "#, + "rust" + ) + > + + "" + + +
+ } +} diff --git a/demo/src/pages/mod.rs b/demo/src/pages/mod.rs index 6138c23..6017702 100644 --- a/demo/src/pages/mod.rs +++ b/demo/src/pages/mod.rs @@ -10,6 +10,7 @@ mod components; mod divider; mod grid; mod home; +mod icon; mod image; mod input; mod input_number; @@ -37,6 +38,7 @@ pub use components::*; pub use divider::*; pub use grid::*; pub use home::*; +pub use icon::*; pub use image::*; pub use input::*; pub use input_number::*; diff --git a/src/icon/mod.rs b/src/icon/mod.rs index 7309210..916b061 100644 --- a/src/icon/mod.rs +++ b/src/icon/mod.rs @@ -22,61 +22,66 @@ pub fn Icon( #[prop(into, optional)] style: Option>, ) -> impl IntoView { - let icon: IconData = icon.get().into(); + let icon = move || icondata_core::IconData::from(icon.get()); - let mut svg = svg::svg(); - if let Some(classes) = class { - svg = svg.classes(classes.get()); - } - // The style set by the user overrides the style set by the icon. - svg = match (style, icon.style) { - (Some(a), Some(b)) => svg.attr("style", format!("{b} {}", a.get())), - (Some(a), None) => svg.attr("style", a.get()), - (None, Some(b)) => svg.attr("style", b), - (None, None) => svg, + let svg = move || { + let icon = icon(); + let mut svg = svg::svg(); + if let Some(classes) = class.clone() { + svg = svg.classes(classes.get()); + } + let mut svg = match (style.clone(), icon.style) { + (Some(a), Some(b)) => svg.attr("style", format!("{b} {}", a.get())), + (Some(a), None) => svg.attr("style", a.get()), + (None, Some(b)) => svg.attr("style", b), + (None, None) => svg, + }; + + if let Some(x) = icon.x { + svg = svg.attr("x", x); + } + if let Some(y) = icon.y { + svg = svg.attr("x", y); + } + //// The style set by the user overrides the style set by the icon. + // We ignore the width and height attributes of the icon, even if the user hasn't specified any. + svg = svg.attr( + "width", + Attribute::String(match (width.clone(), icon.width) { + (Some(a), Some(_b)) => Oco::from(a.get()), + (Some(a), None) => Oco::from(a.get()), + (None, Some(_b)) => Oco::from("1em"), + (None, None) => Oco::from("1em"), + }), + ); + svg = svg.attr( + "height", + Attribute::String(match (height.clone(), icon.height) { + (Some(a), Some(_b)) => Oco::from(a.get()), + (Some(a), None) => Oco::from(a.get()), + (None, Some(_b)) => Oco::from("1em"), + (None, None) => Oco::from("1em"), + }), + ); + if let Some(view_box) = icon.view_box { + svg = svg.attr("viewBox", view_box); + } + if let Some(stroke_linecap) = icon.stroke_linecap { + svg = svg.attr("stroke-linecap", stroke_linecap); + } + if let Some(stroke_linejoin) = icon.stroke_linejoin { + svg = svg.attr("stroke-linejoin", stroke_linejoin); + } + if let Some(stroke_width) = icon.stroke_width { + svg = svg.attr("stroke-width", stroke_width); + } + if let Some(stroke) = icon.stroke { + svg = svg.attr("stroke", stroke); + } + svg = svg.attr("fill", icon.fill.unwrap_or("currentColor")); + svg = svg.attr("role", "graphics-symbol"); + svg = svg.inner_html(icon.data); + svg }; - if let Some(x) = icon.x { - svg = svg.attr("x", x); - } - if let Some(y) = icon.y { - svg = svg.attr("x", y); - } - // We ignore the width and height attributes of the icon, even if the user hasn't specified any. - svg = svg.attr( - "width", - Attribute::String(match (width, icon.width) { - (Some(a), Some(_b)) => Oco::from(a.get()), - (Some(a), None) => Oco::from(a.get()), - (None, Some(_b)) => Oco::from("1em"), - (None, None) => Oco::from("1em"), - }), - ); - svg = svg.attr( - "height", - Attribute::String(match (height, icon.height) { - (Some(a), Some(_b)) => Oco::from(a.get()), - (Some(a), None) => Oco::from(a.get()), - (None, Some(_b)) => Oco::from("1em"), - (None, None) => Oco::from("1em"), - }), - ); - if let Some(view_box) = icon.view_box { - svg = svg.attr("viewBox", view_box); - } - if let Some(stroke_linecap) = icon.stroke_linecap { - svg = svg.attr("stroke-linecap", stroke_linecap); - } - if let Some(stroke_linejoin) = icon.stroke_linejoin { - svg = svg.attr("stroke-linejoin", stroke_linejoin); - } - if let Some(stroke_width) = icon.stroke_width { - svg = svg.attr("stroke-width", stroke_width); - } - if let Some(stroke) = icon.stroke { - svg = svg.attr("stroke", stroke); - } - svg = svg.attr("fill", icon.fill.unwrap_or("currentColor")); - svg = svg.attr("role", "graphics-symbol"); - svg = svg.inner_html(icon.data); - IntoView::into_view(svg) + svg.into_view() }