Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(tabs): limits the width of tabs to the width of md-tab-canvas
Browse files Browse the repository at this point in the history
Closes #3577
  • Loading branch information
Robert Messerle committed Jul 13, 2015
1 parent 2781eac commit 46ffa9e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/tabs/demoDynamicHeight/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ md-content {
md-tabs {
background: #f6f6f6;
border: 1px solid #e1e1e1;
md-tabs-canvas {
md-tabs-wrapper {
background: white;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/demoDynamicTabs/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ md-content {
md-tab-content {
background: #f6f6f6;
}
md-tabs-canvas {
md-tabs-wrapper {
background: white;
}
}
Expand Down
42 changes: 37 additions & 5 deletions src/components/tabs/js/tabsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
defineProperty('focusIndex', handleFocusIndexChange, ctrl.selectedIndex || 0);
defineProperty('offsetLeft', handleOffsetChange, 0);
defineProperty('hasContent', handleHasContent, false);
defineProperty('maxTabWidth', handleMaxTabWidth, getMaxTabWidth());
defineProperty('shouldPaginate', handleShouldPaginate, false);

//-- define boolean attributes
defineBooleanAttribute('noInkBar', handleInkBar);
Expand All @@ -39,7 +41,6 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
ctrl.lastSelectedIndex = null;
ctrl.hasFocus = false;
ctrl.lastClick = true;
ctrl.shouldPaginate = false;
ctrl.shouldCenterTabs = shouldCenterTabs();

//-- define public methods
Expand Down Expand Up @@ -74,10 +75,10 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
$mdUtil.nextTick(function () {
updateHeightFromContent();
adjustOffset();
updatePagination();
updateInkBarStyles();
ctrl.tabs[ctrl.selectedIndex] && ctrl.tabs[ctrl.selectedIndex].scope.select();
loaded = true;
updatePagination();
});
}

Expand All @@ -101,10 +102,18 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
$scope.$on('$destroy', cleanup);
}

/**
* Configure watcher(s) used by Tabs
*/
function configureWatchers () {
$scope.$watch('$mdTabsCtrl.selectedIndex', handleSelectedIndexChange);
}

/**
* Creates a one-way binding manually rather than relying on Angular's isolated scope
* @param key
* @param handler
*/
function defineOneWayBinding (key, handler) {
var attr = $attrs.$normalize('md-' + key);
if (handler) defineProperty(key, handler);
Expand Down Expand Up @@ -135,11 +144,32 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md

//-- Change handlers

/**
* Toggles stretch tabs class and updates inkbar when tab stretching changes
* @param stretchTabs
*/
function handleStretchTabs (stretchTabs) {
angular.element(elements.wrapper).toggleClass('md-stretch-tabs', shouldStretchTabs());
updateInkBarStyles();
}

function handleMaxTabWidth (newWidth, oldWidth) {
if (newWidth !== oldWidth) {
$mdUtil.nextTick(ctrl.updateInkBarStyles);
}
}

function handleShouldPaginate (newValue, oldValue) {
if (newValue !== oldValue) {
ctrl.maxTabWidth = getMaxTabWidth();
ctrl.shouldCenterTabs = shouldCenterTabs();
$mdUtil.nextTick(function () {
ctrl.maxTabWidth = getMaxTabWidth();
adjustOffset(ctrl.selectedIndex);
});
}
}

/**
* Add/remove the `md-no-tab-content` class depending on `ctrl.hasContent`
* @param hasContent
Expand Down Expand Up @@ -430,7 +460,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
*/
function shouldPaginate () {
if (ctrl.noPagination || !loaded) return false;
var canvasWidth = $element.prop('clientWidth');
var canvasWidth = Math.min($element.prop('clientWidth'), ctrl.maxTabWidth);
angular.forEach(elements.dummies, function (tab) { canvasWidth -= tab.offsetWidth; });
return canvasWidth < 0;
}
Expand Down Expand Up @@ -478,8 +508,10 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
*/
function updatePagination () {
ctrl.shouldPaginate = shouldPaginate();
ctrl.shouldCenterTabs = shouldCenterTabs();
$mdUtil.nextTick(function () { adjustOffset(ctrl.selectedIndex); });
}

function getMaxTabWidth () {
return elements.canvas.clientWidth;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/js/tabsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function MdTabs ($mdTheming, $mdUtil, $compile) {
<md-tab-item\
tabindex="-1"\
class="md-tab"\
style="max-width: {{ tabWidth ? tabWidth + \'px\' : \'none\' }}"\
style="max-width: {{ $mdTabsCtrl.maxTabWidth + \'px\' }}"\
ng-repeat="tab in $mdTabsCtrl.tabs"\
role="tab"\
aria-controls="tab-content-{{::tab.id}}"\
Expand Down
2 changes: 2 additions & 0 deletions src/components/tabs/tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ md-tab {
float: left;
font-weight: 500;
box-sizing: border-box;
overflow: hidden;
text-overflow: ellipsis;
&.md-focused {
box-shadow: none;
outline: none;
Expand Down

0 comments on commit 46ffa9e

Please sign in to comment.