Skip to content

Instantly share code, notes, and snippets.

@existentialmutt
Last active March 31, 2023 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save existentialmutt/6b2f9a4056bc0c9d1b5e7c1e3e348ef8 to your computer and use it in GitHub Desktop.
Save existentialmutt/6b2f9a4056bc0c9d1b5e7c1e3e348ef8 to your computer and use it in GitHub Desktop.
Modal ViewComponent
<div class="modal fade" id="<%= dom_id %>" role="dialog" tabindex="-1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<% if header %>
<div class="modal-header">
<%= header %>
<%- if header_close_btn %>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<i class="fa fa-times-circle"></i>
</button>
<%- end %>
</div>
<% end %>
<div class="modal-body">
<%= content %>
</div>
<% if footer %>
<div class="modal-footer">
<%= footer %>
</div>
<% end %>
</div>
</div>
</div>
<%- if open %>
<script>
new bsModal(document.getElementById("<%= dom_id %>"))
</script>
<%- end %>
# frozen_string_literal: true
class ModalComponent < ViewComponent::Base
with_content_areas :header, :footer
attr_accessor :dom_id, :open, :header_close_btn
def initialize(dom_id:, open: true, header_close_btn: true)
self.dom_id ||= "modal-#{SecureRandom.hex(12)}"
self.open = open
self.header_close_btn = header_close_btn
end
end
<%= render ModalComponent.new(open: true) do |c| %>
<%- c.header do %>
Hello World!
<%- end %>
<%- c.footer do %>
Some Footer Content
<%- end %>
Everything else in this block gets printed to the default slot `content`
<%- end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment