Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where dialog is unable to be closed #2288

Merged
merged 4 commits into from
Oct 18, 2023
Merged
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
7 changes: 7 additions & 0 deletions .changeset/eleven-steaks-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/view-components': patch
---

Fixes issue where sometimes a dialog cannot be closed if another is open

<!-- Changed components: Primer::Alpha::Dialog -->
21 changes: 9 additions & 12 deletions app/components/primer/alpha/modal_dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,17 @@ function clickHandler(event: Event) {
return
}
}
// Find the top level dialog that is open.
const topLevelDialog = overlayStack[overlayStack.length - 1]
if (!topLevelDialog) return
Comment on lines -33 to -35
Copy link
Contributor Author

@TylerJDev TylerJDev Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not seem like we needed to rely on checking if the "top level" dialog was open at this point. The function should only be ran the moment a user clicks the close button. This should only target the dialog where that event stemmed, and shouldn't affect any other dialogs that are open.

I removed this logic and replaced it with a conditional that will only check if one of the two acceptable button data-* attributes exist.


dialogId = button.getAttribute('data-close-dialog-id')
if (dialogId === topLevelDialog.id) {
overlayStack.pop()
topLevelDialog.close()
}
if (!overlayStack.length) return

dialogId = button.getAttribute('data-submit-dialog-id')
if (dialogId === topLevelDialog.id) {
overlayStack.pop()
topLevelDialog.close(true)
dialogId = button.getAttribute('data-close-dialog-id') || button.getAttribute('data-submit-dialog-id')
if (dialogId) {
const dialog = document.getElementById(dialogId)
if (dialog instanceof ModalDialogElement) {
const dialogIndex = overlayStack.findIndex(ele => ele.id === dialogId)
overlayStack.splice(dialogIndex, 1)
dialog.close(button.hasAttribute('data-submit-dialog-id'))
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/system/alpha/dialog_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

module Alpha
class IntegrationDialogTest < System::TestCase
def click_on_initial_dialog_close_button
find("button[data-close-dialog-id='dialog-one']").trigger("click")
end

def click_on_nested_dialog_close_button
find("button[data-close-dialog-id='dialog-two']").click
end

def click_on_nested_dialog_button
find("#dialog-show-dialog-two").click
end

def test_modal_has_accessible_name
visit_preview(:default)

Expand All @@ -20,5 +32,32 @@ def test_focuses_close_button

assert_equal page.evaluate_script("document.activeElement")["aria-label"], "Close"
end

def test_closes_top_level_dialog
visit_preview(:nested_dialog)

click_button("Show Dialog")
click_on_nested_dialog_button

assert_equal(find("modal-dialog#dialog-two")["open"], true)

click_on_nested_dialog_close_button

assert_selector "modal-dialog#dialog-two", visible: :hidden
assert_selector "modal-dialog#dialog-one"
end

def test_closes_dialog_that_is_not_top_level
visit_preview(:nested_dialog)

click_button("Show Dialog")
click_on_nested_dialog_button

assert_equal(find("modal-dialog#dialog-two")["open"], true)

click_on_initial_dialog_close_button

assert_selector "modal-dialog#dialog-one", visible: :hidden
end
end
end
Loading