From 5d6852382aa8e40c8da3d8cae637b6cb5f18b172 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 22 Nov 2026 23:08:28 +0100 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20A=C3=B1ade=20iconos=20a=20boton?= =?UTF-8?q?es=20y=20men=C3=BAs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nuevo componente Icon con un pequeño conjunto propio (`icon::Core`) que cada tema puede sustituir; Bootsier usa Bootstrap Icons. `Button`, `nav::Item` y `dropdown::Item` admiten iconos al principio y al final, y el panel de administración los usa en secciones y páginas. --- assets/scss/basic.scss | 1 + assets/scss/basic/_icon.scss | 57 +++++ .../assets/_bootsier-custom.scss | 57 +++++ extensions/pagetop-bootsier/src/lib.rs | 1 + extensions/pagetop-bootsier/src/theme.rs | 2 + extensions/pagetop-bootsier/src/theme/bs.rs | 7 + .../pagetop-bootsier/src/theme/bs/icon.rs | 174 +++++-------- .../pagetop-bootsier/src/theme/bs/nav.rs | 8 +- extensions/pagetop-bootsier/tests/icon.rs | 67 +++++ .../src/component/account_menu.rs | 17 +- extensions/pagetop-user/src/lib.rs | 9 +- src/admin/registry.rs | 50 +++- src/base/component.rs | 4 + src/base/component/button/component.rs | 55 +++- src/base/component/dropdown/item.rs | 41 ++- src/base/component/icon.rs | 14 ++ src/base/component/icon/component.rs | 161 ++++++++++++ src/base/component/icon/core_set.rs | 140 +++++++++++ src/base/component/icon/source.rs | 76 ++++++ src/base/component/nav/component.rs | 15 +- src/base/component/nav/item.rs | 43 +++- tests/admin_template.rs | 26 +- tests/component_icon.rs | 234 ++++++++++++++++++ 23 files changed, 1098 insertions(+), 161 deletions(-) create mode 100644 assets/scss/basic/_icon.scss create mode 100644 extensions/pagetop-bootsier/tests/icon.rs create mode 100644 src/base/component/icon.rs create mode 100644 src/base/component/icon/component.rs create mode 100644 src/base/component/icon/core_set.rs create mode 100644 src/base/component/icon/source.rs create mode 100644 tests/component_icon.rs diff --git a/assets/scss/basic.scss b/assets/scss/basic.scss index ccf728c9..37f27f33 100644 --- a/assets/scss/basic.scss +++ b/assets/scss/basic.scss @@ -25,6 +25,7 @@ @use "basic/dialog"; @use "basic/dropdown"; @use "basic/form"; +@use "basic/icon"; @use "basic/image"; @use "basic/list-filter"; @use "basic/list-view"; diff --git a/assets/scss/basic/_icon.scss b/assets/scss/basic/_icon.scss new file mode 100644 index 00000000..4bb5be8d --- /dev/null +++ b/assets/scss/basic/_icon.scss @@ -0,0 +1,57 @@ +// Inline SVG icons (`Icon`) sit on the text baseline like a glyph of the surrounding font. +svg.icon { + flex-shrink: 0; + vertical-align: -0.125em; +} + +// Color of an icon with an intent (`Icon::with_intent()`). +.icon-primary { color: var(--val-color--primary); } +.icon-neutral { color: var(--val-color--secondary); } +.icon-info { color: var(--val-color--info); } +.icon-success { color: var(--val-color--success); } +.icon-warning { color: var(--val-color--warning); } +.icon-severe { color: var(--val-color--danger); } + +// Icons of menu items (`with_icon_start()`/`with_icon_end()` of `nav::Item` and `dropdown::Item`). +// In a row of links, each icon keeps its distance from the label. +.nav-link > .icon-start { + margin-inline-end: 0.5em; +} +.nav-link > .icon-end { + margin-inline-start: 0.5em; +} + +// In a vertical list (dropdown menu or vertical nav) with any icon, every item is a flex row: the +// end icon goes to the edge, and an item without an icon on one side keeps that space empty, so +// that all the labels line up. The `:has()` selectors look only at the items of this list, not at +// those of a nested menu. +.dropdown-menu:has(> li > * > .icon) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon) > li > .nav-link { + display: flex; + align-items: center; + gap: 0.5em; + + > .icon-start, + > .icon-end { + margin: 0; + } + > .icon-end { + margin-inline-start: auto; + } +} +.dropdown-menu:has(> li > * > .icon-start) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon-start) > li > .nav-link { + &:not(:has(> .icon-start))::before { + content: ""; + flex: 0 0 1em; + } +} +// The toggle of a dropdown already shows its arrow at the end. +.dropdown-menu:has(> li > * > .icon-end) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon-end) > li > .nav-link:not(.dropdown-toggle) { + &:not(:has(> .icon-end))::after { + content: ""; + flex: 0 0 1em; + margin-inline-start: auto; + } +} diff --git a/extensions/pagetop-bootsier/assets/_bootsier-custom.scss b/extensions/pagetop-bootsier/assets/_bootsier-custom.scss index 9ac5e0fd..23869ee9 100644 --- a/extensions/pagetop-bootsier/assets/_bootsier-custom.scss +++ b/extensions/pagetop-bootsier/assets/_bootsier-custom.scss @@ -226,6 +226,63 @@ fieldset > legend { @extend .alert-danger; } +// Inline SVG icons (`Icon`) sit on the text baseline like the Bootstrap Icons glyphs. +svg.icon { + flex-shrink: 0; + vertical-align: -0.125em; +} + +// Color of an icon with an intent (`Icon::with_intent()`), named after the Bootsier palette. +@each $color in primary, secondary, success, info, warning, danger { + .icon-#{$color} { + color: var(--bs-#{$color}); + } +} + +// Icons of menu items (`with_icon_start()`/`with_icon_end()` of `nav::Item` and `dropdown::Item`). +// In a row of links, each icon keeps its distance from the label. +.nav-link > .icon-start { + margin-inline-end: 0.5em; +} +.nav-link > .icon-end { + margin-inline-start: 0.5em; +} + +// In a vertical list (dropdown menu or vertical nav) with any icon, every item is a flex row: the +// end icon goes to the edge, and an item without an icon on one side keeps that space empty, so +// that all the labels line up. The `:has()` selectors look only at the items of this list, not at +// those of a nested menu. +.dropdown-menu:has(> li > * > .icon) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon) > li > .nav-link { + display: flex; + align-items: center; + gap: 0.5em; + + > .icon-start, + > .icon-end { + margin: 0; + } + > .icon-end { + margin-inline-start: auto; + } +} +.dropdown-menu:has(> li > * > .icon-start) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon-start) > li > .nav-link { + &:not(:has(> .icon-start))::before { + content: ""; + flex: 0 0 1em; + } +} +// The toggle of a dropdown already shows its arrow at the end. +.dropdown-menu:has(> li > * > .icon-end) > li > :is(.dropdown-item, .dropdown-item-text), +.nav-vertical:has(> li > .nav-link > .icon-end) > li > .nav-link:not(.dropdown-toggle) { + &:not(:has(> .icon-end))::after { + content: ""; + flex: 0 0 1em; + margin-inline-start: auto; + } +} + // Sort indicator on sortable table headers (`table::SortLink`), using Bootstrap Icons' // sort-up/sort-down glyphs instead of the plain triangle from basic.css. .table-sort::after { diff --git a/extensions/pagetop-bootsier/src/lib.rs b/extensions/pagetop-bootsier/src/lib.rs index a597b81f..9b533006 100644 --- a/extensions/pagetop-bootsier/src/lib.rs +++ b/extensions/pagetop-bootsier/src/lib.rs @@ -283,6 +283,7 @@ impl Theme for Bootsier { Container => |c| theme::bs::container::setup(c), Dialog => |c| theme::bs::dialog::setup(c), Dropdown => |c| theme::bs::dropdown::setup(c), + Icon => |c| theme::bs::icon::setup(c), Image => |c| theme::bs::image::setup(c), Nav => |c| theme::bs::nav::setup(c), Navbar => |c| theme::bs::navbar::setup(c), diff --git a/extensions/pagetop-bootsier/src/theme.rs b/extensions/pagetop-bootsier/src/theme.rs index 1286b959..bd1352e0 100644 --- a/extensions/pagetop-bootsier/src/theme.rs +++ b/extensions/pagetop-bootsier/src/theme.rs @@ -82,6 +82,8 @@ pub use bs::form::select::SelectBsExt; #[doc(hidden)] pub use bs::form::textarea::TextareaBsExt; #[doc(hidden)] +pub use bs::icon::IconBsExt; +#[doc(hidden)] pub use bs::nav::NavBsExt; #[doc(hidden)] pub use bs::navbar::NavbarBsExt; diff --git a/extensions/pagetop-bootsier/src/theme/bs.rs b/extensions/pagetop-bootsier/src/theme/bs.rs index 79c577ee..7f4c69ec 100644 --- a/extensions/pagetop-bootsier/src/theme/bs.rs +++ b/extensions/pagetop-bootsier/src/theme/bs.rs @@ -50,6 +50,13 @@ pub use form::select::SelectBsExt; #[doc(inline)] pub use form::textarea::TextareaBsExt; +// Icon. +pub mod icon; +#[doc(inline)] +pub use icon::Icon; +#[doc(inline)] +pub use icon::IconBsExt; + // Image. pub mod image; #[doc(inline)] diff --git a/extensions/pagetop-bootsier/src/theme/bs/icon.rs b/extensions/pagetop-bootsier/src/theme/bs/icon.rs index a0ba4f3a..9b1d8af7 100644 --- a/extensions/pagetop-bootsier/src/theme/bs/icon.rs +++ b/extensions/pagetop-bootsier/src/theme/bs/icon.rs @@ -1,128 +1,68 @@ -use crate::theme::*; +//! Definiciones para mostrar iconos ([`Icon`]). -const DEFAULT_VIEWBOX: &str = "0 0 16 16"; +use pagetop::prelude::*; -#[derive(AutoDefault, Clone)] -pub enum IconKind { - #[default] - None, - Font(FontSize), - Svg { - shapes: Markup, - viewbox: AttrValue, - }, +pub use pagetop::base::component::icon::{Core, Icon, Source}; + +/// Extensión de Bootsier para [`Icon`]. +/// +/// Añade los iconos de [Bootstrap Icons](https://icons.getbootstrap.com/), que Bootsier ya carga +/// como fuente de iconos en todas las páginas. Además, Bootsier muestra los iconos del conjunto de +/// PageTop ([`Core`]) con su equivalente de Bootstrap Icons. +/// +/// ```rust,no_run +/// use pagetop::prelude::*; +/// use pagetop_bootsier::theme::*; +/// +/// let home = bs::Icon::bi("house-fill").with_label(Lc::n("Home")); +/// ``` +pub trait IconBsExt: Sized { + /// Crea un icono de Bootstrap Icons a partir de su nombre, sin el prefijo `bi-` (p. ej. + /// `"house-fill"`). + fn bi(name: impl AsRef) -> Self; } -#[derive(AutoDefault, Clone, Debug, Getters)] -pub struct Icon { - /// Devuelve identificador, clases CSS, atributos HTML y valores extra del componente. - props: Props, - icon_kind: IconKind, - aria_label: AttrLc, -} - -#[async_trait] -impl Component for Icon { - fn new() -> Self { - Self::default() - } - - fn id(&self) -> Option { - self.props.get_id() - } - - fn setup(&mut self, _cx: &mut Context) { - if !matches!(self.icon_kind(), IconKind::None) { - self.alter_prop(PropsOp::prepend_classes("icon")); - } - if let IconKind::Font(font_size) = self.icon_kind() { - self.alter_prop(PropsOp::add_classes(font_size.as_str())); - } - } - - async fn prepare(&self, cx: &mut Context) -> Result { - Ok(match self.icon_kind() { - IconKind::None => html! {}, - IconKind::Font(_) => { - let aria_label = self.aria_label().lookup(cx); - let has_label = aria_label.is_some(); - html! { - i - (self.props().unpack(cx)) - role=[has_label.then_some("img")] - aria-label=[aria_label] - aria-hidden=[(!has_label).then_some("true")] - {} - } - } - IconKind::Svg { shapes, viewbox } => { - let aria_label = self.aria_label().lookup(cx); - let has_label = aria_label.is_some(); - let viewbox = viewbox.get().unwrap_or_else(|| DEFAULT_VIEWBOX.to_string()); - html! { - svg - xmlns="http://www.w3.org/2000/svg" - viewBox=(viewbox) - fill="currentColor" - focusable="false" - (self.props().unpack(cx)) - role=[has_label.then_some("img")] - aria-label=[aria_label] - aria-hidden=[(!has_label).then_some("true")] - { - (shapes) - } - } - } - }) +impl IconBsExt for Icon { + fn bi(name: impl AsRef) -> Self { + Icon::with(Source::font(util::join!("bi bi-", name.as_ref()))) } } -#[builder_impl] -impl Icon { - pub fn font() -> Self { - Self::default().with_icon_kind(IconKind::Font(FontSize::default())) - } +// **< Icon SETUP >********************************************************************************* - pub fn font_sized(font_size: FontSize) -> Self { - Self::default().with_icon_kind(IconKind::Font(font_size)) - } +// Sustituye cada icono del conjunto de PageTop por su equivalente de Bootstrap Icons. El `setup()` +// del icono ya se ha ejecutado, así que las clases del glifo se añaden aquí; el origen cambia para +// que se pinte como glifo. +pub(crate) fn setup(icon: &mut Icon) { + let Some(&Source::Core(core)) = icon.source() else { + return; + }; + let classes = bi_classes(core); + icon.alter_prop(PropsOp::add_classes(classes)); + icon.alter_source(Source::font(classes)); +} - pub fn svg(shapes: Markup) -> Self { - Self::default().with_icon_kind(IconKind::Svg { - shapes, - viewbox: AttrValue::default(), - }) - } - - pub fn svg_with_viewbox(shapes: Markup, viewbox: impl AsRef) -> Self { - Self::default().with_icon_kind(IconKind::Svg { - shapes, - viewbox: AttrValue::new(viewbox), - }) - } - - // **< Icon BUILDER >*************************************************************************** - - /// Establece el identificador único del componente; igual a `with_prop(PropsOp::set_id(id))`. - pub fn with_id(mut self, id: impl Into) -> Self { - self.props.alter_id(id); - self - } - - /// Modifica identificador, clases CSS, atributos HTML o valores extra del componente. - pub fn with_prop(mut self, op: impl Into) -> Self { - self.props.alter_prop(op); - self - } - - pub fn with_icon_kind(mut self, icon_kind: IconKind) -> Self { - self.icon_kind = icon_kind; - self - } - - pub fn with_aria_label(mut self, label: Lc) -> Self { - self.aria_label.alter_value(label); - self +#[rustfmt::skip] +const fn bi_classes(icon: Core) -> &'static str { + match icon { + Core::BarChart => "bi bi-bar-chart", + Core::Check => "bi bi-check-lg", + Core::ChevronDown => "bi bi-chevron-down", + Core::ChevronLeft => "bi bi-chevron-left", + Core::ChevronRight => "bi bi-chevron-right", + Core::ChevronUp => "bi bi-chevron-up", + Core::Close => "bi bi-x-lg", + Core::Gear => "bi bi-gear", + Core::Home => "bi bi-house", + Core::Key => "bi bi-key", + Core::Layers => "bi bi-layers", + Core::Minus => "bi bi-dash-lg", + Core::People => "bi bi-people", + Core::Person => "bi bi-person", + Core::Plus => "bi bi-plus-lg", + Core::QuestionCircle => "bi bi-question-circle", + Core::Shield => "bi bi-shield-lock", + Core::SignOut => "bi bi-box-arrow-right", + Core::Sliders => "bi bi-sliders", } } diff --git a/extensions/pagetop-bootsier/src/theme/bs/nav.rs b/extensions/pagetop-bootsier/src/theme/bs/nav.rs index 664316e3..dcc72f67 100644 --- a/extensions/pagetop-bootsier/src/theme/bs/nav.rs +++ b/extensions/pagetop-bootsier/src/theme/bs/nav.rs @@ -95,13 +95,15 @@ fn layout_class(layout: nav::Layout, classes: &mut String) { // `data-bs-toggle="dropdown"` para que el JS de Bootstrap lo reconozca (la clase `dropdown-toggle` // por sí sola sólo aporta el estilo, no la inicialización). pub(crate) async fn item_render(item: &Item, cx: &mut Context) -> Result { + let start = item.icon_start().render(cx).await; + let end = item.icon_end().render(cx).await; Ok(match item.item_kind() { ItemKind::Void => html! {}, ItemKind::Label(label) => html! { li (item.props().unpack(cx)) { span class="nav-link disabled" aria-disabled="true" { - (label.using(cx)) + (start) (label.using(cx)) (end) } } }, @@ -144,7 +146,7 @@ pub(crate) async fn item_render(item: &Item, cx: &mut Context) -> Result Result