-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
Primer::OcticonSymbolComponent
to `Primer::Alpha::OcticonSymbo…
…ls` (#1696)
- Loading branch information
1 parent
4ff75a5
commit d709bae
Showing
15 changed files
with
114 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters