Class: NitroKit::EmptyState

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

Constant Summary collapse

TITLE_LEVELS =
(2..6).freeze
VARIANTS =
%i[default borderless].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(title: nil, description: nil, variant: :default, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ EmptyState

Returns a new instance of EmptyState.



11
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/empty_state.rb', line 11

def initialize(
  title: nil,
  description: nil,
  variant: :default,
  level: 2,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @title_content = content_from_keyword(:title, title)
  @description_content = content_from_keyword(:description, description)
  @level = validate_choice!(:level, level, TITLE_LEVELS)
  @variant = validate_choice!(:variant, variant, VARIANTS)
  @icon = nil
  @actions = []

  super(
    component: :empty_state,
    attributes: { id: }.compact,
    html:,
    aria:,
    data:,
    variant:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#variantObject (readonly)

Returns the value of attribute variant.



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

def variant
  @variant
end

Instance Method Details

#action(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/nitro_kit/empty_state.rb', line 76

def action(component, &content)
  unless component.is_a?(NitroKit::Button)
    raise ArgumentError, "EmptyState actions must be NitroKit::Button instances"
  end
  if @actions.any? { |action| action.component.equal?(component) }
    raise ArgumentError, "EmptyState cannot contain the same Button twice"
  end
  raise ArgumentError, "EmptyState accepts at most two actions" if @actions.size == 2

  @actions << Child.new(component:, content:)
  nil
end

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



61
62
63
64
# File 'app/components/nitro_kit/empty_state.rb', line 61

def description(text = nil, &block)
  @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
# File 'app/components/nitro_kit/empty_state.rb', line 66

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

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

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



56
57
58
59
# File 'app/components/nitro_kit/empty_state.rb', line 56

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

#view_template {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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

def view_template
  yield self if block_given?
  require_content!("EmptyState", :title, @title_content)

  section(**root_attributes) do
    render_in_slot(@icon.component, :icon, &@icon.content) if @icon
    public_send(:"h#{level}", **slot_attributes(:title)) { render_deferred_content(@title_content) }
    if @description_content
      p(**slot_attributes(:description)) { render_deferred_content(@description_content) }
    end
    render_actions if @actions.any?
  end
end