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 error].freeze
VARIANT_PALETTE =

The palette family each semantic variant borrows in palette.css. Alert owns this mapping; it is not the Badge color axis.

{
  default: :zinc,
  info: :blue,
  success: :green,
  warning: :amber,
  error: :red
}.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.



22
23
24
25
26
27
28
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/alert.rb', line 22

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.



50
51
52
# File 'app/components/nitro_kit/alert.rb', line 50

def variant
  @variant
end

Instance Method Details

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



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

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

#icon(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


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

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



77
78
79
80
81
# File 'app/components/nitro_kit/alert.rb', line 77

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

#view_template(&block) ⇒ Object



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

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