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

Commit c1e2b12

Browse files
chmelevskijSplaktar
authored andcommitted
fix(navBar): update inkbar on screen resize
- changed how inkbar is moved - use transform to scale and translate the inkbar - also add debounced update to window resize Closes #10121
1 parent a334134 commit c1e2b12

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

src/components/navBar/navBar.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ function MdNavBar($mdAria, $mdTheming) {
104104
'<md-nav-ink-bar ng-hide="ctrl.mdNoInkBar"></md-nav-ink-bar>' +
105105
'</div>',
106106
link: function(scope, element, attrs, ctrl) {
107+
108+
ctrl.width = $window.innerWidth;
109+
110+
function onResize() {
111+
if (ctrl.width !== $window.innerWidth) {
112+
ctrl.updateSelectedTabInkBar();
113+
ctrl.width = $window.innerWidth;
114+
scope.$digest();
115+
}
116+
}
117+
118+
function cleanUp() {
119+
angular.element($window).off('resize', onResize);
120+
}
121+
122+
angular.element($window).on('resize', $mdUtil.debounce(onResize, 300));
123+
scope.$on('$destroy', cleanUp);
124+
107125
$mdTheming(element);
108126
if (!ctrl.navBarAriaLabel) {
109127
$mdAria.expectAsync(element, 'aria-label', angular.noop);
@@ -229,20 +247,28 @@ MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
229247
* Repositions the ink bar to the selected tab.
230248
* @private
231249
*/
232-
MdNavBarController.prototype._updateInkBarStyles = function(tab, newIndex, oldIndex) {
233-
this._inkbar.toggleClass('_md-left', newIndex < oldIndex)
234-
.toggleClass('_md-right', newIndex > oldIndex);
235-
250+
MdNavBarController.prototype._updateInkBarStyles = function(tab, newIndex) {
236251
this._inkbar.css({display: newIndex < 0 ? 'none' : ''});
237252

238253
if (tab) {
239254
var tabEl = tab.getButtonEl();
240255
var left = tabEl.offsetLeft;
256+
var tabWidth = tabEl.offsetWidth;
257+
var navBarWidth = this._navBarEl.getBoundingClientRect().width;
258+
var scale = tabWidth / navBarWidth;
259+
var translate = left / navBarWidth * 100;
241260

242-
this._inkbar.css({left: left + 'px', width: tabEl.offsetWidth + 'px'});
261+
this._inkbar.css({ transform: 'translateX(' + translate + '%) scaleX(' + scale + ')' });
243262
}
244263
};
245264

265+
/**
266+
* Updates inkbar to match current tab.
267+
*/
268+
MdNavBarController.prototype.updateSelectedTabInkBar = function() {
269+
this._updateInkBarStyles(this._getSelectedTab());
270+
}
271+
246272
/**
247273
* Returns an array of the current tabs.
248274
* @return {Array<!MdNavItemController>}

src/components/navBar/navBar.scss

+7-13
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@ $md-nav-bar-height: 48px;
3939
md-nav-ink-bar {
4040
$duration: $swift-ease-in-out-duration * 0.5;
4141
$multiplier: 0.5;
42+
background-color: black;
43+
position: absolute;
4244
bottom: 0;
45+
left: 0;
46+
width: 100%;
4347
height: 2px;
44-
left: auto;
45-
position: absolute;
46-
right: auto;
47-
background-color: black;
48-
49-
&._md-left {
50-
transition: left ($duration * $multiplier) $swift-ease-in-out-timing-function,
51-
right $duration $swift-ease-in-out-timing-function;
52-
}
53-
&._md-right {
54-
transition: left $duration $swift-ease-in-out-timing-function,
55-
right ($duration * $multiplier) $swift-ease-in-out-timing-function;
56-
}
48+
transform-origin: left top;
49+
will-change: transform;
50+
transition: transform ($duration * $multiplier) $swift-ease-in-out-timing-function;
5751

5852
// By default $ngAnimate looks for transition durations on the element, when using ng-hide, ng-if, ng-show.
5953
// The ink bar has a transition duration applied, which means, that $ngAnimate delays the hide process.

src/components/navBar/navBar.spec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,25 @@ describe('mdNavBar', function() {
261261
$scope.selectedTabRoute = 'tab1';
262262
createTabs();
263263

264+
var tabLeft = getTab('tab1')[0].offsetLeft;
265+
var inkbarTranslate = parseFloat(getInkbarEl().style.transform.match(/\d+\.\d+/g)[0]);
266+
var elWidth = el[0].getBoundingClientRect().width;
267+
268+
var translate = tabLeft / elWidth * 100;
264269
// b/c there is no css in the karma test, we have to interrogate the
265270
// inkbar style property directly
266-
expect(parseInt(getInkbarEl().style.left))
267-
.toBeCloseTo(getTab('tab1')[0].offsetLeft, 0.1);
271+
expect(inkbarTranslate)
272+
.toBeCloseTo(translate, 1);
268273

269274
updateSelectedTabRoute('tab3');
270275

271-
expect(parseInt(getInkbarEl().style.left))
272-
.toBeCloseTo(getTab('tab3')[0].offsetLeft, 0.1);
276+
tabLeft = getTab('tab3')[0].offsetLeft
277+
inkbarTranslate = parseFloat(getInkbarEl().style.transform.match(/\d+\.\d+/g)[0]);
278+
elWidth = el[0].getBoundingClientRect().width;
279+
translate = tabLeft / elWidth * 100;
280+
281+
expect(inkbarTranslate)
282+
.toBeCloseTo(translate, 1);
273283
});
274284

275285
it('should hide if md-no-ink-bar is enabled', function() {

0 commit comments

Comments
 (0)