Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/proud-baths-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': minor
---

SelectPanel: Prevents `keydown` events from escaping the SelectPanel when within the items.
12 changes: 12 additions & 0 deletions app/components/primer/alpha/select_panel_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ export class SelectPanelElement extends HTMLElement {
return
}

if (
event.type === 'keydown' &&
event instanceof KeyboardEvent &&
(event.target as Element).closest(visibleMenuItemSelectors)
) {
const hasModifier = event.ctrlKey || event.altKey || event.metaKey
const isAlphabetKey = event.key.length === 1 && /[a-z\d]/i.test(event.key)

// eslint-disable-next-line no-restricted-syntax
if (!hasModifier && isAlphabetKey) event.stopPropagation()
}

if (event.target === this.dialog && event.type === 'close') {
// Remove data-ready so it can be set the next time the panel is opened
this.dialog.removeAttribute('data-ready')
Expand Down
30 changes: 29 additions & 1 deletion test/system/alpha/select_panel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1365,5 +1365,33 @@ def test_announces_error_on_filter_failure
end
end
end

def test_prevents_events_from_bubbling
visit_preview(:default)

click_on_invoker_button

Copilot AI Sep 29, 2025

Copy link

Choose a reason for hiding this comment

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

Remove trailing whitespace on this line.

Suggested change

Copilot uses AI. Check for mistakes.
keyboard.type(:tab)

evaluate_multiline_script(<<~JS)
document.body.addEventListener('keydown', (event) => {
window.bodyKeydownFired = true
}, { once: true })
JS

assert_includes active_element.text, "Item 1"

keyboard.type("a") # type an alphabetic key, which should be stopped from propagating

refute page.evaluate_script("window.bodyKeydownFired")

keyboard.type(:escape) # close the panel

refute_selector "select-panel dialog[open]"

keyboard.type("a")

Copilot AI Sep 29, 2025

Copy link

Choose a reason for hiding this comment

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

Remove trailing whitespace on this line.

Suggested change
keyboard.type("a")
keyboard.type("a")

Copilot uses AI. Check for mistakes.

assert page.evaluate_script("window.bodyKeydownFired")
end
end
end
end
Loading