Class: Charming::Components::Toast

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/toast.rb

Overview

Toast is a small auto-dismissing notification panel, usually composited as an overlay anchored to a screen corner. Controllers manage its lifetime with the ‘show_toast` / `dismiss_toast` helpers (which pair it with a timer); the component itself just renders the styled box.

Toast.new(message: "Saved!", kind: :success)

kind picks the accent style: :info (default), :success, :warn, or :error.

Constant Summary collapse

KINDS =
%i[info success warn error].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(message:, kind: :info, width: nil, theme: nil) ⇒ Toast

message is the toast text. kind is the visual accent. width optionally fixes the box width (otherwise it hugs the message).



20
21
22
23
24
25
# File 'lib/charming/presentation/components/toast.rb', line 20

def initialize(message:, kind: :info, width: nil, theme: nil)
  super(theme: theme)
  @message = message.to_s
  @kind = KINDS.include?(kind) ? kind : :info
  @width = width
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



16
17
18
# File 'lib/charming/presentation/components/toast.rb', line 16

def kind
  @kind
end

#messageObject (readonly)

Returns the value of attribute message.



16
17
18
# File 'lib/charming/presentation/components/toast.rb', line 16

def message
  @message
end

Instance Method Details

#renderObject

Renders the bordered toast box with a kind-colored border.



28
29
30
# File 'lib/charming/presentation/components/toast.rb', line 28

def render
  box(message, style: toast_style)
end