Class: Ariadne::UI::Avatar::Component

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/ariadne/ui/avatar/component.rb

Overview

A component that can represent a user or entity with an image or initials.

Constant Summary collapse

DEFAULT_SHAPE =
:circle
SHAPE_OPTIONS =
[DEFAULT_SHAPE, :square].freeze
DEFAULT_BADGE_POSITION =
:bottom_right
BADGE_POSITIONS =
[:top_right, :top_left, :bottom_right, :bottom_left].freeze

Constants inherited from BaseComponent

BaseComponent::ACCEPT_ANYTHING

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

audited_at, #class_for, #component, component_id, #component_id, component_name, generate_id, #html_attributes, i18n_scope, #merge_data_attributes, #merge_tailwind_classes, #options, stimulus_name, translate, #validate_aria_label!

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes, #prepend_action, #prepend_controller, #prepend_data_attribute

Methods included from ViewComponent::StyleVariants

#merged_styles

Instance Attribute Details

#placeholder_textObject (readonly)

Returns the value of attribute placeholder_text.



51
52
53
# File 'app/components/ariadne/ui/avatar/component.rb', line 51

def placeholder_text
  @placeholder_text
end

Instance Method Details

#badge_classesObject

Returns the merged badge classes



165
166
167
# File 'app/components/ariadne/ui/avatar/component.rb', line 165

def badge_classes
  merge_tailwind_classes([badge_position_classes, badge_style_classes])
end

#badge_position_classesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/components/ariadne/ui/avatar/component.rb', line 131

def badge_position_classes
  position_classes = {
    top_right: "ariadne:-top-1 ariadne:-right-1",
    top_left: "ariadne:-top-1 ariadne:-left-1",
    bottom_right: "ariadne:-bottom-1 ariadne:-right-1",
    bottom_left: "ariadne:-bottom-1 ariadne:-left-1",
  }

  # Apply size-specific adjustments with class-based approach for badge sizing
  size_classes = case size
  when :xs, :sm
    "ariadne:h-3 ariadne:w-3 ariadne:text-[8px]"
  when :md
    "ariadne:h-4 ariadne:w-4 ariadne:text-[10px]"
  when :lg, :xl
    "ariadne:h-5 ariadne:w-5 ariadne:text-xs"
  else
    "ariadne:h-4 ariadne:w-4 ariadne:text-[10px]"
  end

  # Add border to make badge stand out against avatar
  "#{position_classes[badge_position]} #{size_classes} ariadne:ring-1 ariadne:ring-white ariadne:dark:ring-gray-800 ariadne:rounded-full ariadne:flex ariadne:items-center ariadne:justify-center ariadne:overflow-hidden"
end

#badge_style_classesObject



155
156
157
158
159
160
161
162
# File 'app/components/ariadne/ui/avatar/component.rb', line 155

def badge_style_classes
  [
    "ariadne:absolute",
    "ariadne:bg-white",
    "ariadne:p-0.5",
    "ariadne:leading-[0]",
  ]
end

#before_renderObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/components/ariadne/ui/avatar/component.rb', line 111

def before_render
  validate!

  # must be done here rather than within an `accepts_html_attributes` block
  # because it's dependewnt on `with_badge_*` slot's existence, which isn't known until now
  html_attrs[:class] = merge_tailwind_classes([style(size:, shape:, has_src: src.present?, has_badge: badge), html_attrs[:class]].join(" "))

  # One char long or two
  len = [:xs, :sm, :md].include?(size) ? 0 : 1
  @placeholder_text = (text || "").strip.split[0..len].map { |word| word.capitalize[0] }.join

  html_attrs[:aria] = merge_aria(
    html_attrs, {
      aria: {
        label: text || alt,
      },
    }
  )
end