Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit e73d290

Browse files
erwinmombayThomasBurleson
authored andcommitted
fix(tooltip): hide tooltip after mouseleave if focus is achieved through mousedown
1 parent 518237d commit e73d290

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/components/tooltip/tooltip.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,16 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
122122

123123
function bindEvents () {
124124
var autohide = scope.hasOwnProperty('autohide') ? scope.autohide : attr.hasOwnProperty('mdAutohide');
125+
var mouseActive = false;
126+
// to avoid `synthetic clicks` we listen to mousedown instead of `click`
127+
parent.on('mousedown', function() { mouseActive = true; });
125128
parent.on('focus mouseenter touchstart', function() { setVisible(true); });
126-
parent.on('blur mouseleave touchend touchcancel', function() { if ($document[0].activeElement !== parent[0] || autohide) setVisible(false); });
129+
parent.on('blur mouseleave touchend touchcancel', function() {
130+
if ($document[0].activeElement !== parent[0] || autohide || mouseActive) {
131+
setVisible(false);
132+
}
133+
mouseActive = false;
134+
});
127135
angular.element($window).on('resize', debouncedOnResize);
128136
}
129137

src/components/tooltip/tooltip.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,35 @@ describe('<md-tooltip> directive', function() {
154154

155155
}));
156156

157+
it('should not be visible on mousedown and then mouseleave', inject(function($rootScope, $compile, $timeout, $document) {
158+
jasmine.mockElementFocus(this);
159+
160+
var element = $compile('<md-button>' +
161+
'Hello' +
162+
'<md-tooltip md-visible="isVisible">Tooltip</md-tooltip>' +
163+
'</md-button>')($rootScope);
164+
165+
$rootScope.$apply();
166+
167+
// this focus is needed to set `$document.activeElement`
168+
// and wouldn't be required if `document.activeElement` was settable.
169+
element.focus();
170+
element.triggerHandler('focus');
171+
element.triggerHandler('mousedown');
172+
$timeout.flush();
173+
174+
expect($document.activeElement).toBe(element[0]);
175+
expect($rootScope.isVisible).toBe(true);
176+
177+
element.triggerHandler('mouseleave');
178+
$timeout.flush();
179+
180+
// very weak test since this is really always set to false because
181+
// we are not able to set `document.activeElement` to the parent
182+
// of `md-tooltip`. we compensate by testing `$document.activeElement`
183+
// which sort of mocks the behavior through `jasmine.mockElementFocus`
184+
// which should be replaced by a true `document.activeElement` check
185+
// if the problem gets fixed.
186+
expect($rootScope.isVisible).toBe(false);
187+
}));
157188
});

0 commit comments

Comments
 (0)