Skip to content

Commit fce36fa

Browse files
authored
Merge pull request #989 from plotly/fix-updatemenus-padding
Improve padding logic for updatemenus
2 parents c673afe + 4604341 commit fce36fa

File tree

7 files changed

+271
-62
lines changed

7 files changed

+271
-62
lines changed

src/components/updatemenus/attributes.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var fontAttrs = require('../../plots/font_attributes');
1212
var colorAttrs = require('../color/attributes');
1313
var extendFlat = require('../../lib/extend').extendFlat;
14+
var padAttrs = require('../../plots/pad_attributes');
1415

1516
var buttonsAttrs = {
1617
_isLinkedToArray: true,
@@ -140,6 +141,10 @@ module.exports = {
140141
].join(' ')
141142
},
142143

144+
pad: extendFlat({}, padAttrs, {
145+
description: 'Sets the padding around the buttons or dropdown menu.'
146+
}),
147+
143148
font: extendFlat({}, fontAttrs, {
144149
description: 'Sets the font of the update menu button text.'
145150
}),

src/components/updatemenus/defaults.js

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ function menuDefaults(menuIn, menuOut, layoutOut) {
6060
coerce('xanchor');
6161
coerce('yanchor');
6262

63+
coerce('pad.t');
64+
coerce('pad.r');
65+
coerce('pad.b');
66+
coerce('pad.l');
67+
6368
Lib.coerceFont(coerce, 'font', layoutOut.font);
6469

6570
coerce('bgcolor', layoutOut.paper_bgcolor);

src/components/updatemenus/draw.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function drawHeader(gd, gHeader, gButton, menuOpts) {
169169

170170
var active = menuOpts.active,
171171
headerOpts = menuOpts.buttons[active] || constants.blankHeaderOpts,
172-
posOpts = { y: 0, yPad: 0, x: 0, xPad: 0, index: 0 },
172+
posOpts = { y: menuOpts.pad.t, yPad: 0, x: menuOpts.pad.l, xPad: 0, index: 0 },
173173
positionOverrides = {
174174
width: menuOpts.headerWidth,
175175
height: menuOpts.headerHeight
@@ -191,8 +191,8 @@ function drawHeader(gd, gHeader, gButton, menuOpts) {
191191
.text('▼');
192192

193193
arrow.attr({
194-
x: menuOpts.headerWidth - constants.arrowOffsetX,
195-
y: menuOpts.headerHeight / 2 + constants.textOffsetY
194+
x: menuOpts.headerWidth - constants.arrowOffsetX + menuOpts.pad.l,
195+
y: menuOpts.headerHeight / 2 + constants.textOffsetY + menuOpts.pad.t
196196
});
197197

198198
header.on('click', function() {
@@ -275,8 +275,8 @@ function drawButtons(gd, gHeader, gButton, menuOpts) {
275275
}
276276

277277
var posOpts = {
278-
x: x0,
279-
y: y0,
278+
x: x0 + menuOpts.pad.l,
279+
y: y0 + menuOpts.pad.t,
280280
yPad: constants.gapButton,
281281
xPad: constants.gapButton,
282282
index: 0,
@@ -468,27 +468,30 @@ function findDimenstions(gd, menuOpts) {
468468

469469
fakeButtons.remove();
470470

471+
var paddedWidth = menuOpts.totalWidth + menuOpts.pad.l + menuOpts.pad.r;
472+
var paddedHeight = menuOpts.totalHeight + menuOpts.pad.t + menuOpts.pad.b;
473+
471474
var graphSize = gd._fullLayout._size;
472475
menuOpts.lx = graphSize.l + graphSize.w * menuOpts.x;
473476
menuOpts.ly = graphSize.t + graphSize.h * (1 - menuOpts.y);
474477

475478
var xanchor = 'left';
476479
if(anchorUtils.isRightAnchor(menuOpts)) {
477-
menuOpts.lx -= menuOpts.totalWidth;
480+
menuOpts.lx -= paddedWidth;
478481
xanchor = 'right';
479482
}
480483
if(anchorUtils.isCenterAnchor(menuOpts)) {
481-
menuOpts.lx -= menuOpts.totalWidth / 2;
484+
menuOpts.lx -= paddedWidth / 2;
482485
xanchor = 'center';
483486
}
484487

485488
var yanchor = 'top';
486489
if(anchorUtils.isBottomAnchor(menuOpts)) {
487-
menuOpts.ly -= menuOpts.totalHeight;
490+
menuOpts.ly -= paddedHeight;
488491
yanchor = 'bottom';
489492
}
490493
if(anchorUtils.isMiddleAnchor(menuOpts)) {
491-
menuOpts.ly -= menuOpts.totalHeight / 2;
494+
menuOpts.ly -= paddedHeight / 2;
492495
yanchor = 'middle';
493496
}
494497

@@ -500,10 +503,10 @@ function findDimenstions(gd, menuOpts) {
500503
Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index, {
501504
x: menuOpts.x,
502505
y: menuOpts.y,
503-
l: menuOpts.totalWidth * ({right: 1, center: 0.5}[xanchor] || 0),
504-
r: menuOpts.totalWidth * ({left: 1, center: 0.5}[xanchor] || 0),
505-
b: menuOpts.totalHeight * ({top: 1, middle: 0.5}[yanchor] || 0),
506-
t: menuOpts.totalHeight * ({bottom: 1, middle: 0.5}[yanchor] || 0)
506+
l: paddedWidth * ({right: 1, center: 0.5}[xanchor] || 0),
507+
r: paddedWidth * ({left: 1, center: 0.5}[xanchor] || 0),
508+
b: paddedHeight * ({top: 1, middle: 0.5}[yanchor] || 0),
509+
t: paddedHeight * ({bottom: 1, middle: 0.5}[yanchor] || 0)
507510
});
508511
}
509512

src/plots/pad_attributes.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = {
12+
t: {
13+
valType: 'number',
14+
dflt: 0,
15+
role: 'style',
16+
description: 'The amount of padding (in px) along the top of the component.'
17+
},
18+
r: {
19+
valType: 'number',
20+
dflt: 0,
21+
role: 'style',
22+
description: 'The amount of padding (in px) on the right side of the component.'
23+
},
24+
b: {
25+
valType: 'number',
26+
dflt: 0,
27+
role: 'style',
28+
description: 'The amount of padding (in px) along the bottom of the component.'
29+
},
30+
l: {
31+
valType: 'number',
32+
dflt: 0,
33+
role: 'style',
34+
description: 'The amount of padding (in px) on the left side of the component.'
35+
}
36+
};
725 Bytes
Loading

0 commit comments

Comments
 (0)