Skip to content

Commit

Permalink
Move Primer::OcticonSymbolComponent to `Primer::Alpha::OcticonSymbo…
Browse files Browse the repository at this point in the history
…ls` (#1696)
  • Loading branch information
mxriverlynn authored Dec 9, 2022
1 parent 4ff75a5 commit d709bae
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-crabs-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Moving `Primer::OcticonSymbolsComponent` to `Primer::Alpha::OcticonSymbols`, and deprecating the original
61 changes: 61 additions & 0 deletions app/components/primer/alpha/octicon_symbols.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "octicons"

module Primer
module Alpha
# OcticonSymbols renders a symbol dictionary using a list of <%= link_to_octicons %>.
class OcticonSymbols < Primer::Component
warn_on_deprecated_slot_setter

# @example Symbol dictionary
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, color: :success)) %>
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, color: :danger)) %>
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, size: :medium)) %>
# <%= render(Primer::Alpha::OcticonSymbols.new(icons: [{ symbol: :check }, { symbol: :check, size: :medium }])) %>
#
# @param icons [Array<Hash>] List of icons to render, in the format { symbol: :icon_name, size: :small }
def initialize(icons: [])
@icons = {}
icons.each do |icon|
symbol = icon[:symbol]
size = Primer::Beta::Octicon::SIZE_MAPPINGS[
fetch_or_fallback(Primer::Beta::Octicon::SIZE_OPTIONS, icon[:size] || Primer::Beta::Octicon::SIZE_DEFAULT, Primer::Beta::Octicon::SIZE_DEFAULT)
]

cache_key = Primer::Octicon::Cache.get_key(symbol: symbol, size: size)

if (cache_icon = Primer::Octicon::Cache.read(cache_key))
icon_instance = cache_icon
else
icon_instance = Octicons::Octicon.new(symbol, height: size)

Primer::Octicon::Cache.set(cache_key, icon_instance)
end

# Don't put the same icon twice
@icons[[symbol, icon_instance.height]] = icon_instance if @icons[[symbol, icon_instance.height]].nil?
end
end

def render?
@icons.any?
end

def symbol_tags
safe_join(
@icons.values.map do |icon|
content_tag(
:symbol,
icon.path.html_safe, # rubocop:disable Rails/OutputSafety
id: "octicon_#{icon.symbol}_#{icon.height}",
viewBox: icon.options[:viewBox],
width: icon.width,
height: icon.height
)
end
)
end
end
end
end
2 changes: 1 addition & 1 deletion app/components/primer/beta/octicon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Octicon < Primer::Component
# @param icon_name [Symbol, String] Name of <%= link_to_octicons %> to use.
# @param icon [Symbol, String] Name of <%= link_to_octicons %> to use.
# @param size [Symbol] <%= one_of(Primer::Beta::Octicon::SIZE_MAPPINGS, sort: false) %>
# @param use_symbol [Boolean] EXPERIMENTAL (May change or be removed) - Set to true when using with <%= link_to_component(Primer::OcticonSymbolsComponent) %>.
# @param use_symbol [Boolean] EXPERIMENTAL (May change or be removed) - Set to true when using with <%= link_to_component(Primer::Alpha::OcticonSymbols) %>.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(icon_name = nil, icon: nil, size: SIZE_DEFAULT, use_symbol: false, **system_arguments)
icon_key = icon_name || icon
Expand Down
56 changes: 2 additions & 54 deletions app/components/primer/octicon_symbols_component.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,7 @@
# frozen_string_literal: true

require "octicons"

module Primer
# OcticonSymbols renders a symbol dictionary using a list of <%= link_to_octicons %>.
class OcticonSymbolsComponent < Primer::Component
warn_on_deprecated_slot_setter

# @example Symbol dictionary
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, color: :success)) %>
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, color: :danger)) %>
# <%= render(Primer::Beta::Octicon.new(icon: :check, use_symbol: true, size: :medium)) %>
# <%= render(Primer::OcticonSymbolsComponent.new(icons: [{ symbol: :check }, { symbol: :check, size: :medium }])) %>
#
# @param icons [Array<Hash>] List of icons to render, in the format { symbol: :icon_name, size: :small }
def initialize(icons: [])
@icons = {}
icons.each do |icon|
symbol = icon[:symbol]
size = Primer::Beta::Octicon::SIZE_MAPPINGS[
fetch_or_fallback(Primer::Beta::Octicon::SIZE_OPTIONS, icon[:size] || Primer::Beta::Octicon::SIZE_DEFAULT, Primer::Beta::Octicon::SIZE_DEFAULT)
]

