Class: NitroKit::DangerZone
- Defined in:
- app/components/nitro_kit/danger_zone.rb
Defined Under Namespace
Classes: Child
Constant Summary collapse
- TITLE_LEVELS =
(1..6).freeze
Constants inherited from Component
Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES
Instance Method Summary collapse
- #confirmation(&content) ⇒ Object
- #description(text = nil, &block) ⇒ Object
- #escape(component, &content) ⇒ Object
-
#initialize(title: nil, description: nil, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DangerZone
constructor
A new instance of DangerZone.
- #title(text = nil, &block) ⇒ Object
- #view_template {|_self| ... } ⇒ Object
Constructor Details
#initialize(title: nil, description: nil, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DangerZone
Returns a new instance of DangerZone.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/components/nitro_kit/danger_zone.rb', line 9 def initialize( title: nil, description: nil, 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) @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
61 62 63 64 65 66 67 |
# File 'app/components/nitro_kit/danger_zone.rb', line 61 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
56 57 58 59 |
# File 'app/components/nitro_kit/danger_zone.rb', line 56 def description(text = nil, &block) @description_content = declare_content(:description, @description_content, text, &block) nil end |
#escape(component, &content) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/components/nitro_kit/danger_zone.rb', line 69 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
51 52 53 54 |
# File 'app/components/nitro_kit/danger_zone.rb', line 51 def title(text = nil, &block) @title_content = declare_content(:title, @title_content, text, &block) nil end |
#view_template {|_self| ... } ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/components/nitro_kit/danger_zone.rb', line 35 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 public_send(:"h#{@level}", **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 |