From d709bae13ad0ff907b20b0471cbcb847daf3724a Mon Sep 17 00:00:00 2001 From: River Lynn Bailey Date: Fri, 9 Dec 2022 12:45:30 -0600 Subject: [PATCH] Move `Primer::OcticonSymbolComponent` to `Primer::Alpha::OcticonSymbols` (#1696) --- .changeset/happy-crabs-decide.md | 5 ++ .../octicon_symbols.html.erb} | 0 .../primer/alpha/octicon_symbols.rb | 61 +++++++++++++++++++ app/components/primer/beta/octicon.rb | 2 +- .../primer/octicon_symbols_component.rb | 56 +---------------- component_status_migrator.thor | 13 ++++ .../src/@primer/gatsby-theme-doctocat/nav.yml | 2 +- lib/primer/deprecations.yml | 4 ++ lib/tasks/docs.rake | 2 +- static/arguments.json | 30 ++++----- static/audited_at.json | 1 + static/constants.json | 2 + static/statuses.json | 3 +- .../octicon_symbols_test.rb} | 10 +-- test/components/component_test.rb | 1 + 15 files changed, 114 insertions(+), 78 deletions(-) create mode 100644 .changeset/happy-crabs-decide.md rename app/components/primer/{octicon_symbols_component.html.erb => alpha/octicon_symbols.html.erb} (100%) create mode 100644 app/components/primer/alpha/octicon_symbols.rb rename test/components/{octicon_symbols_component_test.rb => alpha/octicon_symbols_test.rb} (75%) diff --git a/.changeset/happy-crabs-decide.md b/.changeset/happy-crabs-decide.md new file mode 100644 index 0000000000..f9f0d6798a --- /dev/null +++ b/.changeset/happy-crabs-decide.md @@ -0,0 +1,5 @@ +--- +'@primer/view-components': patch +--- + +Moving `Primer::OcticonSymbolsComponent` to `Primer::Alpha::OcticonSymbols`, and deprecating the original diff --git a/app/components/primer/octicon_symbols_component.html.erb b/app/components/primer/alpha/octicon_symbols.html.erb similarity index 100% rename from app/components/primer/octicon_symbols_component.html.erb rename to app/components/primer/alpha/octicon_symbols.html.erb diff --git a/app/components/primer/alpha/octicon_symbols.rb b/app/components/primer/alpha/octicon_symbols.rb new file mode 100644 index 0000000000..14f9a03013 --- /dev/null +++ b/app/components/primer/alpha/octicon_symbols.rb @@ -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] 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 diff --git a/app/components/primer/beta/octicon.rb b/app/components/primer/beta/octicon.rb index ee9e94ed57..c917fa5421 100644 --- a/app/components/primer/beta/octicon.rb +++ b/app/components/primer/beta/octicon.rb @@ -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 diff --git a/app/components/primer/octicon_symbols_component.rb b/app/components/primer/octicon_symbols_component.rb index fda7b859a4..a2db3766ad 100644 --- a/app/components/primer/octicon_symbols_component.rb +++ b/app/components/primer/octicon_symbols_component.rb @@ -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] 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 diff --git a/component_status_migrator.thor b/component_status_migrator.thor index 850632a3f0..50f7279caa 100644 --- a/component_status_migrator.thor +++ b/component_status_migrator.thor @@ -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 @@ -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 @@ -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) @@ -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' @@ -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 ", + "default": "`[]`", + "description": "List of icons to render, in the format { symbol: :icon_name, size: :small }" + } + ] + }, { "component": "SegmentedControl", "status": "alpha", @@ -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", @@ -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", - "default": "`[]`", - "description": "List of icons to render, in the format { symbol: :icon_name, size: :small }" - } - ] - }, { "component": "Spinner", "status": "beta", diff --git a/static/audited_at.json b/static/audited_at.json index 9fe6d18b01..a61b9f8d0f 100644 --- a/static/audited_at.json +++ b/static/audited_at.json @@ -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": "", diff --git a/static/constants.json b/static/constants.json index 6af495ae95..ba93b0d3b3 100644 --- a/static/constants.json +++ b/static/constants.json @@ -319,6 +319,8 @@ }, "Primer::Alpha::NavList::Section": { }, + "Primer::Alpha::OcticonSymbols": { + }, "Primer::Alpha::SegmentedControl": { "FULL_WIDTH_DEFAULT": false, "HIDE_LABELS_DEFAULT": false, diff --git a/static/statuses.json b/static/statuses.json index 36b05e2aec..8b977c2833 100644 --- a/static/statuses.json +++ b/static/statuses.json @@ -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", @@ -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", diff --git a/test/components/octicon_symbols_component_test.rb b/test/components/alpha/octicon_symbols_test.rb similarity index 75% rename from test/components/octicon_symbols_component_test.rb rename to test/components/alpha/octicon_symbols_test.rb index 8094e9f75e..42ac2075af 100644 --- a/test/components/octicon_symbols_component_test.rb +++ b/test/components/alpha/octicon_symbols_test.rb @@ -2,7 +2,7 @@ require "components/test_helper" -class PrimerOcticonSymbolsComponentTest < Minitest::Test +class PrimerAlphaOcticonSymbolsTest < Minitest::Test include Primer::ComponentTestHelpers def test_renders_one_octicon @@ -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 @@ -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) @@ -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 diff --git a/test/components/component_test.rb b/test/components/component_test.rb index 761f601d2d..2efeec7231 100644 --- a/test/components/component_test.rb +++ b/test/components/component_test.rb @@ -111,6 +111,7 @@ class PrimerComponentTest < Minitest::Test def test_registered_components ignored_components = [ + "Primer::OcticonSymbolsComponent", "Primer::OcticonComponent", "Primer::Markdown", "Primer::MenuComponent",