Skip to content

Commit e3e29b0

Browse files
acbretoncamertron
andauthored
Prevent Scroll on ActionMenu (#3327)
Co-authored-by: Cameron Dutro <camertron@gmail.com>
1 parent db524df commit e3e29b0

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

.changeset/kind-shirts-shout.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+
Add preventScroll to focus call in Action Menu

app/components/primer/alpha/action_menu/action_menu_element.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ export class ActionMenuElement extends HTMLElement {
205205
const eventIsActivation = this.#isActivation(event)
206206

207207
if (event.type === 'toggle' && (event as ToggleEvent).newState === 'open') {
208-
this.#firstItem?.focus()
208+
window.requestAnimationFrame(() => {
209+
this.#firstItem?.focus()
210+
})
209211
}
210212

211213
if (targetIsInvoker && event.type === 'mousedown') {
@@ -371,7 +373,7 @@ export class ActionMenuElement extends HTMLElement {
371373
}
372374

373375
#handleIncludeFragmentReplaced() {
374-
if (this.#firstItem) this.#firstItem.focus()
376+
this.#firstItem?.focus()
375377
this.#softDisableItems()
376378

377379
// async items have loaded, so component is ready

script/test-setup

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/bash
22

3+
bundle
4+
pushd demo; bundle; popd
35
overmind start --daemonize --procfile Procfile.test --socket ./overmind-test.sock

test/system/alpha/action_bar_test.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def test_focus_set_within_overflow_menu
4444

4545
# We want to ensure that we're within the ActionMenu assert_selector("action-bar") do
4646
keyboard.type(:enter)
47-
assert_equal page.evaluate_script("document.activeElement.classList.contains('ActionListContent')"), true
47+
48+
assert_selector ".ActionListContent" do |content|
49+
page.evaluate_script("document.activeElement === arguments[0]", content)
50+
end
4851
end
4952

5053
def test_escape_in_overflow_menu_sets_focus_back
@@ -58,7 +61,9 @@ def test_escape_in_overflow_menu_sets_focus_back
5861
assert_equal page.evaluate_script("!!document.activeElement.closest('action-menu')"), true
5962

6063
keyboard.type(:enter)
61-
assert_equal page.evaluate_script("document.activeElement.classList.contains('ActionListContent')"), true
64+
assert_selector ".ActionListContent" do |content|
65+
page.evaluate_script("document.activeElement === arguments[0]", content)
66+
end
6267

6368
keyboard.type(:escape)
6469

@@ -75,7 +80,10 @@ def test_click_outside_of_menu_sets_tabindex_back
7580
keyboard.type(:tab, :left)
7681

7782
keyboard.type(:enter)
78-
assert_equal page.evaluate_script("document.activeElement.classList.contains('ActionListContent')"), true
83+
84+
assert_selector ".ActionListContent" do |content|
85+
page.evaluate_script("document.activeElement === arguments[0]", content)
86+
end
7987

8088
mouse.click(x: 0, y: 0)
8189
keyboard.type(:tab)

test/system/alpha/action_menu_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def open_panel_via_keyboard
7575
# open menu, "click" on first item
7676
keyboard.type(:enter)
7777
assert_selector "anchored-position:popover-open" # wait for menu to open
78+
79+
# make sure the first list item is the active element
80+
assert_selector "button[role=menuitem], button[role=menuitemradio], button[role=menuitemcheckbox]" do |button|
81+
page.evaluate_script("document.activeElement === arguments[0]", button)
82+
end
7883
end
7984
end
8085

0 commit comments

Comments
 (0)