mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 04:19:05 +08:00
34 lines
786 B
JavaScript
34 lines
786 B
JavaScript
|
import Modifier from "ember-modifier";
|
||
|
import { registerDestructor } from "@ember/destroyable";
|
||
|
import { bind } from "discourse-common/utils/decorators";
|
||
|
import { inject as service } from "@ember/service";
|
||
|
|
||
|
export default class FloatKitCloseOnEscape extends Modifier {
|
||
|
@service menu;
|
||
|
|
||
|
constructor(owner, args) {
|
||
|
super(owner, args);
|
||
|
registerDestructor(this, (instance) => instance.cleanup());
|
||
|
}
|
||
|
|
||
|
modify(element, [closeFn]) {
|
||
|
this.closeFn = closeFn;
|
||
|
this.element = element;
|
||
|
|
||
|
document.addEventListener("keydown", this.check);
|
||
|
}
|
||
|
|
||
|
@bind
|
||
|
check(event) {
|
||
|
if (event.key === "Escape") {
|
||
|
event.stopPropagation();
|
||
|
event.preventDefault();
|
||
|
this.closeFn();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cleanup() {
|
||
|
document.removeEventListener("keydown", this.check);
|
||
|
}
|
||
|
}
|