use crate::assets::dropdown_styles::DROPDOWN_STYLES;
use crate::enums::dropdown_enums::{
DropdownColorScheme, DropdownConfig, DropdownHoverColor, DropdownItem, DropdownLabelsColor,
DropdownTitleColor,
};
use crate::DropdownButtonConfig;
use dioxus::prelude::*;
#[component]
pub fn DropdownMenu(config_dropdown: DropdownConfig) -> Element {
let mut is_open = use_signal(|| false);
let style_tag = rsx! {
style { "{DROPDOWN_STYLES}" }
};
let arrow_down_svg = rsx! {
svg {
xmlns: "http://www.w3.org/2000/svg",
view_box: "0 0 24 24",
fill: "none",
stroke: "currentColor",
stroke_width: "2",
stroke_linecap: "round",
stroke_linejoin: "round",
class: "feather feather-chevron-down",
path { d: "M6 9l6 6 6-6" }
}
};
let arrow_up_svg = rsx! {
svg {
xmlns: "http://www.w3.org/2000/svg",
view_box: "0 0 24 24",
fill: "none",
stroke: "currentColor",
stroke_width: "2",
stroke_linecap: "round",
stroke_linejoin: "round",
class: "feather feather-chevron-up",
path { d: "M18 15l-6-6-6 6" }
}
};
rsx! {
{style_tag},
if is_open() {
div {
class: "dropdown-overlay",
onclick: move |_| is_open.set(false),
}
}
div {
div { class: "dropdown",
button {
class: "dropdown-toggle",
style: "background-color: {config_dropdown.background_color.as_css_class()}; color: {config_dropdown.title_color.as_css_class()};",
onclick: move |_| is_open.set(!is_open()),
"{config_dropdown.title}"
match is_open() {
true => arrow_up_svg,
false => arrow_down_svg,
}
}
div {
match is_open() {
true => {
rsx! {
div {
class: "dropdown-content",
style: "background-color: {config_dropdown.background_color.as_css_class()}; color: {config_dropdown.labels_color.as_css_class()};",
for item in config_dropdown.label {
if let Some(url) = &item.url {
Link {
class: "link",
to: url.clone(),
style: "color: {config_dropdown.labels_color.as_css_class()}; --custom_color: {config_dropdown.hover_color.as_css_class()};",
"{item.label}"
}
} else {
span {
class: "link",
style: "color: {config_dropdown.labels_color.as_css_class()};",
"{item.label}"
}
}
}
}
}
}
false => {
rsx! {}
}
}
}
}
}
}
}
#[component]
pub fn DropdownMenuButton(config_dropdown: DropdownButtonConfig) -> Element {
let mut is_open = use_signal(|| false);
let style_tag = rsx! {
style { "{DROPDOWN_STYLES}" }
};
let arrow_down_svg = rsx! {
svg {
xmlns: "http://www.w3.org/2000/svg",
view_box: "0 0 24 24",
fill: "none",
stroke: "currentColor",
stroke_width: "2",
stroke_linecap: "round",
stroke_linejoin: "round",
class: "feather feather-chevron-down",
path { d: "M6 9l6 6 6-6" }
}
};
let arrow_up_svg = rsx! {
svg {
xmlns: "http://www.w3.org/2000/svg",
view_box: "0 0 24 24",
fill: "none",
stroke: "currentColor",
stroke_width: "2",
stroke_linecap: "round",
stroke_linejoin: "round",
class: "feather feather-chevron-up",
path { d: "M18 15l-6-6-6 6" }
}
};
rsx! {
{style_tag},
if is_open() {
div {
class: "dropdown-overlay",
onclick: move |_| is_open.set(false),
}
}
div {
div { class: "dropdown",
onclick: move |_| is_open.set(is_open()),
button {
class: "dropdown-toggle",
style: "background-color: {config_dropdown.background_color.as_css_class()}; color: {config_dropdown.title_color.as_css_class()};",
onclick: move |_| is_open.set(!is_open()),
"{config_dropdown.title}"
match is_open() {
true => arrow_up_svg,
false => arrow_down_svg,
}
}
div {
match is_open() {
true => {
rsx! {
div {
class: "dropdown-content",
style: "background-color: {config_dropdown.background_color.as_css_class()}; color: {config_dropdown.labels_color.as_css_class()};",
for (label , onclick_handler) in config_dropdown.labels.iter().zip(config_dropdown.onclick.iter()) {
button {
onclick: onclick_handler.clone(),
class: "button-config",
style: "color: {config_dropdown.labels_color.as_css_class()}; --custom_color: {config_dropdown.hover_color.as_css_class()};",
"{label}"
}
}
}
}
}
false => rsx! {},
}
}
}
}
}
}