cache_key = Primer::Octicon::Cache.get_key(symbol: symbol, size: size)

if (cache_icon = Primer::Octicon::Cache.read(cache_key))
icon_instance = cache_icon
else
icon_instance = Octicons::Octicon.new(symbol, height: size)

Primer::Octicon::Cache.set(cache_key, icon_instance)
end

# Don't put the same icon twice
@icons[[symbol, icon_instance.height]] = icon_instance if @icons[[symbol, icon_instance.height]].nil?
end
end

def render?
@icons.any?
end

def symbol_tags
safe_join(
@icons.values.map do |icon|
content_tag(
:symbol,
icon.path.html_safe, # rubocop:disable Rails/OutputSafety
id: "octicon_#{icon.symbol}_#{icon.height}",
viewBox: icon.options[:viewBox],
width: icon.width,
height: icon.height
)
end
)
end
class OcticonSymbolsComponent < Primer::Alpha::OcticonSymbols
status :deprecated
end
end
13 changes: 13 additions & 0 deletions component_status_migrator.thor
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ComponentStatusMigrator < Thor::Group
end

def move_template
return nil unless File.exist?(template_path)

move_file("template", template_path, template_path_with_status)
end

Expand All @@ -47,10 +49,14 @@ class ComponentStatusMigrator < Thor::Group
end

def move_test
return nil unless File.exist?(test_path)

move_file("test", test_path, test_path_with_status)
end

def move_preview
return nil unless File.exist?(preview_path)

move_file("preview", preview_path, preview_path_with_status)
end

Expand All @@ -63,6 +69,7 @@ class ComponentStatusMigrator < Thor::Group

def add_module_to_preview
return if stable?
return nil unless File.exist?(preview_path_with_status)

insert_into_file(preview_path_with_status, " module #{class_status}\n", after: "module Primer\n")
insert_into_file(preview_path_with_status, " end\n", before: /^end$/, force: true)
Expand All @@ -77,6 +84,8 @@ class ComponentStatusMigrator < Thor::Group
end

def remove_suffix_from_preview_class
return nil unless File.exist?(preview_path)

if preview_path.include?("_component") && !name.include?("Component") # rubocop:disable Rails/NegateInclude
# if the class name does not include 'Component', but the file name does include '_component',
# this line will correct it by removing the incosistency with the word 'Component'
Expand All @@ -89,10 +98,14 @@ class ComponentStatusMigrator < Thor::Group
end

def rename_preview_label
return nil unless File.exist?(preview_path_with_status)

