2021-11-23 10:11:19 -05:00
|
|
|
# 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)
|
2022-09-01 08:11:56 -04:00
|
|
|
# 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)
|
|
|
|
|
2022-08-03 08:11:38 -04:00
|
|
|
if block
|
2022-09-01 08:11:56 -04:00
|
|
|
render Pajamas::BadgeComponent.new(**options_hash), &block
|
2021-11-23 10:11:19 -05:00
|
|
|
else
|
2022-09-01 08:11:56 -04:00
|
|
|
render Pajamas::BadgeComponent.new(*params, **options_hash)
|
2021-11-23 10:11:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|