Class: Shadcn::Command::Dialog::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/command/dialog/component.rb

Overview

The palette: a Command inside a Dialog, which is what upstream's CommandDialog is — it renders no markup of its own beyond the classes it layers on both.

The title and the description are sr-only and required rather than optional, exactly as upstream defaults them: a dialog with no accessible name is a dialog a screen reader opens into silence.

Constant Summary collapse

COMMAND_CLASSES =

Upstream's own spacing for a palette in a dialog, which is where the cmdk-* attributes earn their keep: the rows are taller and the headings are indented, and every selector reaches this port's DOM.

"**:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 " \
"[&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground " \
"[&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 " \
"[&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 " \
"[&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 " \
"[&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5"
CONTENT_CLASSES =
"overflow-hidden p-0"

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, #element_attributes, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(title: "Command Palette", description: "Search for a command to run...", show_close_button: true, label: nil, **attributes) ⇒ Component

Returns a new instance of Component.



27
28
29
30
31
32
33
34
# File 'app/components/shadcn/command/dialog/component.rb', line 27

def initialize(title: "Command Palette", description: "Search for a command to run...",
               show_close_button: true, label: nil, **attributes)
  @title = title
  @description = description
  @show_close_button = show_close_button
  @label = label
  super(**attributes)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



25
26
27
# File 'app/components/shadcn/command/dialog/component.rb', line 25

def description
  @description
end

#labelObject (readonly)

Returns the value of attribute label.



25
26
27
# File 'app/components/shadcn/command/dialog/component.rb', line 25

def label
  @label
end

#show_close_buttonObject (readonly)

Returns the value of attribute show_close_button.



25
26
27
# File 'app/components/shadcn/command/dialog/component.rb', line 25

def show_close_button
  @show_close_button
end

#titleObject (readonly)

Returns the value of attribute title.



25
26
27
# File 'app/components/shadcn/command/dialog/component.rb', line 25

def title
  @title
end

Instance Method Details

#callObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/components/shadcn/command/dialog/component.rb', line 51

def call
  render(Shadcn::Dialog::Component.new(**attributes)) do |dialog|
    dialog.with_trigger(**@trigger_options.to_h) { view_context.capture(&@trigger_block) } if trigger?
    dialog.with_dialog_content(class: CONTENT_CLASSES, show_close_button:) do |dialog_content|
      dialog_content.with_header(class: "sr-only") do |header|
        header.with_title { title }
        header.with_description { description }
      end
      concat(command)
    end
  end
end