Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the wrapping blacklight-icons span to componentized icons #2843

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, tag: :span, name: nil, label: nil, aria_hidden: nil, classes: nil, **options)
self.svg = svg if svg
@classes = Array(classes) + ['blacklight-icons', "blacklight-icons-#{name}"]
@name = name
@tag = tag
@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(@tag, svg&.html_safe, # rubocop:disable Rails/OutputSafety
class: @classes,
**@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