Skip to content
Merged
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
1 change: 1 addition & 0 deletions dev/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ class DevApp extends LitElement {
label="Modal Text Input"
placeholder="Type something..."
></trailhand-text-input>
<input type="file" />
</div>
<div slot="footer">
<trailhand-button @click=${() => (this.modalOpen = false)}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@krumio/trailhand-ui",
"version": "1.9.20",
"version": "1.9.21",
"type": "module",
"description": "Reusable web components built with Lit Element",
"main": "./dist/index.js",
Expand Down
8 changes: 3 additions & 5 deletions src/components/modal/modal.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,16 @@ export const DispatchesCloseEventOnEscape: Story = {
},
args: {
open: true,
dismissible: true,
},
play: async ({ canvasElement }) => {
const modal = canvasElement.querySelector('trailhand-modal') as Modal;
const dialog = modal.shadowRoot?.querySelector(
'dialog',
) as HTMLDialogElement;

const onCloseMock = fn();
modal.addEventListener('modal-close', onCloseMock);

const cancelEvent = new Event('cancel', { bubbles: true, composed: true });
dialog.dispatchEvent(cancelEvent);
// simulate ESC key
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));

await expect(modal.open).toBe(false);
await expect(onCloseMock).toHaveBeenCalled();
Expand Down
24 changes: 23 additions & 1 deletion src/components/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ export class Modal extends LitElement {
this.open = false;
}

private handleCancel(e: Event) {
e.preventDefault();
}

private handleKeydown = (e: KeyboardEvent) => {
if (!this.open) return;

if (e.key === 'Escape' && this.dismissible) {
this.handleClose();
}
};

connectedCallback() {
super.connectedCallback();
window.addEventListener('keydown', this.handleKeydown);
}

disconnectedCallback() {
window.removeEventListener('keydown', this.handleKeydown);
super.disconnectedCallback();
}

private handleBackdropClick(e: MouseEvent) {
if (!this.dismissible) return;
const rect = this.dialog.getBoundingClientRect();
Expand All @@ -178,7 +200,7 @@ export class Modal extends LitElement {
return html`
<dialog
@click=${this.handleBackdropClick}
@cancel=${this.handleClose}
@cancel=${this.handleCancel}
aria-labelledby="modal-title"
aria-modal="true"
part="dialog"
Expand Down
Loading