Class: UI::DialogCloseComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- UI::DialogCloseComponent
- Defined in:
- app/view_components/ui/dialog_close_component.rb
Overview
Dialog close component (ViewComponent) Closes the dialog on click
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(as_child: false, variant: :outline, size: :default, classes: "", **attributes) ⇒ DialogCloseComponent
constructor
A new instance of DialogCloseComponent.
Constructor Details
#initialize(as_child: false, variant: :outline, size: :default, classes: "", **attributes) ⇒ DialogCloseComponent
Returns a new instance of DialogCloseComponent.
19 20 21 22 23 24 25 |
# File 'app/view_components/ui/dialog_close_component.rb', line 19 def initialize(as_child: false, variant: :outline, size: :default, classes: "", **attributes) @as_child = as_child @variant = variant @size = size @classes = classes @attributes = attributes end |
Instance Method Details
#call ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/view_components/ui/dialog_close_component.rb', line 27 def call close_attrs = { data: {action: "click->ui--dialog#close"} }.deep_merge(@attributes) if @as_child # asChild mode: merge attributes into child element rendered = content.to_s doc = Nokogiri::HTML::DocumentFragment.parse(rendered) first_element = doc.children.find { |node| node.element? } if first_element # Merge data attributes (convert Rails naming to HTML) close_attrs.fetch(:data, {}).each do |key, value| html_key = key.to_s.gsub("__", "--").tr("_", "-") first_element["data-#{html_key}"] = value end # Merge CSS classes with TailwindMerge if close_attrs[:class] existing_classes = first_element["class"] || "" merged_classes = TailwindMerge::Merger.new.merge([existing_classes, close_attrs[:class]].join(" ")) first_element["class"] = merged_classes end # Merge other attributes (except data and class) close_attrs.except(:data, :class).each do |key, value| first_element[key.to_s] = value end doc.to_html.html_safe else content end else # Default: render as Button component render(UI::ButtonComponent.new( variant: @variant, size: @size, classes: @classes, **close_attrs ).with_content(content)) end end |