Skip to content

Commit

Permalink
feat(menu): avoid captured key events from navigating the whole page (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
daenub authored Jun 19, 2024
1 parent 4dfddcd commit c40fc4e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ export class LeuDropdown extends LeuElement {
}
}

async _keyUpToggleHandler(event) {
async _keyDownToggleHandler(event) {
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
event.preventDefault()
const menu = this._getMenu()
const menuItems = menu.getMenuItems()

Expand Down Expand Up @@ -144,7 +145,7 @@ export class LeuDropdown extends LeuElement {
expanded=${this.expanded ? "true" : "false"}
?active=${this.expanded}
@click=${this._handleToggleClick}
@keyup=${this._keyUpToggleHandler}
@keydown=${this._keyDownToggleHandler}
>
${hasIcon ? html`<slot name="icon" slot="before"></slot>` : nothing}
${this.label}</leu-button
Expand Down
4 changes: 4 additions & 0 deletions src/components/dropdown/stories/dropdown.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function Template({ label, expanded, icon }) {
return html` <leu-dropdown label=${label} ?expanded=${expanded}>
${icon ? html`<leu-icon name=${icon} slot="icon"></leu-icon>` : nothing}
<leu-menu>
<leu-menu-item
href="https://www.web.statistik.zh.ch/ogd/daten/ressourcen/KTZH_00001120_00002165.csv"
>OGD Ressource</leu-menu-item
>
<leu-menu-item>Als CSV Tabelle</leu-menu-item>
<leu-menu-item>Als XLS Tabelle</leu-menu-item>
<hr />
Expand Down
2 changes: 2 additions & 0 deletions src/components/menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class LeuMenu extends LeuElement {

_handleKeyDown(event) {
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
event.preventDefault()

const menuItems = this.getMenuItems()
let index = menuItems.findIndex((menuItem) => menuItem.tabIndex === 0)

Expand Down

0 comments on commit c40fc4e

Please sign in to comment.