summaryrefslogtreecommitdiff
path: root/app/helpers/badges_helper.rb
blob: 069c15433a5a4ee5afb4f5eae6d1133cd7c044f4 (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
# frozen_string_literal: true

module BadgesHelper
  # 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)
    # Merge the options and html_options hashes if both are present,
    # because the badge component wants a flat list of keyword args.
    args.compact!
    hashes, params = args.partition { |a| a.is_a? Hash }
    options_hash = hashes.reduce({}, :merge)

    if block
      render Pajamas::BadgeComponent.new(**options_hash), &block
    else
      render Pajamas::BadgeComponent.new(*params, **options_hash)
    end
  end
end