I am trying to add a class to an instance of a subclass of goog.ui.Button when the user hovers over the button.
To this end, I overrode handleMouseOver:
/**
* Handles mouseover events.
* @param {goog.events.BrowserEvent} e Mouse event to handle.
* @override
*/
myapp.ui.Button.prototype.handleMouseOver = function(e) {
goog.base(this, 'handleMouseOver', e);
goog.dom.classes.add(this.getElement(), 'button-hover-state');
};
However, when I hover over the button, the class ‘button-hover-state’ is not added to the button. Why not? Chrome Console is not issuing any errors or warnings.
I could listen to an event and respond instead of overriding handleMouseOver, but shouldn’t overriding this function alter what happens when the mouse enters the element?
The reason why your method
myapp.ui.Button.prototype.handleMouseOverdoes not do anything is because mouse events are not being dispatched.The default renderer used by
goog.ui.Buttonisgoog.ui.NativeButtonRenderer. The methodgoog.ui.NativeButtonRenderer.prototype.createDomcallsgoog.ui.NativeButtonRenderer.prototype.setUpNativeButton_, which in turn, calls:There are couple of approaches you could use to enable mouse events.
Use the default button renderer and override
goog.ui.Component.prototype.renderUse a different default renderer such as
goog.ui.ButtonRendererWhen using
goog.ui.ButtonRenderer, the CSS classgoog-button-hoveris automatically added to the button when the mouse hovers over the button and removed when the mouse leaves the button.