Created
September 23, 2022 22:08
-
-
Save andrewmcodes/f4517f8089723b6c0c78cde219b97734 to your computer and use it in GitHub Desktop.
Example showing how to use `as` option in ViewComponent
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
class BoxComponent < ViewComponent::Base | |
DEFAULT_TAG = :div | |
def initialize(**opts) | |
@opts = opts | |
@tag_opts = opts.except(:as, :classes) | |
end | |
def call | |
tag.send tag_name, content, class: classes, **tag_opts | |
end | |
private | |
attr_reader :opts, :tag_opts | |
def classes | |
join_classes [opts[:classes], "foo"] | |
end | |
def tag_name | |
opts.fetch(:as, DEFAULT_TAG).to_sym | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment