Skip to content

Commit

Permalink
Move Primer::CounterComponent to Primer::Beta::Counter (#1279)
Browse files Browse the repository at this point in the history
* search for component file, and exit with error info if zero or more than one are found

* added changeset

* initial move of Primer::Counter to Primer::Beta::Counter

* added changeset

* removed file that jumped in from merge conflict resolution

* removing component_status_migrator changes that i didn't mean to put in this PR
  • Loading branch information
mxriverlynn authored and jonrohan committed Aug 8, 2022
1 parent fe70901 commit 7da8c96
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 197 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-dryers-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

moving Primer::Counter to Primer::Beta::Counter, and replace original with a deprecated version"
4 changes: 2 additions & 2 deletions app/components/primer/beta/auto_complete/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Item < Primer::Component

# The trailing visual rendered after the link.
#
# @param kwargs [Hash] The arguments accepted by <%= link_to_component(Primer::OcticonComponent) %>, <%= link_to_component(Primer::LabelComponent) %>, or <%= link_to_component(Primer::CounterComponent) %>
# @param kwargs [Hash] The arguments accepted by <%= link_to_component(Primer::OcticonComponent) %>, <%= link_to_component(Primer::LabelComponent) %>, or <%= link_to_component(Primer::Beta::Counter) %>
renders_one :trailing_visual, types: {
icon: Primer::OcticonComponent,
label: Primer::LabelComponent,
counter: Primer::CounterComponent
counter: Primer::Beta::Counter
}

# Optional description
Expand Down
113 changes: 113 additions & 0 deletions app/components/primer/beta/counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

module Primer
module Beta
# Use `Counter` to add a count to navigational elements and buttons.
#
# @accessibility
# Always use `Counter` with adjacent text that provides supplementary information regarding what the count is for. For instance, `Counter`
# should be accompanied with text such as `issues` or `pull requests`.
#
class Counter < Primer::Component
status :beta

DEFAULT_SCHEME = :default
SCHEME_MAPPINGS = {
DEFAULT_SCHEME => "",
:primary => "Counter--primary",
:secondary => "Counter--secondary",
# deprecated
:gray => "Counter--primary",
:light_gray => "Counter--secondary"
}.freeze
DEPRECATED_SCHEME_OPTIONS = [:gray, :light_gray].freeze
SCHEME_OPTIONS = (SCHEME_MAPPINGS.keys - DEPRECATED_SCHEME_OPTIONS).freeze

#
# @example Default
# <%= render(Primer::Beta::Counter.new(count: 25)) %>
#
# @example Schemes
# <%= render(Primer::Beta::Counter.new(count: 25, scheme: :primary)) %>
# <%= render(Primer::Beta::Counter.new(count: 25, scheme: :secondary)) %>
#
# @param count [Integer, Float::INFINITY, nil] The number to be displayed (e.x. # of issues, pull requests)
# @param scheme [Symbol] Color scheme. <%= one_of(Primer::Beta::Counter::SCHEME_OPTIONS) %>
# @param 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+")
# @param hide_if_zero [Boolean] If true, a `hidden` attribute is added to the counter if `count` is zero.
# @param text [String] Text to display instead of count.
# @param round [Boolean] Whether to apply our standard rounding logic to value.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
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 # rubocop:disable Style/NumericPredicate
end

def call
render(Primer::BaseComponent.new(**@system_arguments)) { value }
end

private

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
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
end
end
end
6 changes: 3 additions & 3 deletions app/components/primer/button_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class ButtonComponent < Primer::Component
#
# Use:
#
# - `trailing_visual_counter` for a <%= link_to_component(Primer::CounterComponent) %>.
# - `trailing_visual_counter` for a <%= link_to_component(Primer::Beta::Counter) %>.
#
# @param system_arguments [Hash] Same arguments as <%= link_to_component(Primer::CounterComponent) %>.
# @param system_arguments [Hash] Same arguments as <%= link_to_component(Primer::Beta::Counter) %>.
renders_one :trailing_visual, types: {
counter: lambda { |**system_arguments|
system_arguments[:ml] = 2

Primer::CounterComponent.new(**system_arguments)
Primer::Beta::Counter.new(**system_arguments)
}
}
alias counter trailing_visual_counter # remove alias when all buttons are migrated to new slot names
Expand Down
108 changes: 2 additions & 106 deletions app/components/primer/counter_component.rb
Original file line number Diff line number Diff line change
@@ -1,111 +1,7 @@
# frozen_string_literal: true

module Primer
# Use `Counter` to add a count to navigational elements and buttons.
#
# @accessibility
# Always use `Counter` with adjacent text that provides supplementary information regarding what the count is for. For instance, `Counter`
# should be accompanied with text such as `issues` or `pull requests`.
#
class CounterComponent < Primer::Component
status :beta

DEFAULT_SCHEME = :default
SCHEME_MAPPINGS = {
DEFAULT_SCHEME => "",
:primary => "Counter--primary",
:secondary => "Counter--secondary",
# deprecated
:gray => "Counter--primary",
:light_gray => "Counter--secondary"
}.freeze
DEPRECATED_SCHEME_OPTIONS = [:gray, :light_gray].freeze
SCHEME_OPTIONS = (SCHEME_MAPPINGS.keys - DEPRECATED_SCHEME_OPTIONS).freeze

#
# @example Default
# <%= render(Primer::CounterComponent.new(count: 25)) %>
#
# @example Schemes
# <%= render(Primer::CounterComponent.new(count: 25, scheme: :primary)) %>
# <%= render(Primer::CounterComponent.new(count: 25, scheme: :secondary)) %>
#
# @param count [Integer, Float::INFINITY, nil] The number to be displayed (e.x. # of issues, pull requests)
# @param scheme [Symbol] Color scheme. <%= one_of(Primer::CounterComponent::SCHEME_OPTIONS) %>
# @param 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+")
# @param hide_if_zero [Boolean] If true, a `hidden` attribute is added to the counter if `count` is zero.
# @param text [String] Text to display instead of count.
# @param round [Boolean] Whether to apply our standard rounding logic to value.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
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 # rubocop:disable Style/NumericPredicate
end

def call
render(Primer::BaseComponent.new(**@system_arguments)) { value }
end

private

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
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
class CounterComponent < Primer::Beta::Counter
status :deprecated
end
end
2 changes: 1 addition & 1 deletion app/components/primer/menu_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MenuComponent < Primer::Component
# <% c.item(href: "#url") do %>
# <%= render(Primer::OcticonComponent.new("check")) %>
# With Icon and Counter
# <%= render(Primer::CounterComponent.new(count: 25)) %>
# <%= render(Primer::Beta::Counter.new(count: 25)) %>
# <% end %>
# <% end %>
#
Expand Down
4 changes: 2 additions & 2 deletions app/components/primer/navigation/tab_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class TabComponent < Primer::Component

# Counter to be rendered in the Tab right.
#
# @param kwargs [Hash] The same arguments as <%= link_to_component(Primer::CounterComponent) %>.
renders_one :counter, Primer::CounterComponent
# @param kwargs [Hash] The same arguments as <%= link_to_component(Primer::Beta::Counter) %>.
renders_one :counter, Primer::Beta::Counter

attr_reader :selected

Expand Down
2 changes: 1 addition & 1 deletion docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Primer ViewComponents is an implementation of the Primer Design System, using [V
Render Primer ViewComponents from templates:

```erb
<%= render(Primer::CounterComponent.new(count: 25)) %>
<%= render(Primer::Beta::Counter.new(count: 25)) %>
```

## Installation
Expand Down
2 changes: 1 addition & 1 deletion docs/content/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ stable status.
|------------------|---------------|
| [`State`](https://primer.style/css/components/labels#states) | [`Primer::StateComponent`](https://primer.style/view-components/components/state) |
| [`breadcrumb-item`](https://primer.style/css/components/breadcrumb) | [`Primer::Beta::Breadcrumbs`](https://primer.style/view-components/components/beta/breadcrumbs) |
| [`Counter`](https://primer.style/css/stickersheet/labels#counters) | [`Primer::CounterComponent`](https://primer.style/view-components/components/counter) |
| [`Counter`](https://primer.style/css/stickersheet/labels#counters) | [`Primer::Beta::Counter`](https://primer.style/view-components/components/counter) |
| [`Subhead`](https://primer.style/css/components/subhead) | [`Primer::SubheadComponent`](https://primer.style/view-components/components/subhead) |
| [`blankslate`](https://primer.style/css/components/blankslate) | [`Primer::Beta::Blankslate`](https://primer.style/view-components/components/beta/blankslate) |
2 changes: 1 addition & 1 deletion docs/src/@primer/gatsby-theme-doctocat/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
- title: CloseButton
url: "/components/beta/closebutton"
- title: Counter
url: "/components/counter"
url: "/components/beta/counter"
- title: Details
url: "/components/details"
- title: Dropdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module DeprecatedComponentsHelpers
COMPONENT_TO_USE_INSTEAD = {
"Primer::ButtonGroup" => "Primer::Beta::ButtonGroup",
"Primer::CloseButton" => "Primer::Beta::CloseButton",
"Primer::CounterComponent" => "Primer::Beta::Counter",
"Primer::Alpha::AutoComplete::Item" => "Primer::Beta::AutoComplete::Item",
"Primer::Alpha::AutoComplete" => "Primer::Beta::AutoComplete",
"Primer::BlankslateComponent" => "Primer::Beta::Blankslate",
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/primer/component_name_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ComponentNameMigration < BaseCop
"Primer::BoxComponent" => "Primer::Box",
"Primer::ButtonGroup" => "Primer::Beta::ButtonGroup",
"Primer::CloseButton" => "Primer::Beta::CloseButton",
"Primer::CounterComponent" => "Primer::Beta::Counter",
"Primer::BlankslateComponent" => "Primer::Beta::Blankslate",
"Primer::BorderBoxComponent" => "Primer::Beta::BorderBox",
"Primer::BaseButton" => "Primer::Beta::BaseButton",
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/docs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace :docs do
Primer::Alpha::ButtonMarketing,
Primer::ClipboardCopy,
Primer::Beta::CloseButton,
Primer::CounterComponent,
Primer::Beta::Counter,
Primer::DetailsComponent,
Primer::Dropdown,
Primer::DropdownMenuComponent,
Expand Down
Loading

0 comments on commit 7da8c96

Please sign in to comment.