Skip to content

Commit

Permalink
Restore the wrapping blacklight-icons span to componentized icons
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 13, 2022
1 parent f26bb06 commit a796a4b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 14 additions & 2 deletions app/components/blacklight/icons/icon_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ module Icons
# You can override the default svg by setting:
# Blacklight::Icons::ListComponent.svg = '<svg>your SVG here</svg>'
class IconComponent < ::ViewComponent::Base
def initialize(svg: nil)
# rubocop:disable Metrics/ParameterLists
def initialize(svg: nil, component: :span, name: nil, label: nil, aria_hidden: nil, classes: '', **options)
self.svg = svg if svg
@classes = classes
@name = name
@component = component
@options = options.merge(aria: options.fetch(:aria, {}).reverse_merge(label: label, hidden: aria_hidden))
end
# rubocop:enable Metrics/ParameterLists

def call
svg&.html_safe # rubocop:disable Rails/OutputSafety
tag.public_send(@component, svg&.html_safe, # rubocop:disable Rails/OutputSafety
class: ['blacklight-icons', "blacklight-icons-#{name}", @classes].flatten,
**@options)
end

class_attribute :svg

def name
@name ||= self.class.name.demodulize.underscore.sub('_component', '')
end
end
end
end
6 changes: 3 additions & 3 deletions app/helpers/blacklight/icon_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ module Blacklight::IconHelperBehavior
# the svg everytime.
# @param [String, Symbol] icon_name
# @return [String]
def blacklight_icon(icon_name, _options = {})
render "Blacklight::Icons::#{icon_name.to_s.camelize}Component".constantize.new
def blacklight_icon(icon_name, **kwargs)
render "Blacklight::Icons::#{icon_name.to_s.camelize}Component".constantize.new(**kwargs)
rescue NameError
Blacklight.deprecation.warn(
"Falling back on the LegacyIconComponent with \"#{icon_name}\" is deprecated. Instead create the component `Blacklight::Icons::#{icon_name.to_s.camelize}Component` for this icon."
)

render Blacklight::Icons::LegacyIconComponent.new(name: icon_name)
render Blacklight::Icons::LegacyIconComponent.new(name: icon_name, **kwargs)
end
end
16 changes: 14 additions & 2 deletions spec/helpers/blacklight/icon_helper_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

RSpec.describe Blacklight::IconHelperBehavior do
describe '#blacklight_icon' do
subject(:icon) { helper.blacklight_icon(:search) }
subject(:icon) { helper.blacklight_icon(:search, classes: 'custom-class') }

it 'returns the svg' do
expect(icon).to have_css 'svg'
expect(icon).to have_css '.blacklight-icons svg'
end

it 'adds classes to the wrappering element' do
expect(icon).to have_css '.custom-class svg'
end

context 'with backwards compatible arguments' do
subject(:icon) { helper.blacklight_icon(:search, aria_hidden: true, label: 'blah') }

it 'adds aria attributes' do
expect(icon).to have_css '[aria-hidden="true"][aria-label="blah"]'
end
end
end
end

0 comments on commit a796a4b

Please sign in to comment.