Skip to content

Commit 1259249

Browse files
keithamuscamertron
andauthored
Fix dialog click outside early return (#2549)
Co-authored-by: Cameron Dutro <[email protected]>
1 parent c3260e1 commit 1259249

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.changeset/small-flowers-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/view-components": patch
3+
---
4+
5+
Ensure dialogs do not close when a child menu item (or similar) is clicked

app/components/primer/dialog_helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function dialogInvokerButtonHandler(event: Event) {
1515
// If the behaviour is allowed through the dialog will be shown but then
1616
// quickly hidden- as if it were never shown. This prevents that.
1717
event.preventDefault()
18+
event.stopPropagation()
1819
}
1920
}
2021

@@ -35,7 +36,7 @@ export class DialogHelperElement extends HTMLElement {
3536
#abortController: AbortController | null = null
3637
connectedCallback() {
3738
const {signal} = (this.#abortController = new AbortController())
38-
document.addEventListener('click', dialogInvokerButtonHandler)
39+
document.addEventListener('click', dialogInvokerButtonHandler, true)
3940
document.addEventListener('click', this, {signal})
4041
this.ownerDocument.body.style.setProperty(
4142
'--dialog-scrollgutter',
@@ -58,7 +59,10 @@ export class DialogHelperElement extends HTMLElement {
5859
const target = event.target as HTMLElement
5960
const dialog = this.dialog
6061
if (!dialog?.open) return
61-
if (target?.closest('dialog') !== dialog) return
62+
63+
// if the target is inside the dialog, but is not the dialog itself, leave
64+
// the dialog open
65+
if (target?.closest('dialog') === dialog && target !== dialog) return
6266

6367
const rect = dialog.getBoundingClientRect()
6468
const clickWasInsideDialog =

test/css/component_specific_selectors_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class ComponentSpecificSelectorsTest < Minitest::Test
3737
".Banner .Banner-close"
3838
],
3939
Primer::Alpha::Dialog => [
40-
".Overlay"
40+
".Overlay",
41+
".has-modal"
4142
],
4243
Primer::Alpha::Layout => [
4344
".Layout-divider",

test/system/alpha/dialog_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,24 @@ def test_with_scrollable_body
7474
visit_preview(:long_text)
7575
click_button("Show Dialog")
7676
end
77+
78+
def test_outside_click_closes_dialog
79+
visit_preview(:default)
80+
81+
click_button("Show Dialog")
82+
page.driver.browser.mouse.click(x: 0, y: 0)
83+
84+
refute_selector "dialog[open]"
85+
end
86+
87+
def test_outside_menu_click_does_not_close_dialog
88+
visit_preview(:with_auto_complete)
89+
90+
click_button("Show Dialog")
91+
fill_in "input", with: "a"
92+
93+
find(".ActionListItem", text: "Avocado").click
94+
assert_selector "dialog[open]"
95+
end
7796
end
7897
end

0 commit comments

Comments
 (0)