Class: NitroKit::Alert

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

Constant Summary collapse

VARIANTS =
%i[default info success warning destructive].freeze
LIVE_MODES =
%i[off polite assertive].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(variant: :default, title: nil, description: nil, live: :off, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Alert

Returns a new instance of Alert.



12
13
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
# File 'app/components/nitro_kit/alert.rb', line 12

def initialize(
  variant: :default,
  title: nil,
  description: nil,
  live: :off,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @variant = validate_choice!(:variant, variant, VARIANTS)
  @live = validate_choice!(:live, live, LIVE_MODES)
  @title_content = content_from_keyword(:title, title)
  @description_content = content_from_keyword(:description, description)
  @icon = nil

  super(
    component: :alert,
    attributes: { id:, role: live_role },
    html:,
    aria:,
    data:,
    variant:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#variantObject (readonly)

Returns the value of attribute variant.



40
41
42
# File 'app/components/nitro_kit/alert.rb', line 40

def variant
  @variant
end

Instance Method Details

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



73
74
75
76
77
# File 'app/components/nitro_kit/alert.rb', line 73

def description(text = nil, &block)
  ensure_collecting!
  @description_content = declare_content(:description, @description_content, text, &block)
  nil
end

#icon(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
# File 'app/components/nitro_kit/alert.rb', line 56

def icon(component, &content)
  ensure_collecting!
  unless component.is_a?(NitroKit::Icon)
    raise ArgumentError, "Alert icon must be a NitroKit::Icon"
  end
  raise ArgumentError, "Alert accepts at most one icon" if @icon

  @icon = Child.new(component:, content:)
  nil
end

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



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

def title(text = nil, &block)
  ensure_collecting!
  @title_content = declare_content(:title, @title_content, text, &block)
  nil
end

#view_template(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/nitro_kit/alert.rb', line 42

def view_template(&block)
  collect_declarations(&block)

  div(**root_attributes) do
    render_in_slot(@icon.component, :icon, &@icon.content) if @icon
    if @title_content
      div(**slot_attributes(:title)) { render_deferred_content(@title_content) }
    end
    if @description_content
      div(**slot_attributes(:description)) { render_deferred_content(@description_content) }
    end
  end
end