Class: NitroKit::Dropdown

Inherits:
Component
  • Object
show all
Includes:
Phlex::Rails::Helpers::LinkTo
Defined in:
app/components/nitro_kit/dropdown.rb

Constant Summary collapse

ITEM_VARIANTS =
i[default destructive]

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(placement: nil, **attrs) ⇒ Dropdown

Returns a new instance of Dropdown.



9
10
11
12
13
14
15
16
17
18
19
# File 'app/components/nitro_kit/dropdown.rb', line 9

def initialize(placement: nil, **attrs)
  @placement = placement

  super(
    attrs,
    data: {
      controller: "nk--dropdown",
      nk__dropdown_placement_value: placement
    }
  )
end

Instance Attribute Details

#placementObject (readonly)

Returns the value of attribute placement.



21
22
23
# File 'app/components/nitro_kit/dropdown.rb', line 21

def placement
  @placement
end

Instance Method Details

#content(as: :div, **attrs) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/components/nitro_kit/dropdown.rb', line 50

def content(as: :div, **attrs)
  builder do
    div(
      **mattr(
        attrs,
        role: "menu",
        aria: { hidden: "true" },
        class: content_class,
        data: { nk__dropdown_target: "content" },
        popover: true
      )
    ) do
      yield
    end
  end
end

#destructive_item(*args, **attrs, &block) ⇒ Object



112
113
114
115
116
# File 'app/components/nitro_kit/dropdown.rb', line 112

def destructive_item(*args, **attrs, &block)
  builder do
    item(*args, **attrs, variant: :destructive, &block)
  end
end

#destructive_item_to(text_or_block, href = nil, **attrs, &block) ⇒ Object



118
119
120
121
122
123
# File 'app/components/nitro_kit/dropdown.rb', line 118

def destructive_item_to(text_or_block, href = nil, **attrs, &block)
  builder do
    href = args.shift if block_given?
    destructive_item(text_or_block, href: href, **attrs, &block)
  end
end

#item(text = nil, href: nil, variant: :default, **attrs, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/components/nitro_kit/dropdown.rb', line 75

def item(text = nil, href: nil, variant: :default, **attrs, &block)
  builder do
    common_attrs = mattr(
      attrs,
      role: "menuitem",
      tabindex: "-1",
      class: [ item_class, item_variant_class(variant) ]
    )

    if href
      link_to(href, **common_attrs) do
        text_or_block(text, &block)
      end
    else
      div(**common_attrs) do
        text_or_block(text, &block)
      end
    end
  end
end

#item_to(text_or_href, href = nil, **attrs, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/components/nitro_kit/dropdown.rb', line 96

def item_to(
  text_or_href,
  href = nil,
  **attrs,
  &block
)
  builder do
    if block_given?
      href = text_or_href
      text_or_href = nil
    end

    item(text_or_href, href: href, **attrs, &block)
  end
end

#separatorObject



125
126
127
128
129
# File 'app/components/nitro_kit/dropdown.rb', line 125

def separator
  builder do
    hr(class: separator_class)
  end
end

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



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

def title(text = nil, **attrs, &block)
  builder do
    div(**mattr(attrs, class: title_class)) do
      text_or_block(text, &block)
    end
  end
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/components/nitro_kit/dropdown.rb', line 29

def trigger(text = nil, as: NitroKit::Button, **attrs, &block)
  builder do
    trigger_attrs = mattr(
      attrs,
      aria: { haspopup: "true", expanded: "false" },
      data: { nk__dropdown_target: "trigger", action: "click->nk--dropdown#toggle" }
    )

    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



23
24
25
26
27
# File 'app/components/nitro_kit/dropdown.rb', line 23

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