Skip to content

Commit b798d46

Browse files
authored
Merge pull request #8024 from Lexachoc/fix-modebar-hover
Fix stale modebar hover colors after style updates
2 parents bdebe0a + 78c3d74 commit b798d46

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

draftlogs/8024_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix modebar hover colors not updating after `color` or `activecolor` changes [[#8024](https://github.com/plotly/plotly.js/pull/8024)], with thanks to @Lexachoc for the contribution!

src/lib/dom.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,29 @@ function setStyleOnHover(selector, activeSelector, childSelector, activeStyle, i
109109
element = document;
110110
}
111111
element.querySelectorAll(selector).forEach(function(el) {
112+
el._hoverStyle = {
113+
activeStyleParts: activeStyleParts,
114+
inactiveStyleParts: inactiveStyleParts
115+
};
116+
112117
if(!el.getAttribute(eventAddedAttrName)) {
113118
// Emulate ":hover" CSS style using JS event handlers to set the
114119
// style in a strict CSP-compliant manner.
115120
el.addEventListener('mouseenter', function() {
121+
var hoverStyle = this._hoverStyle;
116122
var childEl = this.querySelector(childSelector);
117123
if(childEl) {
118-
childEl.style[activeStyleParts[0]] = activeStyleParts[1];
124+
childEl.style[hoverStyle.activeStyleParts[0]] = hoverStyle.activeStyleParts[1];
119125
}
120126
});
121127
el.addEventListener('mouseleave', function() {
128+
var hoverStyle = this._hoverStyle;
122129
var childEl = this.querySelector(childSelector);
123130
if(childEl) {
124131
if(activeSelector && this.matches(activeSelector)) {
125-
childEl.style[activeStyleParts[0]] = activeStyleParts[1];
132+
childEl.style[hoverStyle.activeStyleParts[0]] = hoverStyle.activeStyleParts[1];
126133
} else {
127-
childEl.style[inactiveStyleParts[0]] = inactiveStyleParts[1];
134+
childEl.style[hoverStyle.inactiveStyleParts[0]] = hoverStyle.inactiveStyleParts[1];
128135
}
129136
}
130137
});

test/jasmine/tests/modebar_test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,31 @@ describe('ModeBar', function() {
16191619
.then(done, done.fail);
16201620
});
16211621

1622+
it('changes hover icon colors', function(done) {
1623+
Plotly.newPlot(gd, [], {
1624+
modebar: {
1625+
color: colors[0],
1626+
activecolor: colors[1]
1627+
}
1628+
})
1629+
.then(function() {
1630+
button = selectButton(gd._fullLayout._modeBar, targetBtn);
1631+
1632+
// intentionally swap colors to verify hover handlers use the updated values
1633+
return Plotly.relayout(gd, {
1634+
'modebar.color': colors[1],
1635+
'modebar.activecolor': colors[0]
1636+
});
1637+
})
1638+
.then(function() {
1639+
button.node.dispatchEvent(new window.MouseEvent('mouseenter'));
1640+
checkButtonColor(button, colors[0]);
1641+
button.node.dispatchEvent(new window.MouseEvent('mouseleave'));
1642+
checkButtonColor(button, colors[1]);
1643+
})
1644+
.then(done, done.fail);
1645+
});
1646+
16221647
it('changes background color (displayModeBar: hover)', function(done) {
16231648
Plotly.newPlot(gd, [], {modebar: { bgcolor: colors[0]}})
16241649
.then(function() {

0 commit comments

Comments
 (0)