summaryrefslogtreecommitdiff
path: root/app/helpers/badges_helper.rb
blob: 26ebe8a6470aa6cfc0a9bc4d9de82d39e632d57c (plain)
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

module BadgesHelper
  VARIANT_CLASSES = {
    muted: "badge-muted",
    neutral: "badge-neutral",
    info: "badge-info",
    success: "badge-success",
    warning: "badge-warning",
    danger: "badge-danger"
  }.tap { |hash| hash.default = hash.fetch(:muted) } .freeze

  SIZE_CLASSES = {
    sm: "sm",
    md: "md",
    lg: "lg"
  }.tap { |hash| hash.default = hash.fetch(:md) } .freeze

  GL_BADGE_CLASSES = %w[gl-badge badge badge-pill].freeze

  GL_ICON_CLASSES = %w[gl-icon gl-badge-icon].freeze

  # Creates a GitLab UI badge.
  #
  # Examples:
  #   # Plain text badge
  #   gl_badge_tag("foo")
  #
  #   # Danger variant
  #   gl_badge_tag("foo", variant: :danger)
  #
  #   # Small size
  #   gl_badge_tag("foo", size: :sm)
  #
  #   # With icon
  #   gl_badge_tag("foo", icon: "question-o")
  #
  #   # Icon-only
  #   gl_badge_tag("foo", icon: "question-o", icon_only: true)
  #
  #   # Badge link
  #   gl_badge_tag("foo", nil, href: some_path)
  #
  #   # Custom classes
  #   gl_badge_tag("foo", nil, class: "foo-bar")
  #
  #   # Block content
  #   gl_badge_tag({ variant: :danger }, { class: "foo-bar" }) do
  #     "foo"
  #   end
  #
  # For accessibility, ensure that the given text or block is non-empty.
  #
  # See also https://gitlab-org.gitlab.io/gitlab-ui/?path=/story/base-badge--default.
  def gl_badge_tag(*args, &block)
    if block_given?
      build_gl_badge_tag(capture(&block), *args)
    else
      build_gl_badge_tag(*args)
    end
  end

  private

  def build_gl_badge_tag(content, options = nil, html_options = nil)
    options ||= {}
    html_options ||= {}

    icon_only = options[:icon_only]
    variant_class = VARIANT_CLASSES[options.fetch(:variant, :muted)]
    size_class = SIZE_CLASSES[options.fetch(:size, :md)]
    icon_classes = GL_ICON_CLASSES.dup << options.fetch(:icon_classes, nil)

    html_options = html_options.merge(
      class: [
        *GL_BADGE_CLASSES,
        variant_class,
        size_class,
        *html_options[:class]
      ]
    )

    if icon_only
      html_options['aria-label'] = content
      html_options['role'] = 'img'
    end

    if options[:icon]
      icon_classes << "gl-mr-2" unless icon_only
      icon = sprite_icon(options[:icon], css_class: icon_classes.join(' '))

      content = icon_only ? icon : icon + content
    end

    tag = html_options[:href].nil? ? :span : :a

    content_tag(tag, content, html_options)
  end
end