Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
We wanted to use the HTML
<picture>
tag in our website for responsive images (specifically, for art direction) and found that Rails did not have built-in support for it. By introducing thepicture_tag
helper, we can simplify the usage of the<picture>
element in Rails applications, enabling developers to incorporate responsive images in their projects easily.The need for a
picture_tag
in Rails led to the creation of this gem and this RailsConf talk. After presenting the talk, a lot of people told me I should add this feature to Rails, so I'm porting some of the gem's functionality here.The
picture
tag is a more "robust" version of theimg
tag, because it supports resolution switching, art direction, and image support fallback, so I think we should have it in Rails (theimg
tag only supports resolution switching). More information here:Detail
This PR adds support for the HTML picture tag. It supports passing one element (String), multiple (an Array), or a Block if you need full control of what it's being generated. All properties that are passed to the helper will apply to the
picture
tag. If you need to pass properties to theimg
tag, you can do it inside the:image
key.Since the
picture
tag requires animg
tag (contrary to theaudio_tag
orvideo_tag
, which only requiresource
s), the last element you provide will be used as the source for theimg
tag. For complete control over the picture tag, a block can be passed, which will populate the contents of the tag accordingly.It can be used like this for a single source:
which will generate the following:
For multiple sources:
will generate:
Full control via a block:
will generate:
Additional information
Another option would be to be more explicit on the image and sources that are generated (could be more cumbersome).
Something like this:
The issue with this is that we would need a way to specify which properties belong to the
img
tag and which to thepicture
tag itself. I did not go with this approach since I tried to more or less replicate how theaudio_tag
,video_tag
, andimage_tag
helpers work.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]