From 282716edf572258c3ccc304641a15d20b24c6f87 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Mon, 19 Aug 2013 10:21:44 -0700 Subject: [PATCH] made closable by ESC key --- component.json | 3 ++- index.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/component.json b/component.json index d113845..40f4ac8 100644 --- a/component.json +++ b/component.json @@ -8,7 +8,8 @@ ], "dependencies": { "component/emitter": "1.0.0", - "component/dom": "0.7.1" + "component/dom": "0.7.1", + "segmentio/on-escape": "0.0.2" }, "scripts": [ "index.js", diff --git a/index.js b/index.js index 0327f75..22ec259 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ */ var Emitter = require('emitter'); +var escape = require('on-escape'); var tmpl = require('./template'); var o = require('dom'); @@ -48,11 +49,16 @@ function overlay(options){ function Overlay(options) { Emitter.call(this); options = options || {}; + this.hidden = true; this.target = options.target || 'body'; this.closable = options.closable; this.el = o(tmpl); this.el.appendTo(this.target); - if (this.closable) this.el.on('click', this.hide.bind(this)); + this.hide = this.hide.bind(this); + if (this.closable) { + this.el.on('click', this.hide); + escape(this.hide); + } } /** @@ -72,6 +78,7 @@ Emitter(Overlay.prototype); Overlay.prototype.show = function(){ this.emit('show'); + this.hidden = false; this.el.removeClass('hide'); return this; }; @@ -86,7 +93,10 @@ Overlay.prototype.show = function(){ */ Overlay.prototype.hide = function(){ + if (this.hidden) return this; this.emit('hide'); + this.hidden = true; + escape.unbind(this.hide); return this.remove(); };