Module: Iron::AvatarHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/iron/avatar_helper.rb

Instance Method Summary collapse

Instance Method Details

#avatar(user, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/iron/avatar_helper.rb', line 3

def avatar(user, options = {})
  variant = options.delete(:variant) || "circle"
  alt = options.delete(:alt) || ""
  css_class = tw(
    options.delete(:class),
    # Basic layout
    "inline-grid shrink-0 align-middle [--avatar-radius:20%] [--ring-opacity:20%] *:col-start-1 *:row-start-1",
    "outline outline-1 -outline-offset-1 outline-black/(--ring-opacity) dark:outline-white/(--ring-opacity)",
    # Add the correct border radius
    variant == "square" ? "rounded-(--avatar-radius) *:rounded-(--avatar-radius)" : "rounded-full *:rounded-full"
  )

  tag.span class: css_class, data: { slot: "avatar" } do
    tag.svg class: "size-full select-none fill-current p-[5%] text-[48px] font-medium uppercase",
      viewbox: "0 0 100 100",
      "aria-hidden": alt.present? ? nil : true do
      safe_join([
        alt.present? ? tag.title(alt) : nil,
        tag.text(
          user.initials,
          x: "50%",
          y: "50%",
          "alignment-baseline": "middle",
          "dominant-baseline": "middle",
          "text-anchor": "middle",
          dy: ".125em",
        )
      ])
    end
  end
end