ElMassimo / vite_ruby

⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
https://vite-ruby.netlify.app/
MIT License
1.32k stars 122 forks source link

Feature request: vite_picture_tag #386

Closed mattbrictson closed 1 year ago

mattbrictson commented 1 year ago

Is your feature request related to a problem? Please describe.

Rails introduced a picture_tag helper earlier this year. It is on the main branch (not yet released) and presumably will be part of Rails 7.1.

Describe the solution you'd like

I'd like to see a vite_picture_tag wrapper, perhaps with an implementation like this:

def vite_picture_tag(*sources, &block)
  sources.flatten!
  options = sources.extract_options!
  vite_sources = sources.map { |src| vite_asset_path(src) }

  # Delegate to ActionView::Helpers::AssetTagHelper#picture_tag
  picture_tag(*vite_sources, options, &block)
end

Describe alternatives you've considered

The tag helper is syntax sugar; <picture> tags can of course still be created manually without too much effort:

tag.picture do
  tag.source srcset: vite_asset_path("images/one.png")
  tag.source srcset: vite_asset_path("images/two.png")
  vite_image_tag("images/two.png", alt: "example")
end

Versus:

vite_picture_tag("images/one.png", "images/two.png", image: {alt: "example"})

Additional context

I'd be happy to contribute a PR, but I am wondering: should I wait until Rails 7.1 is released?

ElMassimo commented 1 year ago

Hi Matt!

Sounds reasonable to add vite_picture_tag once picture_tag is released.

To avoid users from attempting to use it in older versions of Rails it might be preferable to do something like:

if defined?(Rails) && Rails.gem_version >= Gem::Version.new("7.1")
  def vite_picture_tag(*sources, &block)