Class: Shadcn::Sidebar::MenuButton::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/sidebar/menu_button/component.rb

Overview

SidebarMenuButton — the row a sidebar is mostly made of.

tooltip: wraps the button in a Tooltip, as upstream does (vendor/shadcn/ui/sidebar.tsx:534-542). Upstream decides whether to show it with a runtime prop — hidden={state !== "collapsed" || isMobile} — which a server cannot evaluate. Here the same two conditions are CSS, read off the panel's own group: data-state is what the controller already writes, and data-mobile is what it sets while the mobile sheet is open. Same result, decided by the browser rather than by the render.

Constant Summary collapse

TOOLTIP_CLASSES =

A tooltip is only of use when the label beside the icon is gone, so it is hidden while the sidebar is expanded, and while the mobile sheet is showing the full labels anyway.

"group-data-[state=expanded]:hidden group-data-[mobile=true]:hidden"

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #tag_name

Constructor Details

#initialize(variant: :default, size: :default, active: false, tooltip: nil, **attributes) ⇒ Component

Returns a new instance of Component.



65
66
67
68
69
70
71
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 65

def initialize(variant: :default, size: :default, active: false, tooltip: nil, **attributes)
  @variant = variant&.to_sym || :default
  @size = size&.to_sym || :default
  @active = active
  @tooltip = tooltip
  super(**attributes)
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



63
64
65
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 63

def active
  @active
end

#sizeObject (readonly)

Returns the value of attribute size.



63
64
65
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 63

def size
  @size
end

#tooltipObject (readonly)

Returns the value of attribute tooltip.



63
64
65
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 63

def tooltip
  @tooltip
end

#variantObject (readonly)

Returns the value of attribute variant.



63
64
65
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 63

def variant
  @variant
end

Instance Method Details

#callObject



73
74
75
76
77
78
79
80
81
82
83
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 73

def call
  return render_element(body: content) if tooltip.blank?

  render(Shadcn::Tooltip::Component.new(side: :right, align: :center)) do |wrapper|
    # The trigger takes this button's own attributes, `data-slot`
    # included, so the element in the document is still a
    # `sidebar-menu-button` — which is what upstream's `asChild` does.
    wrapper.with_trigger(as: tag_name, **element_attributes.transform_keys(&:to_sym)) { content }
    wrapper.with_tooltip_content(class: TOOLTIP_CLASSES) { tooltip }
  end
end

#element_attributes(**defaults) ⇒ Object



89
90
91
92
93
94
95
96
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 89

def element_attributes(**defaults)
  super(**{
    type: "button",
    "data-sidebar" => "menu-button",
    "data-size" => size,
    "data-active" => active.to_s
  }.merge(defaults))
end

#style_variantsObject



85
86
87
# File 'app/components/shadcn/sidebar/menu_button/component.rb', line 85

def style_variants
  { variant:, size: }
end