Skip to content

Commit 673b542

Browse files
authored
Merge pull request #3397 from plotly/modebar-bg-hover-fix
Add :hover styleRule for modebar.bgcolor
2 parents 9fec03a + a4fb8cd commit 673b542

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

build/plotcss.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var rules = {
3232
"X .cursor-ne-resize": "cursor:ne-resize;",
3333
"X .cursor-grab": "cursor:-webkit-grab;cursor:grab;",
3434
"X .modebar": "position:absolute;top:2px;right:2px;z-index:1001;",
35+
"X .ease-bg": "-webkit-transition:background-color 0.3s ease 0s;-moz-transition:background-color 0.3s ease 0s;-ms-transition:background-color 0.3s ease 0s;-o-transition:background-color 0.3s ease 0s;transition:background-color 0.3s ease 0s;",
3536
"X .modebar--hover>:not(.watermark)": "opacity:0;-webkit-transition:opacity 0.3s ease 0s;-moz-transition:opacity 0.3s ease 0s;-ms-transition:opacity 0.3s ease 0s;-o-transition:opacity 0.3s ease 0s;transition:opacity 0.3s ease 0s;",
3637
"X:hover .modebar--hover .modebar-group": "opacity:1;",
3738
"X .modebar-group": "float:left;display:inline-block;box-sizing:border-box;margin-left:8px;position:relative;vertical-align:middle;white-space:nowrap;",

src/components/modebar/modebar.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,22 @@ proto.update = function(graphInfo, buttons) {
5252
this.element.setAttribute('id', modeBarId);
5353
this._uid = modeBarId;
5454

55-
if(context.displayModeBar === 'hover') {
56-
this.element.className = 'modebar modebar--hover';
57-
}
58-
else this.element.className = 'modebar';
55+
this.element.className = 'modebar';
56+
if(context.displayModeBar === 'hover') this.element.className += ' modebar--hover ease-bg';
5957

6058
if(fullLayout.modebar.orientation === 'v') {
6159
this.element.className += ' vertical';
6260
buttons = buttons.reverse();
6361
}
6462

63+
var style = fullLayout.modebar;
64+
var bgSelector = context.displayModeBar === 'hover' ? '.js-plotly-plot .plotly:hover ' : '';
65+
6566
Lib.deleteRelatedStyleRule(modeBarId);
66-
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId, 'background-color: ' + fullLayout.modebar.bgcolor);
67-
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn .icon path', 'fill: ' + fullLayout.modebar.color);
68-
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn:hover .icon path', 'fill: ' + fullLayout.modebar.activecolor);
69-
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn.active .icon path', 'fill: ' + fullLayout.modebar.activecolor);
67+
Lib.addRelatedStyleRule(modeBarId, bgSelector + '#' + modeBarId, 'background-color: ' + style.bgcolor);
68+
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn .icon path', 'fill: ' + style.color);
69+
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn:hover .icon path', 'fill: ' + style.activecolor);
70+
Lib.addRelatedStyleRule(modeBarId, '#' + modeBarId + ' .modebar-btn.active .icon path', 'fill: ' + style.activecolor);
7071

7172
// if buttons or logo have changed, redraw modebar interior
7273
var needsNewButtons = !this.hasButtons(buttons);
@@ -135,7 +136,6 @@ proto.updateButtons = function(buttons) {
135136
proto.createGroup = function() {
136137
var group = document.createElement('div');
137138
group.className = 'modebar-group';
138-
139139
return group;
140140
};
141141

src/css/_modebar.scss

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
z-index: 1001;
66
}
77

8+
.ease-bg {
9+
@include vendor('transition', background-color 0.3s ease 0s);
10+
}
11+
812
.modebar--hover > :not(.watermark) {
913
opacity: 0;
1014
@include vendor('transition', opacity 0.3s ease 0s);

test/jasmine/tests/modebar_test.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,16 @@ describe('ModeBar', function() {
12901290
expect(style.fill).toBe(color);
12911291
}
12921292

1293+
function getStyleRule() {
1294+
var uid = gd._fullLayout._uid;
1295+
var ownerNode = document.getElementById('plotly.js-style-modebar-' + uid);
1296+
var styleSheets = document.styleSheets;
1297+
for(var i = 0; i < styleSheets.length; i++) {
1298+
var ss = styleSheets[i];
1299+
if(ss.ownerNode === ownerNode) return ss;
1300+
}
1301+
}
1302+
12931303
it('create an associated style element and destroy it on purge', function(done) {
12941304
var styleSelector, style;
12951305
Plotly.plot(gd, [], {})
@@ -1313,7 +1323,7 @@ describe('ModeBar', function() {
13131323
button = selectButton(gd._fullLayout._modeBar, targetBtn);
13141324
checkButtonColor(button, colors[0]);
13151325
})
1316-
.then(function() {Plotly.relayout(gd, 'modebar.color', colors[1]);})
1326+
.then(function() { return Plotly.relayout(gd, 'modebar.color', colors[1]); })
13171327
.then(function() {
13181328
checkButtonColor(button, colors[1]);
13191329
})
@@ -1336,16 +1346,35 @@ describe('ModeBar', function() {
13361346
.then(done);
13371347
});
13381348

1339-
it('changes background color', function(done) {
1349+
it('changes background color (displayModeBar: hover)', function(done) {
13401350
Plotly.plot(gd, [], {modebar: { bgcolor: colors[0]}})
1351+
.then(function() {
1352+
style = window.getComputedStyle(gd._fullLayout._modeBar.element);
1353+
expect(style.backgroundColor).toBe('rgba(0, 0, 0, 0)');
1354+
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[0]);
1355+
})
1356+
.then(function() { return Plotly.relayout(gd, 'modebar.bgcolor', colors[1]); })
1357+
.then(function() {
1358+
style = window.getComputedStyle(gd._fullLayout._modeBar.element);
1359+
expect(style.backgroundColor).toBe('rgba(0, 0, 0, 0)');
1360+
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[1]);
1361+
})
1362+
.catch(failTest)
1363+
.then(done);
1364+
});
1365+
1366+
it('changes background color (displayModeBar: true)', function(done) {
1367+
Plotly.plot(gd, [], {modebar: {bgcolor: colors[0]}}, {displayModeBar: true})
13411368
.then(function() {
13421369
style = window.getComputedStyle(gd._fullLayout._modeBar.element);
13431370
expect(style.backgroundColor).toBe(colors[0]);
1371+
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[0]);
13441372
})
1345-
.then(function() {Plotly.relayout(gd, 'modebar.bgcolor', colors[1]);})
1373+
.then(function() { return Plotly.relayout(gd, 'modebar.bgcolor', colors[1]); })
13461374
.then(function() {
13471375
style = window.getComputedStyle(gd._fullLayout._modeBar.element);
13481376
expect(style.backgroundColor).toBe(colors[1]);
1377+
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[1]);
13491378
})
13501379
.catch(failTest)
13511380
.then(done);
@@ -1360,7 +1389,7 @@ describe('ModeBar', function() {
13601389
size = modeBarEl.getBoundingClientRect();
13611390
expect(size.width < size.height).toBeTruthy();
13621391
})
1363-
.then(function() {Plotly.relayout(gd, 'modebar.orientation', 'h');})
1392+
.then(function() { return Plotly.relayout(gd, 'modebar.orientation', 'h'); })
13641393
.catch(failTest)
13651394
.then(function() {
13661395
size = modeBarEl.getBoundingClientRect();

0 commit comments

Comments
 (0)