Skip to content

Commit

Permalink
Modals with HTML dialog element, Tailwind and Stimulus
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Aug 15, 2023
1 parent 270e25b commit f5a6356
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/javascript/controllers/dialog_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="dialog"
export default class extends Controller {
static targets = ["modal"]
connect() {
}

open() {
// this.modalTarget.show();
this.modalTarget.showModal();
document.body.classList.add("overflow-hidden")
}

clickOutside(event) {
if (event.target === this.modalTarget) {
this.modalTarget.close();
}
}
}
20 changes: 20 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@
</div>
</nav>
<main class="bg-slate-500 w-5/6 p-4 flex-grow">

<%= Time.zone.now %>
<div data-controller="dialog" data-action="click->dialog#clickOutside">
<button data-action="click->dialog#open">Open modal</button>
<dialog data-dialog-target="modal" class="backdrop:bg-slate-400 backdrop:opacity-80 w-full lg:w-2/3">
<div class="p-4">
<p>Greetings, one and all!</p>
<form>
<button formmethod="dialog">Cancel</button>
<button>Submit</button>
</form>
<% (1..100).each do |i| %>
<div class="bg-rose-400">
<%= i%>
</div>
<% end %>
</div>
</dialog>
</div>

2-column layout with fixed widths
<div class="flex flex-col md:flex-row gap-4">
<div class="w-full md:w-3/4">
Expand Down

0 comments on commit f5a6356

Please sign in to comment.