Class: NitroKit::Dialog

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

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(identifier: nil, **attrs) ⇒ Dialog



5
6
7
8
9
10
11
12
# File 'app/components/nitro_kit/dialog.rb', line 5

def initialize(identifier: nil, **attrs)
  @identifier = identifier || SecureRandom.hex(6)

  super(
    attrs,
    data: { controller: "nk--dialog", action: "click->nk--dialog#clickOutside" }
  )
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



14
15
16
# File 'app/components/nitro_kit/dialog.rb', line 14

def identifier
  @identifier
end

Instance Method Details

#close_button(**attrs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/nitro_kit/dialog.rb', line 65

def close_button(**attrs)
  builder do
    render(
      Button.new(
        **mattr(
          attrs,
          variant: :ghost,
          size: :sm,
          class: "absolute top-2 right-2",
          data: { action: "nk--dialog#close" }
        )
      )
    ) do
      render(Icon.new(:x))
    end
  end
end

#description(text = nil, **attrs, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/components/nitro_kit/dialog.rb', line 91

def description(text = nil, **attrs, &block)
  builder do
    div(
      **mattr(
        attrs,
        id: id(:description),
        class: "text-muted-content mb-6 text-sm leading-relaxed"
      )
    ) do
      text_or_block(text, &block)
    end
  end
end

#dialog(**attrs) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/components/nitro_kit/dialog.rb', line 47

def dialog(**attrs)
  builder do
    html_dialog(
      **mattr(
        attrs,
        class: dialog_class,
        data: { nk__dialog_target: "dialog" },
        aria: {
          labelledby: id(:title),
          describedby: id(:description)
        }
      )
    ) do
      yield
    end
  end
end

#html_dialogObject



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

alias :html_dialog :dialog

#title(text = nil, **attrs, &block) ⇒ Object



83
84
85
86
87
88
89
# File 'app/components/nitro_kit/dialog.rb', line 83

def title(text = nil, **attrs, &block)
  builder do
    h2(**mattr(attrs, id: id(:title), class: "text-lg font-semibold mb-2")) do
      text_or_block(text, &block)
    end
  end
end

#trigger(text = nil, as: Button, **attrs, &block) ⇒ Object



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 22

def trigger(text = nil, as: Button, **attrs, &block)
  builder do
    trigger_attrs = mattr(
      attrs,
      data: {
        nk__dialog_target: "trigger",
        action: "click->nk--dialog#open"
      }
    )

    case as
    when Symbol
      send(as, **trigger_attrs) do
        text_or_block(text, &block)
      end
    else
      render(as.new(**trigger_attrs)) do
        text_or_block(text, &block)
      end
    end
  end
end

#view_templateObject



16
17
18
19
20
# File 'app/components/nitro_kit/dialog.rb', line 16

def view_template
  div(**attrs) do
    yield
  end
end