Class: DaisyUI::ChatBubble

Inherits:
BaseComponent show all
Defined in:
app/components/daisy_ui/data_display/chat_bubble.rb,
app/components/daisy_ui/data_display/chat_bubble/metadata.rb

Overview

Chat bubble component implementing DaisyUI’s chat bubble styles

Examples:

Basic usage

<%= render(ChatBubbleComponent.new(text: "Hello!")) %>

With avatar

<%= render(ChatBubbleComponent.new(text: "Hello!")) do |c|
  <% c.with_avatar(img_src: "user.jpg", img_alt: "User") %>
<% end %>

With header and footer

<%= render(ChatBubbleComponent.new(text: "Hello!")) do |c|
  <% c.with_header(text: "John Doe", time: "12:45") %>
  <% c.with_footer(text: "Delivered", time: "12:46") %>
<% end %>

End position

<%= render(ChatBubbleComponent.new(text: "Hello!", position: :end)) %>

With color

<%= render(ChatBubbleComponent.new(text: "Hello!", color: :primary)) %>

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

COLORS =

Available chat bubble colors from DaisyUI

{
  primary: 'chat-bubble-primary',
  secondary: 'chat-bubble-secondary',
  accent: 'chat-bubble-accent',
  neutral: 'chat-bubble-neutral',
  info: 'chat-bubble-info',
  success: 'chat-bubble-success',
  warning: 'chat-bubble-warning',
  error: 'chat-bubble-error'
}.freeze
POSITIONS =

Available positions

{
  start: 'chat-start',
  end: 'chat-end'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, position: :start, color: nil, text_class: nil, header_class: nil, footer_class: nil, avatar: {}, header: {}, footer: {}, **system_arguments) ⇒ ChatBubble

Returns a new instance of ChatBubble.

Parameters:

  • text (String) (defaults to: nil)

    The text content to display inside the chat bubble

  • position (Symbol) (defaults to: :start)

    Position of the chat bubble (:start, :end)

  • color (Symbol) (defaults to: nil)

    Color variant of the chat bubble

  • avatar (Hash) (defaults to: {})

    Options for the avatar component

  • header (Hash) (defaults to: {})

    Options for the header metadata component

  • footer (Hash) (defaults to: {})

    Options for the footer metadata component



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/components/daisy_ui/data_display/chat_bubble.rb', line 71

def initialize(text = nil, position: :start, color: nil,
               text_class: nil, header_class: nil,
               footer_class: nil,
               avatar: {},
               header: {},
               footer: {},
               **system_arguments)
  @text = text
  @position = build_argument(position, POSITIONS, 'position')
  @color = build_argument(color, COLORS, 'color')
  @text_class = text_class
  @header_class = header_class
  @footer_class = footer_class
  @avatar_options = avatar
  @header_options = header
  @footer_options = footer
  @content_text = text
  super(**system_arguments)

  # Setup slots from provided options
  with_avatar(**@avatar_options) if @avatar_options&.any?
  with_header(**@header_options) if @header_options&.any?
  with_footer(**@footer_options) if @footer_options&.any?
end

Instance Method Details

#callObject



105
106
107
108
109
110
111
112
113
114
# File 'app/components/daisy_ui/data_display/chat_bubble.rb', line 105

def call
  tag.div(**html_attributes) do
    components = []
    components << avatar if avatar?
    components << header if header?
    components << render_bubble if @content_text.present?
    components << footer if footer?
    safe_join(components)
  end
end

#with_text(content = nil) ⇒ Object

Set the text content



97
98
99
100
101
102
103
# File 'app/components/daisy_ui/data_display/chat_bubble.rb', line 97

def with_text(content = nil, &)
  @content_text = if block_given?
                    capture(&)
                  else
                    content
                  end
end