Class: NitroKit::DangerZone

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

Defined Under Namespace

Classes: Child

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, description: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DangerZone

Returns a new instance of DangerZone.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/components/nitro_kit/danger_zone.rb', line 7

def initialize(
  title: nil,
  description: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @title_content = content_from_keyword(:title, title)
  @description_content = content_from_keyword(:description, description)
  @confirmation = nil
  @escape = nil

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

Instance Method Details

#confirmation(&content) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'app/components/nitro_kit/danger_zone.rb', line 57

def confirmation(&content)
  raise ArgumentError, "DangerZone confirmation requires a block" unless content
  raise ArgumentError, "DangerZone accepts exactly one confirmation" if @confirmation

  @confirmation = content
  nil
end

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



52
53
54
55
# File 'app/components/nitro_kit/danger_zone.rb', line 52

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

#escape(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


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

def escape(component, &content)
  unless component.is_a?(NitroKit::Button)
    raise ArgumentError, "DangerZone escape must be a NitroKit::Button"
  end
  if component.variant == :destructive
    raise ArgumentError, "DangerZone escape cannot use the destructive Button variant"
  end
  raise ArgumentError, "DangerZone accepts at most one safe escape action" if @escape

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

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



47
48
49
50
# File 'app/components/nitro_kit/danger_zone.rb', line 47

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

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

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/nitro_kit/danger_zone.rb', line 31

def view_template
  yield self if block_given?
  require_content!("DangerZone", :title, @title_content)
  require_content!("DangerZone", :description, @description_content)
  raise ArgumentError, "DangerZone requires exactly one confirmation" unless @confirmation

  section(**root_attributes) do
    header(**slot_attributes(:header)) do
      h2(**slot_attributes(:title)) { render_deferred_content(@title_content) }
      p(**slot_attributes(:description)) { render_deferred_content(@description_content) }
    end
    div(**slot_attributes(:confirmation), &@confirmation)
    render_in_slot(@escape.component, :escape, &@escape.content) if @escape
  end
end