Skip to content

Commit 0e11f1a

Browse files
committed
Added close dialog on esc key
1 parent ca607d4 commit 0e11f1a

File tree

5 files changed

+49
-42
lines changed

5 files changed

+49
-42
lines changed

.github/workflows/npm-publish-github-packages.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import '@armino-dev/jquery-timed-dialog/dist/timed-dialog.min.css';
4141
### Directly into your html from cdn
4242
```html
4343
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
44-
<script src="[node_modules/@armino-dev/jquery-timed-dialog/dist/timed-dialog.min.js](https://esm.sh/@armino-dev/jquery-timed-dialog)"></script>
44+
<script src="https://esm.sh/@armino-dev/jquery-timed-dialog"></script>
4545
<link rel="stylesheet" href="https://esm.sh/@armino-dev/jquery-timed-dialog/dist/timed-dialog.min.css" />
4646
```
4747

dist/timed-dialog.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specs/timed-dialogSpec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,42 @@ describe("jQuery Timed Dialog Plugin", () => {
5656
it("Should be only one dialog", () => {
5757
expect(dialogsCount).toBe(1);
5858
});
59-
59+
60+
it ("Should close on escape key", (done) => {
61+
let dialog = $().timedDialog(options);
62+
const random = dialog.random;
63+
let event = new KeyboardEvent('keydown', {key: 'Escape'});
64+
document.dispatchEvent(event);
65+
setTimeout(() => {
66+
expect($(`#timed-dialog-${random}`).length).toBeLessThan(1);
67+
done();
68+
}, 600);
69+
});
70+
71+
it ("Should close on overlay click", (done) => {
72+
let dialog = $().timedDialog(options);
73+
const random = dialog.random;
74+
let overlay = document.querySelector(`#overlay-timed-dialog-${random}`);
75+
overlay.click();
76+
setTimeout(() => {
77+
expect($(`#timed-dialog-${random}`).length).toBeLessThan(1);
78+
done();
79+
}, 600);
80+
});
81+
82+
it ("Should contain a title", () => {
83+
let dialog = $().timedDialog(options);
84+
const random = dialog.random;
85+
let title = $(`#timed-dialog-${random} h1.title`);
86+
expect(title.text()).toBe('Test');
87+
});
88+
89+
it ("Should contain a body", () => {
90+
let dialog = $().timedDialog(options);
91+
const random = dialog.random;
92+
let body = $(`#timed-dialog-${random} div.body`);
93+
expect(body.text()).toBe('Testing the dialog');
94+
});
6095
});
6196

6297
describe ("Buttons interactions", () => {

src/timed-dialog.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@
202202
dismissDialog();
203203
}
204204
});
205+
206+
$(document).on('keydown', (evt) => {
207+
if (settings.closeOnEscape) {
208+
if (evt.key === 'Escape') {
209+
dismissDialog();
210+
}
211+
}
212+
});
205213
}
206214

207215
function animateTimeout() {

0 commit comments

Comments
 (0)