Skip to content

Commit 63a274a

Browse files
committed
fix(core): a11y
- Handle key event on `drawer` & `accordion`
1 parent 2e73c7f commit 63a274a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/core/src/components/accordion/accordion.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, type ComponentInterface, Element, Host, Method, Prop, h } from '@stencil/core';
2+
import { ENTER, ESC, SPACE } from 'key-definitions';
23
import { compareOptions } from '#utils/forms';
34
import { hostContext } from '#utils/helpers';
45
import { ChevronDown } from '../ChevronDown';
@@ -105,6 +106,19 @@ export class Accordion implements ComponentInterface {
105106
this.open = !this.open;
106107
};
107108

109+
private onKeyPress = (...keys: string[]) => {
110+
return async (ev: KeyboardEvent) => {
111+
ev.preventDefault();
112+
if (!keys.includes(ev.key)) {
113+
return;
114+
}
115+
if (ev.key === ESC.key) {
116+
return this.hide();
117+
}
118+
return this.toggle();
119+
};
120+
};
121+
108122
render() {
109123
const { host } = this;
110124

@@ -117,6 +131,7 @@ export class Accordion implements ComponentInterface {
117131
<header
118132
class="accordion-title"
119133
onClick={this.onClick}
134+
onKeyUp={this.onKeyPress(SPACE.key, ENTER.key, ESC.key)}
120135
part="header"
121136
>
122137
<div class="header-content">

packages/core/src/components/drawer/drawer.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Watch,
1111
h,
1212
} from '@stencil/core';
13+
import { ESC } from 'key-definitions';
1314
import { componentConfig } from '#config';
1415
import { isRTL } from '#utils/dir';
1516
import type { OverlayInterface } from '#utils/overlay';
@@ -143,6 +144,17 @@ export class Drawer implements ComponentInterface, OverlayInterface {
143144
this.dismiss();
144145
};
145146

147+
private onKeyPress = (...keys: string[]) => {
148+
return async (ev: KeyboardEvent) => {
149+
ev.preventDefault();
150+
if (!keys.includes(ev.key)) {
151+
return;
152+
}
153+
154+
return this.dismiss();
155+
};
156+
};
157+
146158
render() {
147159
const { host } = this;
148160

@@ -161,6 +173,7 @@ export class Drawer implements ComponentInterface, OverlayInterface {
161173
<div
162174
class="drawer-backdrop"
163175
onClick={this.onClick}
176+
onKeyUp={this.onKeyPress(ESC.key)}
164177
part="backdrop"
165178
/>
166179
<div

0 commit comments

Comments
 (0)