Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/components/ruby_ui/dialog/dialog_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ def initialize(size: :md, **attrs)
end

def view_template
template(data: {ruby_ui__dialog_target: "content"}) do
div(data_controller: "ruby-ui--dialog") do
backdrop
div(**attrs) do
yield
close_button
end
dialog(data: {ruby_ui__dialog_target: "modal"}, class: "backdrop:bg-transparent") do
backdrop
div(**attrs) do
yield
close_button
end
end
end
Expand All @@ -33,8 +31,9 @@ def view_template
def default_attrs
{
data_state: "open",
data_ruby_ui__dialog_target: "content",
class: [
"fixed flex flex-col pointer-events-auto left-[50%] top-[50%] z-50 w-full max-h-screen overflow-y-auto translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 sm:rounded-lg md:w-full",
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closing]:animate-out data-[state=closing]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closing]:zoom-out-95 data-[state=open]:zoom-in-95 sm:rounded-lg",
SIZES[@size]
]
}
Expand Down Expand Up @@ -69,8 +68,9 @@ def close_button
def backdrop
div(
data_state: "open",
data_ruby_ui__dialog_target: "backdrop",
data_action: "click->ruby-ui--dialog#dismiss esc->ruby-ui--dialog#dismiss",
class: "fixed pointer-events-auto inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=open]:fade-in-0"
class: "fixed pointer-events-auto inset-0 z-50 bg-black/50 data-[state=open]:animate-in data-[state=closing]:animate-out data-[state=closing]:fade-out-0 duration-200 data-[state=open]:fade-in-0"
)
end
end
Expand Down
39 changes: 29 additions & 10 deletions app/javascript/controllers/ruby_ui/dialog_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="dialog"
export default class extends Controller {
static targets = ["content"]
static targets = ["modal", "content", "backdrop"]
static values = {
open: {
type: Boolean,
Expand All @@ -11,22 +11,41 @@ export default class extends Controller {
}

connect() {
if (this.openValue) {
this.open()
}
if (this.openValue) this.open()
}

open(e) {
e?.preventDefault();
document.body.insertAdjacentHTML('beforeend', this.contentTarget.innerHTML)
// prevent scroll on body
document.body.classList.add('overflow-hidden')
this.modalTarget.showModal()

this.backdropTarget.setAttribute('data-state', 'open')
this.contentTarget.setAttribute('data-state', 'open')
}

dismiss() {
// allow scroll on body
const content = this.contentTarget.querySelector('div[data-state]')
const backdrop = this.contentTarget.querySelector('div[data-action*="dismiss"]')
if (content) content.setAttribute('data-state', 'closing')
this.backdropTarget.setAttribute('data-state', 'closing')
this.contentTarget.setAttribute('data-state', 'closing')


Promise.all(
this.contentTarget.getAnimations().map((animation) => animation.finished),
this.backdropTarget.getAnimations().map((animation) => animation.finished),
).then(() => {
this.contentTarget.removeAttribute("closing")
this.backdropTarget.removeAttribute("closing")
this.modalTarget.close()
})

document.body.classList.remove('overflow-hidden')
// remove the element
this.element.remove()

// Hide after animation completes (150ms)
// setTimeout(() => {
// this.contentTarget.removeAttribute("closing")
// this.backdropTarget.removeAttribute("closing")
// this.modalTarget.close()
// }, 600)
}
}
3 changes: 2 additions & 1 deletion app/views/docs/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def view_template
DialogTrigger do
Button { "Open Dialog" }
end
DialogContent do
# class: "backdrop:bg-black/80 border-red-500 border-2"
DialogContent() do
DialogHeader do
DialogTitle { "RubyUI to the rescue" }
DialogDescription { "RubyUI helps you build accessible standard compliant web apps with ease" }
Expand Down