Last active
March 31, 2023 19:22
-
-
Save existentialmutt/6b2f9a4056bc0c9d1b5e7c1e3e348ef8 to your computer and use it in GitHub Desktop.
Modal ViewComponent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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