Skip to content

Commit 2d09974

Browse files
Robert MesserleJulianWielga
Robert Messerle
authored andcommitted
feat(tabs): adds support for md-center-tabs
Closes angular#1992
1 parent 6cf1061 commit 2d09974

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/components/tabs/js/tabsController.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ctrl.attachRipple = attachRipple;
2424
ctrl.shouldStretchTabs = shouldStretchTabs;
2525
ctrl.shouldPaginate = shouldPaginate;
26+
ctrl.shouldCenterTabs = shouldCenterTabs;
2627
ctrl.insertTab = insertTab;
2728
ctrl.removeTab = removeTab;
2829
ctrl.select = select;
@@ -97,7 +98,8 @@
9798
}
9899

99100
function handleOffsetChange (left) {
100-
angular.element(elements.paging).css('left', '-' + left + 'px');
101+
var newValue = shouldCenterTabs() ? '' : '-' + left + 'px';
102+
angular.element(elements.paging).css('left', newValue);
101103
$scope.$broadcast('$mdTabsPaginationChanged');
102104
}
103105

@@ -113,6 +115,7 @@
113115
}
114116

115117
function adjustOffset () {
118+
if (shouldCenterTabs()) return;
116119
var tab = elements.tabs[ctrl.focusIndex],
117120
left = tab.offsetLeft,
118121
right = tab.offsetWidth + left;
@@ -249,6 +252,10 @@
249252
}
250253
}
251254

255+
function shouldCenterTabs () {
256+
return $scope.centerTabs && !shouldPaginate();
257+
}
258+
252259
function shouldPaginate () {
253260
var canvasWidth = $element.prop('clientWidth');
254261
angular.forEach(elements.tabs, function (tab) { canvasWidth -= tab.offsetWidth; });

src/components/tabs/js/tabsDirective.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @param {string=} md-align-tabs Attribute to indicate position of tab buttons: `bottom` or `top`; default is `top`
5858
* @param {string=} md-stretch-tabs Attribute to indicate whether or not to stretch tabs: `auto`, `always`, or `never`; default is `auto`
5959
* @param {boolean=} md-dynamic-height When enabled, the tab wrapper will resize based on the contents of the selected tab
60+
* @param {boolean=} md-center-tabs When enabled, tabs will be centered provided there is no need for pagination
6061
*
6162
* @usage
6263
* <hljs lang="html">
@@ -91,6 +92,7 @@
9192
return {
9293
scope: {
9394
dynamicHeight: '=?mdDynamicHeight',
95+
centerTabs: '=?mdCenterTabs',
9496
selectedIndex: '=?mdSelected',
9597
stretchTabs: '@?mdStretchTabs'
9698
},
@@ -126,6 +128,7 @@
126128
ng-keydown="$mdTabsCtrl.keydown($event)"\
127129
role="tablist">\
128130
<md-pagination-wrapper\
131+
ng-class="{ \'md-center-tabs\': $mdTabsCtrl.shouldCenterTabs() }"\
129132
md-tab-scroll="$mdTabsCtrl.scroll($event)">\
130133
<md-tab-item\
131134
tabindex="-1"\

src/components/tabs/tabs.scss

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ md-tabs {
158158
transition: left $swift-ease-in-out-duration $swift-ease-in-out-timing-function;
159159
position: relative;
160160
width: 999999px;
161+
left: 0;
162+
transform: translate3d(0, 0, 0);
163+
&.md-center-tabs {
164+
width: initial;
165+
float: left;
166+
left: 50%;
167+
transform: translate3d(-50%, 0, 0);
168+
}
161169
}
162170
&.md-stretch-tabs {
163171
md-pagination-wrapper {

0 commit comments

Comments
 (0)