Class: NitroKit::Dialog

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/dialog.rb

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, dismissible: true, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Dialog

Returns a new instance of Dialog.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/nitro_kit/dialog.rb', line 14

def initialize(
  id:,
  dismissible: true,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  unless id.is_a?(String) && id.present? && !id.match?(/\s/)
    raise ArgumentError, "Dialog id: must be a non-blank String without whitespace"
  end

  @id = id
  @dismissible = validate_boolean!(:dismissible, dismissible)

  super(
    component: :dialog,
    attributes: {
      id:,
      data: {
        controller: "nk--dialog",
        nk__dialog_dismissible_value: @dismissible
      }
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



45
46
47
# File 'app/components/nitro_kit/dialog.rb', line 45

def id
  @id
end

Instance Method Details

#close_button(label: I18n.t("nitro_kit.dialog.close"), html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/components/nitro_kit/dialog.rb', line 117

def close_button(
  label: I18n.t("nitro_kit.dialog.close"),
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  ensure_rendering_panel!
  raise ArgumentError, "Dialog accepts at most one close button" if @close_button
  unless @dismissible
    raise ArgumentError, "Dialog dismissible: false renders no close button"
  end

  @close_button = CloseButton.new(
    label:,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class
  )
  nil
end

#panel(title:, description: nil, nonmodal: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/components/nitro_kit/dialog.rb', line 84

def panel(
  title:,
  description: nil,
  nonmodal: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  ensure_collecting!
  raise ArgumentError, "Dialog accepts exactly one panel" if @panel

  unless title.is_a?(String) && title.present?
    raise ArgumentError, "Dialog title: must be a non-blank String"
  end
  unless description.nil? || (description.is_a?(String) && description.present?)
    raise ArgumentError, "Dialog description: must be nil or a non-blank String"
  end

  @panel = Panel.new(
    title:,
    description:,
    nonmodal: validate_boolean!(:nonmodal, nonmodal),
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class,
    content:
  )
  nil
end

#trigger(text = nil, variant: :default, size: :md, disabled: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/components/nitro_kit/dialog.rb', line 56

def trigger(
  text = nil,
  variant: :default,
  size: :md,
  disabled: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  ensure_collecting!
  raise ArgumentError, "Dialog accepts at most one trigger" if @trigger

  @trigger = Trigger.new(
    text:,
    variant:,
    size:,
    disabled: validate_boolean!(:disabled, disabled),
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class,
    content:
  )
  nil
end

#view_template(&block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'app/components/nitro_kit/dialog.rb', line 47

def view_template(&block)
  collect_declarations(&block)

  div(**root_attributes) do
    render_trigger if @trigger
    render_panel
  end
end