🚧 (base): Añade BadgeKind para el tipo de Badge
This commit is contained in:
parent
997017bb75
commit
4ba66f5995
6 changed files with 230 additions and 43 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// Badge.
|
||||
pub(crate) mod badge;
|
||||
pub use badge::{Badge, BadgeBootsier};
|
||||
pub use badge::{Badge, BadgeKind};
|
||||
|
||||
// Button.
|
||||
mod button;
|
||||
|
|
|
|||
|
|
@ -1,45 +1,18 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
use crate::theme::*;
|
||||
|
||||
pub use pagetop::base::component::Badge;
|
||||
|
||||
const EXTRA_TEXT_BG: &str = "bootsier.badge.text_bg";
|
||||
|
||||
/// Extensión de Bootsier para [`Badge`].
|
||||
///
|
||||
/// Proporciona el método [`with_text_bg()`](Self::with_text_bg) para fijar el color de fondo del
|
||||
/// badge usando un color de texto con contraste suficiente garantizado.
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use pagetop::prelude::*;
|
||||
/// use pagetop_bootsier::theme::*;
|
||||
///
|
||||
/// let admin = bs::Badge::labeled(Lc::n("Admin")).with_text_bg(ThemeColor::Danger);
|
||||
/// ```
|
||||
pub trait BadgeBootsier {
|
||||
#[builder_fn]
|
||||
fn with_text_bg(self, color: ThemeColor) -> Self;
|
||||
}
|
||||
|
||||
impl BadgeBootsier for Badge {
|
||||
/// Establece el color de fondo usando un color de texto con contraste garantizado.
|
||||
///
|
||||
/// Igual a `with_prop(PropsOp::add_classes(class::TextColor::Bg(color)))`, pero sin el riesgo
|
||||
/// de acumular más de una clase `text-bg-{color}` si se llama varias veces.
|
||||
#[builder_fn]
|
||||
fn with_text_bg(mut self, color: ThemeColor) -> Self {
|
||||
self.alter_prop(PropsOp::set_extra(EXTRA_TEXT_BG, color));
|
||||
self
|
||||
}
|
||||
}
|
||||
pub use pagetop::base::component::{Badge, BadgeKind};
|
||||
|
||||
// **< Badge SETUP >********************************************************************************
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub(crate) fn setup(badge: &mut Badge) {
|
||||
let color = badge.props().extra_or(EXTRA_TEXT_BG, ThemeColor::Secondary);
|
||||
badge.alter_prop(PropsOp::replace_classes(
|
||||
"badge",
|
||||
util::join!("badge ", class::TextColor::Bg(color).to_class()),
|
||||
));
|
||||
let (old, new) = match badge.kind() {
|
||||
BadgeKind::Primary => ("badge-primary", "text-bg-primary"),
|
||||
BadgeKind::Secondary => ("badge-secondary", "text-bg-secondary"),
|
||||
BadgeKind::Success => ("badge-success", "text-bg-success"),
|
||||
BadgeKind::Info => ("badge-info", "text-bg-info"),
|
||||
BadgeKind::Warning => ("badge-warning", "text-bg-warning"),
|
||||
BadgeKind::Danger => ("badge-danger", "text-bg-danger"),
|
||||
};
|
||||
badge.alter_prop(PropsOp::replace_classes(old, new));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue