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

Simulate click handler for anchor tags in actionmenu #2199

Closed
Closed
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/seven-eagles-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/view-components': patch
---

Simular click events for Anchor tags in ActionMenu

<!-- Changed components: Primer::Alpha::ActionMenu -->
18 changes: 16 additions & 2 deletions app/components/primer/alpha/action_menu/action_menu_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export class ActionMenuElement extends HTMLElement {
const id = this.querySelector('[role=menu]')?.id
if (!id) return null
for (const el of this.querySelectorAll(`[aria-controls]`)) {
if (el.getAttribute('aria-controls') === id) return el as HTMLButtonElement
if (el.getAttribute('aria-controls') === id) {
return el as HTMLButtonElement
}
}
return null
}
Expand Down Expand Up @@ -94,7 +96,9 @@ export class ActionMenuElement extends HTMLElement {
this.#updateInput()

if (this.includeFragment) {
this.includeFragment.addEventListener('include-fragment-replaced', this, {signal})
this.includeFragment.addEventListener('include-fragment-replaced', this, {
signal
})
}
}

Expand Down Expand Up @@ -131,6 +135,16 @@ export class ActionMenuElement extends HTMLElement {
})
}

if (this.selectVariant === 'none') {
if (event instanceof KeyboardEvent && event.key === ' ' && event.target instanceof HTMLAnchorElement) {
// Simulate clicks for links if Space is pressed
const cancelled = event.target.dispatchEvent(new MouseEvent('click', {bubbles: true}))
if (!cancelled && event.target.hasAttribute('href')) {
location.href = new URL(event.target.getAttribute('href') || '', location.href).toString()
}
}
}

// The rest of the code below deals with single/multiple selection behavior, and should not
// interfere with events fired by menu items whose behavior is specified outside the library.
if (this.selectVariant !== 'multiple' && this.selectVariant !== 'single') return
Expand Down
Loading