class Primer::Beta::Counter
should be accompanied with text such as ‘issues` or `pull requests`.
Always use `Counter` with adjacent text that provides supplementary information regarding what the count is for. For instance, `Counter`
@accessibility
Use `Counter` to add a count to navigational elements and buttons.
def call
def call render(Primer::BaseComponent.new(**@system_arguments)) { value } end
def initialize(
-
system_arguments
(Hash
) -- <%= link_to_system_arguments_docs %> -
round
(Boolean
) -- Whether to apply our standard rounding logic to value. -
text
(String
) -- Text to display instead of count. -
hide_if_zero
(Boolean
) -- If true, a `hidden` attribute is added to the counter if `count` is zero. -
limit
(Integer, nil
) -- Maximum value to display. Pass `nil` for no limit. (e.x. if `count` == 6,000 and `limit` == 5000, counter will display "5,000+") -
scheme
(Symbol
) -- Color scheme. <%= one_of(Primer::Beta::Counter::SCHEME_OPTIONS) %> -
count
(Integer, Float::INFINITY, nil
) -- The number to be displayed (e.x. # of issues, pull requests)
def initialize( count: 0, scheme: DEFAULT_SCHEME, limit: 5_000, hide_if_zero: false, text: "", round: false, **system_arguments ) @count = count @limit = limit @hide_if_zero = hide_if_zero @text = text @round = round @system_arguments = deny_tag_argument(**system_arguments) @has_limit = !@limit.nil? @system_arguments[:title] = title @system_arguments[:tag] = :span @system_arguments[:classes] = class_names( "Counter", @system_arguments[:classes], SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME, deprecated_values: DEPRECATED_SCHEME_OPTIONS)] ) @system_arguments[:hidden] = true if count == 0 && hide_if_zero end
def title
def title if @text.present? @text elsif @count.nil? "Not available" elsif @count == Float::INFINITY "Infinity" else count = @count.to_i str = number_with_delimiter(@has_limit ? [count, @limit].min : count) str += "+" if @has_limit && count > @limit str end end
def value
def value if @text.present? @text elsif @count.nil? "" # CSS will hide it elsif @count == Float::INFINITY "∞" else if @round count = @has_limit ? [@count.to_i, @limit].min : @count.to_i precision = count.between?(100_000, 999_999) ? 0 : 1 units = { thousand: "k", million: "m", billion: "b" } str = number_to_human(count, precision: precision, significant: false, units: units, format: "%n%u") else @count = @count.to_i str = number_with_delimiter(@has_limit ? [@count, @limit].min : @count) end str += "+" if @has_limit && @count.to_i > @limit str end end