Created
August 21, 2021 17:05
-
-
Save brandoncordell/cc5e154286dea0e58e6a4f5513957be0 to your computer and use it in GitHub Desktop.
view_component issue
This file contains hidden or 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
# 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 |
This file contains hidden or 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
<%= render Test::Component.new %> | |
<%= render Icon::Component.new %> |
This file contains hidden or 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
# 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