Skip to content

Commit 6b68292

Browse files
authored
Merge pull request #1257 from jasongrout/tbuttonswidth
Introduce a ToggleButtons button_width style attribute.
2 parents 71a0b23 + 82349a8 commit 6b68292

4 files changed

Lines changed: 52 additions & 6 deletions

File tree

ipywidgets/widgets/widget_selection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from .widget_description import DescriptionWidget
1717
from .valuewidget import ValueWidget
1818
from .widget_core import CoreWidget
19-
from .widget import register
19+
from .widget_style import Style
20+
from .trait_types import InstanceDict
21+
from .widget import register, widget_serialization
2022
from traitlets import (Unicode, Bool, Int, Any, Dict, TraitError, CaselessStrEnum,
2123
Tuple, List, Union, observe, validate)
2224
from ipython_genutils.py3compat import unicode_type
@@ -272,6 +274,11 @@ def _repr_keys(self):
272274
for key in sorted(chain(keys, ('options',))):
273275
yield key
274276

277+
@register
278+
class ToggleButtonsStyle(Style, CoreWidget):
279+
"""Button style widget."""
280+
_model_name = Unicode('ToggleButtonsStyleModel').tag(sync=True)
281+
button_width = Unicode(help="The width of each button.").tag(sync=True)
275282

276283
@register
277284
class ToggleButtons(_Selection):
@@ -284,6 +291,7 @@ class ToggleButtons(_Selection):
284291

285292
tooltips = List(Unicode(), help="Tooltips for each button.").tag(sync=True)
286293
icons = List(Unicode(), help="Icons names for each button (FontAwesome names without the fa- prefix).").tag(sync=True)
294+
style = InstanceDict(ToggleButtonsStyle).tag(sync=True, **widget_serialization)
287295

288296
button_style = CaselessStrEnum(
289297
values=['primary', 'success', 'info', 'warning', 'danger', ''],

packages/base/src/widget_style.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class StyleView extends WidgetView {
4141
for (let key of Object.keys(ModelType.styleProperties)) {
4242
this.registerTrait(key)
4343
}
44+
45+
// Set the initial styles
46+
this.style();
4447
}
4548

4649
/**
@@ -54,9 +57,6 @@ class StyleView extends WidgetView {
5457
this.listenTo(this.model, 'change:' + trait, (model, value) => {
5558
this.handleChange(trait, value);
5659
});
57-
58-
// Set the initial value on display.
59-
this.handleChange(trait, this.model.get(trait));
6060
}
6161

6262
/**
@@ -85,6 +85,15 @@ class StyleView extends WidgetView {
8585
}
8686
}
8787

88+
/**
89+
* Apply styles for all registered traits
90+
*/
91+
style() {
92+
for (let trait of this._traitNames) {
93+
this.handleChange(trait, this.model.get(trait));
94+
}
95+
}
96+
8897
/**
8998
* Remove the styling from the parent view.
9099
*/

packages/controls/css/widgets-base.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@
105105
/* General Button Styling */
106106

107107
.jupyter-button {
108-
padding: 0;
108+
padding-left: 10px;
109+
padding-right: 10px;
110+
padding-top: 0px;
111+
padding-bottom: 0px;
109112
display: inline-block;
110113
white-space: nowrap;
111114
overflow: hidden;

packages/controls/src/widget_selection.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4+
import {
5+
StyleModel
6+
} from '@jupyter-widgets/base';
7+
48
import {
59
CoreDescriptionModel,
610
} from './widget_core';
@@ -356,7 +360,24 @@ class RadioButtonsView extends DescriptionView {
356360
}
357361

358362
export
359-
class ToggleButtonsModel extends SelectionModel {
363+
class ToggleButtonsStyleModel extends StyleModel {
364+
defaults() {
365+
return _.extend(super.defaults(), {
366+
_model_name: 'ToggleButtonsStyleModel',
367+
});
368+
}
369+
370+
public static styleProperties = {
371+
button_width: {
372+
selector: '.widget-toggle-button',
373+
attribute: 'width',
374+
default: null
375+
}
376+
};
377+
}
378+
379+
export
380+
class ToggleButtonsModel extends SelectionModel {
360381
defaults() {
361382
return {...super.defaults(),
362383
_model_name: 'ToggleButtonsModel',
@@ -463,6 +484,11 @@ class ToggleButtonsView extends DescriptionView {
463484
}
464485
});
465486

487+
this.stylePromise.then(function(style) {
488+
if (style) {
489+
style.style();
490+
}
491+
});
466492
return super.update(options);
467493
}
468494

0 commit comments

Comments
 (0)