Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Created August 21, 2021 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandoncordell/cc5e154286dea0e58e6a4f5513957be0 to your computer and use it in GitHub Desktop.
Save brandoncordell/cc5e154286dea0e58e6a4f5513957be0 to your computer and use it in GitHub Desktop.
view_component issue
# frozen_string_literal: true
# app/components/icon/component.rb (using sidecar assets via the view_component-contrib gem)
module Icon
class Component < ApplicationViewComponent
DEFAULT_SIZE = :base
SIZES = %i[xxs xs sm base lg xl xxl].freeze
SIZE_MAPPINGS = {
xxs: 'fa-2xs',
xs: 'fa-xs',
sm: 'fa-sm',
base: '',
lg: 'fa-lg',
xl: 'fa-xl',
xxl: 'fa-2xl'
}.freeze
DEFAULT_STYLE = :solid
STYLES = %i[brands duotone light regular solid thin].freeze
STYLE_MAPPINGS = {
brands: 'fab',
duotone: 'fad',
light: 'fal',
regular: 'far',
solid: 'fas',
thin: 'fat'
}.freeze
end
def initialize(icon:, style: DEFAULT_STYLE, size: DEFAULT_SIZE, **options)
super
@icon = icon
@style = style
@size = size
@options = options
end
def call
tag.i content_tag_options
end
def classes
"fa #{STYLE_MAPPINGS[@style]} #{SIZE_MAPPINGS[@size]} #{@icon}"
end
def content_tag_options
options = @options.except(:icon, :size, :style)
options[:class] = classes
options
end
end
<%= render Test::Component.new %>
<%= render Icon::Component.new %>
# frozen_string_literal: true
# app/components/test/component.rb
module Test
class Component < ApplicationViewComponent
def call
content_tag :p, 'Test!'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment