Skip to content

Commit baf609f

Browse files
feat(elements): close drawer when pressing Escape
Related to #3370
1 parent 811f49e commit baf609f

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

packages/elements/src/components/drawer/drawer.component.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ResizeController } from '@lit-labs/observers/resize-controller.js';
22
import { html, LitElement, nothing, unsafeCSS } from 'lit';
33
import { customElement, property } from 'lit/decorators.js';
4+
import { classMap } from 'lit/directives/class-map.js';
45
import type { Ref } from 'lit/directives/ref.js';
56
import { createRef, ref } from 'lit/directives/ref.js';
67
import { styleMap } from 'lit/directives/style-map.js';
@@ -63,9 +64,25 @@ export class MutationTestReportDrawer extends LitElement {
6364
event.stopImmediatePropagation();
6465
};
6566

67+
connectedCallback(): void {
68+
super.connectedCallback();
69+
window.addEventListener('keydown', this.#handleKeyDown);
70+
}
71+
72+
disconnectedCallback(): void {
73+
window.removeEventListener('keydown', this.#handleKeyDown);
74+
super.disconnectedCallback();
75+
}
76+
77+
#handleKeyDown = (event: KeyboardEvent) => {
78+
if (event.key === 'Escape') {
79+
this.mode = 'closed';
80+
}
81+
};
82+
6683
render() {
67-
const contentHeight = this.#contentHeightController.value;
68-
const styles = styleMap({ height: contentHeight ? `${contentHeight}px` : undefined });
84+
const isOpen = this.mode === 'open';
85+
const height = this.#contentHeightController.value;
6986

7087
return html`<aside @click="${(event: Event) => event.stopPropagation()}" class="ml-6 mr-4">
7188
<header class="w-full py-4" ${ref(this.#headerRef)}>
@@ -77,7 +94,10 @@ export class MutationTestReportDrawer extends LitElement {
7794
)}
7895
</h2>
7996
</header>
80-
<div style="${styles}" class="mb-4 overflow-y-auto motion-safe:transition-max-width">
97+
<div
98+
style="${height && isOpen ? `height: ${height}px;` : nothing}"
99+
class="${classMap({ ['mb-4 motion-safe:transition-max-width']: true, 'overflow-y-auto': isOpen })}"
100+
>
81101
<slot name="summary"></slot>
82102
${renderIf(this.hasDetail && this.mode === 'open', html`<slot name="detail"></slot>`)}
83103
</div>

packages/elements/test/unit/components/drawer.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MutationTestReportDrawer } from '../../../src/components/drawer/drawer.component.js';
22
import { CustomElementFixture } from '../helpers/CustomElementFixture.js';
3+
import { userEvent } from '@vitest/browser/context';
34

45
describe(MutationTestReportDrawer.name, () => {
56
let sut: CustomElementFixture<MutationTestReportDrawer>;
@@ -72,6 +73,16 @@ describe(MutationTestReportDrawer.name, () => {
7273
const event = await sut.catchNativeEvent('click', () => sut.$<HTMLElement>('header').click());
7374
expect(event).undefined;
7475
});
76+
77+
it('should close when escape is pressed', async () => {
78+
sut.element.mode = 'open';
79+
await sut.whenStable();
80+
81+
await userEvent.keyboard('{Escape}');
82+
await sut.whenStable();
83+
84+
expect(sut.element).toHaveProperty('mode', 'closed');
85+
});
7586
});
7687

7788
function readMoreToggle(): HTMLElement {

0 commit comments

Comments
 (0)