Class: NitroKit::Dropdown

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

Defined Under Namespace

Classes: Entry, Trigger

Constant Summary collapse

PLACEMENTS =
%i[bottom_start bottom_end top_start top_end].freeze
ITEM_VARIANTS =
%i[default destructive].freeze
ITEM_TYPES =
%i[button submit reset].freeze

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: nil, placement: :bottom_start, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Dropdown

Returns a new instance of Dropdown.



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
# File 'app/components/nitro_kit/dropdown.rb', line 38

def initialize(
  id: nil,
  placement: :bottom_start,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @identifier = component_id(id)
  @placement = validate_choice!(:placement, placement, PLACEMENTS)
  @entries = []

  super(
    component: :dropdown,
    attributes: {
      id: @identifier,
      data: {
        controller: "nk--dropdown",
        placement: placement_value
      }
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



66
67
68
# File 'app/components/nitro_kit/dropdown.rb', line 66

def identifier
  @identifier
end

#placementObject (readonly)

Returns the value of attribute placement.



66
67
68
# File 'app/components/nitro_kit/dropdown.rb', line 66

def placement
  @placement
end

Instance Method Details

#item(text = nil, href: nil, icon: nil, variant: :default, type: :button, disabled: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/components/nitro_kit/dropdown.rb', line 128

def item(
  text = nil,
  href: nil,
  icon: nil,
  variant: :default,
  type: :button,
  disabled: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  ensure_collecting!
  validate_content!(:item, text, content)
  variant = validate_choice!(:variant, variant, ITEM_VARIANTS)
  type = validate_choice!(:type, type.to_s.to_sym, ITEM_TYPES)
  disabled = validate_boolean!(:disabled, disabled)
  if !href.nil? && (!href.is_a?(String) || href.empty?)
    raise ArgumentError, "Dropdown item href must be nil or a non-blank String"
  end

  add_entry(
    kind: :item,
    text:,
    href:,
    icon:,
    variant:,
    type:,
    disabled:,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class,
    content:
  )
end

#separator(html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'app/components/nitro_kit/dropdown.rb', line 166

def separator(html: {}, aria: {}, data: {}, desperately_need_a_class: nil)
  ensure_collecting!
  add_entry(
    kind: :separator,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class
  )
end

#title(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/components/nitro_kit/dropdown.rb', line 114

def title(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content)
  ensure_collecting!
  validate_content!(:title, text, content)
  add_entry(
    kind: :title,
    text:,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class,
    content:
  )
end

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

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
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
# File 'app/components/nitro_kit/dropdown.rb', line 77

def trigger(
  text = nil,
  variant: :default,
  size: :md,
  icon: nil,
  icon_end: nil,
  label: nil,
  disabled: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  ensure_collecting!
  raise ArgumentError, "Dropdown accepts exactly one trigger" if @trigger
  if text.nil? && !content && icon.nil? && icon_end.nil?
    raise ArgumentError, "Dropdown trigger requires text, a block, or an icon"
  end

  @trigger = Trigger.new(
    text:,
    variant: validate_trigger_choice!(:variant, variant, Button::VARIANTS),
    size: validate_trigger_choice!(:size, size, Button::SIZES),
    icon:,
    icon_end:,
    label:,
    disabled: validate_boolean!(:disabled, disabled),
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class,
    content:
  )
  nil
end

#view_template(&block) ⇒ Object



68
69
70
71
72
73
74
75
# File 'app/components/nitro_kit/dropdown.rb', line 68

def view_template(&block)
  collect_entries(&block)

  div(**root_attributes) do
    render_trigger
    render_content
  end
end