Last active
March 2, 2024 18:06
-
-
Save palkan/aeb398fcf8ace091d7c3d44bd83f0da7 to your computer and use it in GitHub Desktop.
[ViewComponent] html_option
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
# From https://evilmartians.com/chronicles/viewcomponent-in-the-wild-embracing-tailwindcss-classes-and-html-attributes | |
class ApplicationViewComponent < ViewComponentContrib::Base | |
# Move default attrs to a constants so we can re-use it | |
EMPTY_ATTRS = {}.freeze | |
class << self | |
def html_option(name, default: nil, **opts) | |
if default | |
opts[:type] = proc { default.merge(_1) } | |
end | |
# Assuming you have `extend Dry::Initializer` | |
option name, default: proc { EMPTY_ATTRS }, **opts | |
end | |
end | |
def dots(**) | |
tag.attributes(**) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment