Class: UI::DialogComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
DialogBehavior
Defined in:
app/view_components/ui/dialog_component.rb

Overview

Dialog container component (ViewComponent) A modal dialog window that renders content over the main page

Examples:

Basic usage

<%= render UI::DialogComponent.new do %>
  <%= render UI::TriggerComponent.new { "Open Dialog" } %>
  <%= render UI::OverlayComponent.new do %>
    <%= render UI::ContentComponent.new do %>
      <%= render UI::HeaderComponent.new do %>
        <%= render UI::TitleComponent.new { "Dialog Title" } %>
        <%= render UI::DescriptionComponent.new { "Dialog description" } %>
      <% end %>
      <!-- Content -->
      <%= render UI::FooterComponent.new do %>
        <%= render UI::CloseComponent.new { "Cancel" } %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

Instance Method Summary collapse

Methods included from DialogBehavior

#dialog_base_classes, #dialog_classes, #dialog_data_attributes, #dialog_html_attributes, #merged_dialog_data_attributes

Constructor Details

#initialize(open: false, close_on_escape: true, close_on_overlay_click: true, classes: "", **attributes) ⇒ DialogComponent

Returns a new instance of DialogComponent.

Parameters:

  • open (Boolean) (defaults to: false) —

    whether the dialog is open

  • close_on_escape (Boolean) (defaults to: true) —

    close on Escape key press

  • close_on_overlay_click (Boolean) (defaults to: true) —

    close on overlay click

  • classes (String) (defaults to: "") —

    additional CSS classes

  • attributes (Hash) —

    additional HTML attributes



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/view_components/ui/dialog_component.rb', line 30

def initialize(
  open: false,
  close_on_escape: true,
  close_on_overlay_click: true,
  classes: "",
  **attributes
)
  @open = open
  @close_on_escape = close_on_escape
  @close_on_overlay_click = close_on_overlay_click
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#call ⇒ Object



44
45
46
47
48
49
# File 'app/view_components/ui/dialog_component.rb', line 44

def call
  attrs = dialog_html_attributes
  attrs[:data] = attrs[:data].merge(@attributes.fetch(:data, {}))

   :div, content, **attrs.merge(@attributes.except(:data))
end