Class: DaisyUI::Chat

Inherits:
BaseComponent show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
app/components/daisy_ui/data_display/chat.rb

Overview

Chat component for displaying a list of chat messages

Examples:

Basic usage

<%= render(ChatComponent.new(messages: [
  { text: "Hello!", user_id: 1 },
  { text: "Hi there!", user_id: 2 }
], current_user_id: 1)) %>

With specific positions (overriding automatic positioning)

<%= render(ChatComponent.new(messages: [
  { text: "Hello!", position: :start },
  { text: "Hi there!", position: :end }
])) %>

With avatar, header and footer

<%= render(ChatComponent.new(
  messages: [
    {
      text: "Hello!",
      user_id: 1,
      avatar: { img_src: "user.jpg", img_alt: "User" },
      header: { text: "John", time: "12:45" },
      footer: { text: "Delivered", time: "12:46" }
    }
  ],
  current_user_id: 2
)) %>

Instance Method Summary collapse

Constructor Details

#initialize(messages: [], current_user_id: nil, **system_arguments) ⇒ Chat

Returns a new instance of Chat.

Parameters:

  • messages (Array<Hash>) (defaults to: [])

    Array of message objects to display

  • current_user_id (Any) (defaults to: nil)

    ID of the current user to determine message positions

Options Hash (messages:):

  • :text (String)

    The message text

  • :user_id (Any)

    ID of the user who sent the message

  • :position (Symbol)

    Optional override for the position (:start or :end)

  • :color (Symbol)

    The color of the message bubble

  • :avatar (Hash)

    Avatar options

  • :header (Hash)

    Header options

  • :footer (Hash)

    Footer options



46
47
48
49
50
51
52
53
# File 'app/components/daisy_ui/data_display/chat.rb', line 46

def initialize(messages: [], current_user_id: nil, **system_arguments)
  @messages = messages
  @current_user_id = current_user_id
  super(**system_arguments)

  # Pre-populate bubbles from messages array if provided
  @messages.each { |message| add_bubble(message) }
end

Instance Method Details

#callObject



55
56
57
58
59
60
61
# File 'app/components/daisy_ui/data_display/chat.rb', line 55

def call
  return unless bubbles.any?

  (:div, class: 'w-full') do
    safe_join(bubbles)
  end
end