Skip to content

Commit

Permalink
feature: action dynamic backdrop option (#3379)
Browse files Browse the repository at this point in the history
* feature: action dynamic backdrop option

* style: dynamic_backdrop reader

* code review changes

* rename to `close_modal_on_backdrop_click`
  • Loading branch information
thiagoyoussef authored Nov 5, 2024
1 parent 15836c2 commit 40abfed
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/components/avo/modal_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<div class="modal-container fixed inset-0 w-full min-h-screen z-[100] flex justify-center items-center"
data-controller="modal"
data-modal-target="modal"
data-modal-close-modal-on-backdrop-click-value="<%= close_modal_on_backdrop_click %>"
>
<div aria-expanded="true" class="modal-overlay absolute w-full h-full bg-opacity-25 bg-gray-800 flex justify-center items-center" data-action="click->modal#close"></div>
<div aria-expanded="true" class="modal-overlay absolute w-full h-full bg-opacity-25 bg-gray-800 flex justify-center items-center" data-modal-target="backdrop" data-action="click->modal#close" ></div>
<div aria-expanded="true" role="dialog" aria-modal="true" class="modal-body rounded-lg inset-auto bg-white flex z-50 relative shadow-modal <%= overflow_classes %> <%= width_classes %> <%= height_classes %>">
<div class="flex-1 flex flex-col justify-between">
<div>
Expand All @@ -25,4 +26,3 @@
</div>
</div>
</div>

1 change: 1 addition & 0 deletions app/components/avo/modal_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Avo::ModalComponent < Avo::BaseComponent
prop :width, default: :md
prop :body_class
prop :overflow, default: :auto
prop :close_modal_on_backdrop_click, default: true, reader: :public

def width_classes
case @width.to_sym
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/js/controllers/modal_controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ['modal']
static targets = ['modal', 'backdrop']

static values = {
closeModalOnBackdropClick: true,
}

close() {
if (event.target === this.backdropTarget && !this.closeModalOnBackdropClickValue) return

this.modalTarget.remove()

document.dispatchEvent(new Event('actions-modal:close'))
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
**@action.class.form_data_attributes,
} do |form|
%>
<%= render Avo::ModalComponent.new do |c| %>
<%= render Avo::ModalComponent.new(close_modal_on_backdrop_click: @action.close_modal_on_backdrop_click) do |c| %>
<% c.with_heading do %>
<%= @action.action_name %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BaseAction
class_attribute :may_download_file
class_attribute :turbo
class_attribute :authorize, default: true
class_attribute :close_modal_on_backdrop_click, default: true

attr_accessor :view
attr_accessor :response
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/avo/actions/export_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Avo::Actions::ExportCsv < Avo::BaseAction
self.name = "Export CSV"
self.no_confirmation = false
self.standalone = true
self.close_modal_on_backdrop_click = false

def fields
# Add more fields here for custom user-selected columns
Expand Down
29 changes: 29 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,35 @@
end
end

describe "action close_modal_on_backdrop_click" do
it "closes the modal on backdrop click" do
Avo::Actions::ExportCsv.close_modal_on_backdrop_click = true

visit "/admin/resources/projects"

click_on "Actions"
click_on "Export CSV"
find('[data-modal-target="backdrop"]').trigger("click")

expect(page).not_to have_selector '[data-controller="modal"]'
end

it "does not close the modal on backdrop click" do
Avo::Actions::ExportCsv.close_modal_on_backdrop_click = false

visit "/admin/resources/projects"

click_on "Actions"
click_on "Export CSV"
find('[data-modal-target="backdrop"]').trigger("click")

expect(page).to have_selector '[data-controller="modal"]'

click_on "Cancel"
expect(page).not_to have_selector '[data-controller="modal"]'
end
end

describe "redirects when no confirmation" do
it "redirects to hey page" do
visit "/admin/resources/users"
Expand Down

0 comments on commit 40abfed

Please sign in to comment.