Skip to content

Instantly share code, notes, and snippets.

@palkan
Last active March 2, 2024 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save palkan/aeb398fcf8ace091d7c3d44bd83f0da7 to your computer and use it in GitHub Desktop.
Save palkan/aeb398fcf8ace091d7c3d44bd83f0da7 to your computer and use it in GitHub Desktop.
[ViewComponent] html_option
# 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