debian-mirror-gitlab/app/helpers/badges_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.3 KiB
Ruby
Raw Normal View History

2022-01-26 12:08:38 +05:30
# 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-10-11 01:57:18 +05:30
# 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-27 11:52:29 +05:30
if block
2022-10-11 01:57:18 +05:30
render Pajamas::BadgeComponent.new(**options_hash), &block
2022-01-26 12:08:38 +05:30
else
2022-10-11 01:57:18 +05:30
render Pajamas::BadgeComponent.new(*params, **options_hash)
2022-01-26 12:08:38 +05:30
end
end
end