Class: Flowbite::Avatar::Dot

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/flowbite/avatar/dot.rb

Overview

Renders a status dot indicator positioned on top of an avatar.

Usually added through the top_dot or bottom_dot slots of Flowbite::Avatar, which set the position and size. When rendered on its own, the dot must be placed inside a relatively positioned container together with the avatar.

Examples:

Through an Avatar slot

<%= render(Flowbite::Avatar.new(src: user.avatar_url)) do |avatar| %>
  <% avatar.with_top_dot(label: "Online", style: :success) %>
<% end %>

Standalone

<div class="relative">
  <%= render(Flowbite::Avatar.new(src: user.avatar_url)) %>
  <%= render(Flowbite::Avatar::Dot.new(label: "Online", position: :bottom, style: :success)) %>
</div>

See Also:

Constant Summary collapse

POSITIONS =
{
  bottom: ["bottom-0"],
  top: ["top-0"]
}.freeze
SIZES =

Horizontal offsets placing the dot at the right edge of an avatar of the given size.

{
  xs: ["left-1.5"],
  sm: ["left-3"],
  base: ["left-5"],
  default: ["left-7"],
  lg: ["left-8"],
  xl: ["left-11"],
  "2xl": ["left-13"]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, position: :top, size: :default, style: :success) ⇒ Dot

Returns a new instance of Dot.

Parameters:

  • label (String) (defaults to: nil) —

    Text describing the status for screen readers, since the dot otherwise only conveys it through color.

  • position (Symbol) (defaults to: :top) —

    Where to place the dot (:bottom, :top).

  • size (Symbol) (defaults to: :default) —

    The size of the avatar the dot is placed on.

  • style (Symbol) (defaults to: :success) —

    The color of the dot (:danger, :success).



71
72
73
74
75
76
# File 'app/components/flowbite/avatar/dot.rb', line 71

def initialize(label: nil, position: :top, size: :default, style: :success)
  @label = label
  @position = position
  @size = size
  @style = style
end

Instance Attribute Details

#label ⇒ Object (readonly)

Returns the value of attribute label.



64
65
66
# File 'app/components/flowbite/avatar/dot.rb', line 64

def label
  @label
end

#position ⇒ Object (readonly)

Returns the value of attribute position.



64
65
66
# File 'app/components/flowbite/avatar/dot.rb', line 64

def position
  @position
end

#size ⇒ Object (readonly)

Returns the value of attribute size.



64
65
66
# File 'app/components/flowbite/avatar/dot.rb', line 64

def size
  @size
end

#style ⇒ Object (readonly)

Returns the value of attribute style.



64
65
66
# File 'app/components/flowbite/avatar/dot.rb', line 64

def style
  @style
end

Class Method Details

.classes(position: :top, size: :default, style: :success) ⇒ Object



43
44
45
46
47
48
# File 'app/components/flowbite/avatar/dot.rb', line 43

def classes(position: :top, size: :default, style: :success)
  POSITIONS.fetch(position) + sizes.fetch(size) +
    ["absolute", "w-3.5", "h-3.5"] +
    styles.fetch(style).fetch(:default) +
    ["border-2", "border-buffer", "rounded-full"]
end

.sizes ⇒ Object



50
51
52
# File 'app/components/flowbite/avatar/dot.rb', line 50

def sizes
  SIZES
end

.styles ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/components/flowbite/avatar/dot.rb', line 54

def styles
  Flowbite::Styles.from_hash(
    {
      danger: {default: ["bg-danger"]},
      success: {default: ["bg-success"]}
    }.freeze
  )
end

Instance Method Details

#call ⇒ Object



78
79
80
81
82
# File 'app/components/flowbite/avatar/dot.rb', line 78

def call
  (:span, class: self.class.classes(position: position, size: size, style: style)) do
    (:span, label, class: "sr-only") if label.present?
  end
end