Class: NitroKit::Toast
- Defined in:
- app/components/nitro_kit/toast.rb
Defined Under Namespace
Classes: FlashMessages, Item
Constant Summary collapse
- DEFAULT_ID =
"nk-toast"
Constants inherited from Component
Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#initialize(duration: 5_000, label: I18n.t("nitro_kit.toast.label"), id: DEFAULT_ID, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Toast
constructor
A new instance of Toast.
- #item(title: nil, description: nil, variant: :default, dismissible: true, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object
- #list_id ⇒ Object
- #view_template(&block) ⇒ Object
Constructor Details
#initialize(duration: 5_000, label: I18n.t("nitro_kit.toast.label"), id: DEFAULT_ID, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Toast
Returns a new instance of Toast.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/components/nitro_kit/toast.rb', line 150 def initialize( duration: 5_000, label: I18n.t("nitro_kit.toast.label"), id: DEFAULT_ID, html: {}, aria: {}, data: {}, desperately_need_a_class: nil ) unless duration.is_a?(Integer) && duration.positive? raise ArgumentError, "Toast duration must be a positive Integer" end unless label.is_a?(String) && label.present? raise ArgumentError, "Toast label must be a non-blank String" end unless id.is_a?(String) && id.present? && !id.match?(/\s/) raise ArgumentError, "Toast id: must be a non-blank String without whitespace" end @duration = duration @id = id @items = [] super( component: :toast, attributes: { id:, role: "region", data: { controller: "nk--toast", action: "turbo:before-cache@document->nk--toast#teardown", nk__toast_duration_value: duration } }, html:, aria: aria.merge(label:, live: "polite"), data:, desperately_need_a_class: ) end |
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
191 192 193 |
# File 'app/components/nitro_kit/toast.rb', line 191 def duration @duration end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
191 192 193 |
# File 'app/components/nitro_kit/toast.rb', line 191 def id @id end |
Instance Method Details
#item(title: nil, description: nil, variant: :default, dismissible: true, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'app/components/nitro_kit/toast.rb', line 209 def item( title: nil, description: nil, variant: :default, dismissible: true, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content ) ensure_collecting! component = Item.new( title:, description:, variant:, dismissible:, id:, html:, aria:, data:, desperately_need_a_class: ) @items << ItemDeclaration.new(component:, content:) nil end |
#list_id ⇒ Object
193 194 195 |
# File 'app/components/nitro_kit/toast.rb', line 193 def list_id "#{id}-list" end |
#view_template(&block) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/components/nitro_kit/toast.rb', line 197 def view_template(&block) collect_items(&block) section(**root_attributes) do ol(**slot_attributes(:list, attributes: { id: list_id })) do @items.each do |entry| render_in_slot(entry.component, :item, &entry.content) end end end end |