Skip to content

Commit eff56fc

Browse files
committed
improve fallback for button 'title':
- don't show hover text when 'title' is set to null or false or '' - if undefined, fall back to 'name'
1 parent 2934f11 commit eff56fc

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/components/modebar/buttons.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var modebarButtons = module.exports = {};
2323
* @param {string} name
2424
* name / id of the buttons (for tracking)
2525
* @param {string} title
26-
* text that appears while hovering over the button
26+
* text that appears while hovering over the button,
27+
* enter null, false or '' for no hover text
2728
* @param {string} icon
2829
* svg icon associated with the button
2930
* @param {string} [gravity]
@@ -68,7 +69,6 @@ modebarButtons.toImage = {
6869
filename += '.' + format;
6970

7071
ev.once('success', function(result) {
71-
7272
gd._snapshotInProgress = false;
7373

7474
var downloadLink = document.createElement('a');

src/components/modebar/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ proto.createButton = function (config) {
121121
button.setAttribute('rel', 'tooltip');
122122
button.className = 'modebar-btn';
123123

124-
button.setAttribute('data-title', config.title || '');
125-
button.setAttribute('data-gravity', config.gravity || 'n');
124+
var title = config.title;
125+
if(title !== null && title !== false && title !== '') {
126+
button.setAttribute('data-title', title || config.name);
127+
}
126128

127129
if(config.attr !== undefined) button.setAttribute('data-attr', config.attr);
128130

@@ -147,6 +149,7 @@ proto.createButton = function (config) {
147149
if(config.toggle) button.classList.add('active');
148150

149151
button.appendChild(this.createIcon(config.icon || Icons.tooltip_basic));
152+
button.setAttribute('data-gravity', config.gravity || 'n');
150153

151154
return button;
152155
};

test/jasmine/tests/modebar_test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var manageModebar = require('@src/components/modebar/manage');
77
describe('Modebar', function() {
88
'use strict';
99

10-
function noop() {};
10+
function noop() {}
1111

1212
function getMockContainerTree() {
1313
var root = document.createElement('div');
@@ -45,6 +45,10 @@ describe('Modebar', function() {
4545
return d3.select(modebar.element).selectAll('a.plotlyjsicon')[0].length;
4646
}
4747

48+
function checkBtnAttr(modebar, index, attr) {
49+
var buttons = d3.select(modebar.element).selectAll('a.modebar-btn');
50+
return d3.select(buttons[0][index]).attr(attr);
51+
}
4852

4953
var buttons = [[{
5054
name: 'button 1',
@@ -88,6 +92,32 @@ describe('Modebar', function() {
8892
}).toThrowError();
8993
});
9094

95+
it('defaults title to name when missing', function() {
96+
var modebar = createModebar(getMockGraphInfo(), [[
97+
{ name: 'the title too', click: noop }
98+
]]);
99+
100+
expect(checkBtnAttr(modebar, 0, 'data-title')).toEqual('the title too');
101+
});
102+
103+
it('hides title to when title is set to null or \'\' or false', function() {
104+
var modebar;
105+
106+
modebar = createModebar(getMockGraphInfo(), [[
107+
{ name: 'button', title: null, click: noop }
108+
]]);
109+
expect(checkBtnAttr(modebar, 0, 'data-title')).toBe(null);
110+
111+
modebar = createModebar(getMockGraphInfo(), [[
112+
{ name: 'button', title: '', click: noop }
113+
]]);
114+
expect(checkBtnAttr(modebar, 0, 'data-title')).toBe(null);
115+
116+
modebar = createModebar(getMockGraphInfo(), [[
117+
{ name: 'button', title: false, click: noop }
118+
]]);
119+
expect(checkBtnAttr(modebar, 0, 'data-title')).toBe(null);
120+
});
91121
});
92122

93123
describe('modebar.removeAllButtons', function() {

0 commit comments

Comments
 (0)