gsub_file(preview_path_with_status, /# @label #{name}/, "# @label #{name_without_suffix}")
end

def rename_test_class
return nil unless File.exist?(test_path_with_status)

gsub_file(test_path_with_status, /class .*Test </, "class Primer#{class_status}#{name_without_suffix.gsub('::', '')}Test <")
end

Expand Down
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 @@ -78,7 +78,7 @@
- title: Octicon
url: "/components/beta/octicon"
- title: OcticonSymbols
url: "/components/octiconsymbols"
url: "/components/alpha/octiconsymbols"
- title: Popover
url: "/components/beta/popover"
- title: ProgressBar
Expand Down
4 changes: 4 additions & 0 deletions lib/primer/deprecations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ deprecations:
autocorrect: true
replacement: "Primer::Beta::Octicon"

- component: "Primer::OcticonSymbolsComponent"
autocorrect: true
replacement: "Primer::Alpha::OcticonSymbols"

- component: "Primer::PopoverComponent"
autocorrect: true
replacement: "Primer::Beta::Popover"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/docs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace :docs do
Primer::HellipButton,
Primer::Alpha::Image,
Primer::LocalTime,
Primer::OcticonSymbolsComponent,
Primer::Alpha::OcticonSymbols,
Primer::Alpha::ImageCrop,
Primer::IconButton,
Primer::Beta::AutoComplete,
Expand Down
30 changes: 15 additions & 15 deletions static/arguments.json
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,20 @@
}
]
},
{
"component": "OcticonSymbols",
"status": "alpha",
"source": "https://github.com/primer/view_components/tree/main/app/components/primer/alpha/octicon_symbols.rb",
"lookbook": "https://primer.style/view-components/lookbook/inspect/primer/alpha/octicon_symbols_preview/default/",
"parameters": [
{
"name": "icons",
"type": "Array<Hash>",
"default": "`[]`",
"description": "List of icons to render, in the format { symbol: :icon_name, size: :small }"
}
]
},
{
"component": "SegmentedControl",
"status": "alpha",
Expand Down Expand Up @@ -1803,7 +1817,7 @@
"name": "use_symbol",
"type": "Boolean",
"default": "`false`",
"description": "EXPERIMENTAL (May change or be removed) - Set to true when using with [OcticonSymbols](/components/octiconsymbols)."
"description": "EXPERIMENTAL (May change or be removed) - Set to true when using with [OcticonSymbols](/components/alpha/octiconsymbols)."
},
{
"name": "system_arguments",
Expand Down Expand Up @@ -2337,20 +2351,6 @@
}
]
},
{
"component": "OcticonSymbols",
"status": "alpha",
"source": "https://github.com/primer/view_components/tree/main/app/components/primer/octicon_symbols_component.rb",
"lookbook": "https://primer.style/view-components/lookbook/inspect/primer/octicon_symbols_preview/default/",
"parameters": [
{
"name": "icons",
"type": "Array<Hash>",
"default": "`[]`",
"description": "List of icons to render, in the format { symbol: :icon_name, size: :small }"
}
]
},
{
"component": "Spinner",
"status": "beta",
Expand Down
1 change: 1 addition & 0 deletions static/audited_at.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Primer::Alpha::NavList": "",
"Primer::Alpha::NavList::Item": "",
"Primer::Alpha::NavList::Section": "",
"Primer::Alpha::OcticonSymbols": "",
"Primer::Alpha::SegmentedControl": "",
"Primer::Alpha::SegmentedControl::Item": "",
"Primer::Alpha::TabNav": "",
Expand Down
2 changes: 2 additions & 0 deletions static/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@
},
"Primer::Alpha::NavList::Section": {
},
"Primer::Alpha::OcticonSymbols": {
},
"Primer::Alpha::SegmentedControl": {
"FULL_WIDTH_DEFAULT": false,
"HIDE_LABELS_DEFAULT": false,
Expand Down
3 changes: 2 additions & 1 deletion static/statuses.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Primer::Alpha::NavList": "alpha",
"Primer::Alpha::NavList::Item": "alpha",
"Primer::Alpha::NavList::Section": "alpha",
"Primer::Alpha::OcticonSymbols": "alpha",
"Primer::Alpha::SegmentedControl": "alpha",
"Primer::Alpha::SegmentedControl::Item": "alpha",
"Primer::Alpha::TabNav": "alpha",
Expand Down Expand Up @@ -84,7 +85,7 @@
"Primer::MenuComponent": "deprecated",
"Primer::Navigation::TabComponent": "alpha",
"Primer::OcticonComponent": "deprecated",
"Primer::OcticonSymbolsComponent": "alpha",
"Primer::OcticonSymbolsComponent": "deprecated",
"Primer::PopoverComponent": "deprecated",
"Primer::SpinnerComponent": "beta",
"Primer::StateComponent": "beta",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "components/test_helper"

class PrimerOcticonSymbolsComponentTest < Minitest::Test
class PrimerAlphaOcticonSymbolsTest < Minitest::Test
include Primer::ComponentTestHelpers

def test_renders_one_octicon
Expand All @@ -12,13 +12,13 @@ def test_renders_one_octicon
}
]

render_inline(Primer::OcticonSymbolsComponent.new(icons: icons))
render_inline(Primer::Alpha::OcticonSymbols.new(icons: icons))

assert_selector("svg symbol#octicon_alert_16 path", visible: false)
end

def test_does_not_render_if_there_are_no_icons
render_inline(Primer::OcticonSymbolsComponent.new)
render_inline(Primer::Alpha::OcticonSymbols.new)

refute_selector("svg", visible: false)
end
Expand All @@ -34,7 +34,7 @@ def test_renders_octicon_with_alternate_sizes
}
]

render_inline(Primer::OcticonSymbolsComponent.new(icons: icons))
render_inline(Primer::Alpha::OcticonSymbols.new(icons: icons))

assert_selector("symbol#octicon_alert_16", visible: false)
assert_selector("symbol#octicon_alert_24", visible: false)
Expand All @@ -51,7 +51,7 @@ def test_renders_one_octicon_when_only_one_size_exists
}
]

render_inline(Primer::OcticonSymbolsComponent.new(icons: icons))
render_inline(Primer::Alpha::OcticonSymbols.new(icons: icons))

assert_selector("symbol#octicon_markdown_16", count: 1, visible: false)
end
Expand Down
1 change: 1 addition & 0 deletions test/components/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class PrimerComponentTest < Minitest::Test

def test_registered_components
ignored_components = [
"Primer::OcticonSymbolsComponent",
"Primer::OcticonComponent",
"Primer::Markdown",
"Primer::MenuComponent",
Expand Down

0 comments on commit d709bae

Please sign in to comment.