Skip to content

Instantly share code, notes, and snippets.

@boardfish
Last active February 3, 2022 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boardfish/cd998a3975ac9a332966e3ba9346dde4 to your computer and use it in GitHub Desktop.
Save boardfish/cd998a3975ac9a332966e3ba9346dde4 to your computer and use it in GitHub Desktop.
Basic <picture> component
<%= render PictureComponent.new do |c| %>
<% c.source(srcset: 'http://placehold.it/500', media: '(min-width: 800px)') %>
<% c.img(srcset: 'http://placehold.it/300', alt: 'Placeholder image') %>
<% end %>
<picture>
<% sources.each do |source| %>
<%= source %>
<% end %>
<%= img %>
</picture>
# frozen_string_literal: true
class PictureComponent < ApplicationComponent
renders_many :sources, ->(**params) { tag.source(**params) }
renders_one :img, ->(**params) { tag.img(**params) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment