Class: PhlexKit::SidebarMenuButton
- Inherits:
-
BaseComponent
- Object
- Phlex::HTML
- BaseComponent
- PhlexKit::SidebarMenuButton
- Defined in:
- app/components/phlex_kit/sidebar/sidebar_menu_button.rb
Overview
The interactive element of a SidebarMenuItem. as: is :button (default) or :a
(pass href:); active: true marks the current page (drives the highlight via
data-active — a named kwarg, not an attr, since mix would merge a repeated
attribute rather than override; an active link also gets aria-current="page").
tooltip: labels the button while an icon-collapsed rail hides its text
(pure-CSS hover bubble) and doubles as the aria-label so the button keeps an
accessible name once the text is hidden. Bare block text is wrapped in a
automatically so the collapsed rail's > :not(svg) rule can hide it;
blocks that emit their own markup (icon + Label) pass through
untouched — keep the label in a real element there. Attrs pass through via
mix. See sidebar.rb.
Instance Method Summary collapse
-
#initialize(as: :button, active: false, tooltip: nil, **attrs) ⇒ SidebarMenuButton
constructor
A new instance of SidebarMenuButton.
- #view_template(&block) ⇒ Object
Methods inherited from BaseComponent
Constructor Details
#initialize(as: :button, active: false, tooltip: nil, **attrs) ⇒ SidebarMenuButton
Returns a new instance of SidebarMenuButton.
14 15 16 17 18 19 |
# File 'app/components/phlex_kit/sidebar/sidebar_menu_button.rb', line 14 def initialize(as: :button, active: false, tooltip: nil, **attrs) @as = as @active = active @tooltip = tooltip @attrs = attrs end |
Instance Method Details
#view_template(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/components/phlex_kit/sidebar/sidebar_menu_button.rb', line 21 def view_template(&block) base = { class: "pk-sidebar-menu-button", "data-active": (@active ? "true" : "false") } base["aria-current"] = "page" if @active && @as == :a if @tooltip base["data-tooltip"] = @tooltip base["aria-label"] = @tooltip end send(@as, **mix(base, @attrs)) do # capture renders the block once; pure text (no markup) gets the span # wrapper the icon-collapsed CSS needs. The captured output is # already-escaped HTML, so re-emit it via raw(safe(...)). content = capture(&block) if content.include?("<") || content.empty? raw safe(content) else span { raw safe(content) } end end end |