Class: UI::SidebarMenuButton

Inherits:
Phlex::HTML
  • Object
show all
Includes:
SidebarMenuButtonBehavior
Defined in:
app/components/ui/sidebar_menu_button.rb

Overview

MenuButton - Phlex implementation

Interactive button within a sidebar menu item. Supports variants, sizes, and active state.

Examples:

Basic usage

render UI::MenuButton.new do
  render UI::Icon.new(name: "home")
  span { "Home" }
end

As a link with asChild

render UI::MenuButton.new(as_child: true) do |attrs|
  a(**attrs, href: "/home") do
    render UI::Icon.new(name: "home")
    span { "Home" }
  end
end

Active state

render UI::MenuButton.new(active: true) do
  render UI::Icon.new(name: "settings")
  span { "Settings" }
end

With tooltip for icon mode

render UI::TooltipProvider.new do
  render UI::Tooltip.new do
    render UI::TooltipTrigger.new(as_child: true) do |trigger_attrs|
      render UI::MenuButton.new(**trigger_attrs) do
        render UI::Icon.new(name: "home")
        span { "Home" }
      end
    end
    render UI::TooltipContent.new(side: "right") { "Home" }
  end
end

Constant Summary

Constants included from SidebarMenuButtonBehavior

UI::SidebarMenuButtonBehavior::SIZES, UI::SidebarMenuButtonBehavior::VARIANTS

Instance Method Summary collapse

Methods included from SidebarMenuButtonBehavior

#sidebar_menu_button_classes, #sidebar_menu_button_data_attributes, #sidebar_menu_button_html_attributes

Constructor Details

#initialize(variant: :default, size: :default, active: false, as_child: false, classes: "", **attributes) ⇒ SidebarMenuButton

Returns a new instance of SidebarMenuButton.



43
44
45
46
47
48
49
50
# File 'app/components/ui/sidebar_menu_button.rb', line 43

def initialize(variant: :default, size: :default, active: false, as_child: false, classes: "", **attributes)
  @variant = variant.to_sym
  @size = size.to_sym
  @active = active
  @as_child = as_child
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#view_template(&block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/components/ui/sidebar_menu_button.rb', line 52

def view_template(&block)
  if @as_child
    yield sidebar_menu_button_html_attributes.deep_merge(@attributes)
  else
    all_attributes = sidebar_menu_button_html_attributes

    if @attributes.key?(:class)
      merged_class = TailwindMerge::Merger.new.merge([
        all_attributes[:class],
        @attributes[:class]
      ].compact.join(" "))
      all_attributes = all_attributes.merge(class: merged_class)
    end

    all_attributes = all_attributes.deep_merge(@attributes.except(:class))

    button(**all_attributes, &block)
  end
end