From 73d4fcdc1ba889d23afbd21964b66a5023669f1c Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 5 Sep 2015 03:34:40 -0700 Subject: [PATCH] feat(tooltip): hide tooltip when `esc` is hit - Hide tooltip when `esc` is hit for accessibility --- src/tooltip/test/tooltip.spec.js | 14 ++++++++++++++ src/tooltip/tooltip.js | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/src/tooltip/test/tooltip.spec.js b/src/tooltip/test/tooltip.spec.js index e8e9d068e8..bbbed52324 100644 --- a/src/tooltip/test/tooltip.spec.js +++ b/src/tooltip/test/tooltip.spec.js @@ -224,6 +224,20 @@ describe('tooltip', function() { trigger(elm, 'mouseleave'); }); + it('should close when escape is hit while open', function() { + expect(tooltipScope.isOpen).toBe(false); + + trigger(elm, 'mouseenter'); + + expect(tooltipScope.isOpen).toBe(true); + + var event = $.Event('keypress'); + event.which = 27; + elm.trigger(event); + + expect(tooltipScope.isOpen).toBe(false); + }); + describe('with specified enable expression', function() { beforeEach(inject(function($compile) { scope.enable = false; diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js index 864d861afc..745ff619b2 100644 --- a/src/tooltip/tooltip.js +++ b/src/tooltip/tooltip.js @@ -406,6 +406,12 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position']) element[0].addEventListener(trigger, showTooltipBind); element[0].addEventListener(triggers.hide[idx], hideTooltipBind); } + + element.on('keypress', function(e) { + if (e.which === 27) { + hideTooltipBind(); + } + }); }); } }