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

Commit 391479b

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(mdSubheader): Non top-level md-content causes incorrect position.
If the subheader's parent `<md-content>` was not the top-level item, the stickied subheader would be incorrectly positioned further down the page. Also added more tests to toolbar and fixed issue to ensure subheader works with a toolbar using `md-scroll-shrink`. Fixes #4420. References #2825. Closes #4439.
1 parent 32ab2eb commit 391479b

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/components/sticky/sticky.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
7070
contentEl.on('$scroll', onScroll);
7171

7272
var self;
73-
var stickyBaseoffset = contentEl.prop('offsetTop');
7473
return self = {
7574
prev: null,
7675
current: null, //the currently stickied item
@@ -86,7 +85,6 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
8685
// Add an element and its sticky clone to this content's sticky collection
8786
function add(element, stickyClone) {
8887
stickyClone.addClass('md-sticky-clone');
89-
stickyClone.css('top', stickyBaseoffset + 'px');
9088

9189
var item = {
9290
element: element,

src/components/subheader/demoBasicUsage/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
21
<div ng-controller="SubheaderAppCtrl" layout="column" flex layout-fill>
2+
3+
<md-toolbar md-scroll-shrink>
4+
<div class="md-toolbar-tools">My Messages</div>
5+
</md-toolbar>
6+
37
<md-content style="height: 600px;" md-theme="altTheme">
48

59
<section>

src/components/toolbar/toolbar.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ function mdToolbarDirective($$rAF, $mdConstant, $mdUtil, $mdTheming, $animate) {
6868

6969
$mdTheming(element);
7070

71-
setupScrollShrink();
71+
if (angular.isDefined(attr.mdScrollShrink)) {
72+
setupScrollShrink();
73+
}
7274

7375
function setupScrollShrink() {
7476

@@ -100,7 +102,7 @@ function mdToolbarDirective($$rAF, $mdConstant, $mdUtil, $mdTheming, $animate) {
100102
// If the scope is destroyed (which could happen with ng-if), make sure
101103
// to disable scroll shrinking again
102104

103-
scope.$on('$destroy', disableScrollShrink );
105+
scope.$on('$destroy', disableScrollShrink);
104106

105107
/**
106108
*
@@ -115,9 +117,15 @@ function mdToolbarDirective($$rAF, $mdConstant, $mdUtil, $mdTheming, $animate) {
115117
onMdContentLoad(null, closestContent);
116118
}
117119

118-
// Disable only if the attribute's expression evaluates to false
120+
// Evaluate the expression
121+
shrinkWithScroll = scope.$eval(shrinkWithScroll);
119122

120-
if ( shrinkWithScroll ) disableScrollShrink = enableScrollShrink();
123+
// Disable only if the attribute's expression evaluates to false
124+
if (shrinkWithScroll === false) {
125+
disableScrollShrink();
126+
} else {
127+
disableScrollShrink = enableScrollShrink();
128+
}
121129
}
122130

123131
/**
@@ -170,7 +178,7 @@ function mdToolbarDirective($$rAF, $mdConstant, $mdUtil, $mdTheming, $animate) {
170178
*
171179
*/
172180
function enableScrollShrink() {
173-
if ( !contentElement ) return angular.noop; // no md-content
181+
if (!contentElement) return angular.noop; // no md-content
174182

175183
contentElement.on('scroll', debouncedContentScroll);
176184
contentElement.attr('scroll-shrink', 'true');

src/components/toolbar/toolbar.spec.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ describe('<md-toolbar>', function() {
131131
expect($exceptionHandler.errors).toEqual([]);
132132
}));
133133

134+
it('disables scroll shrink when the attribute is not provided', inject(function() {
135+
build(
136+
'<div>' +
137+
' <md-toolbar></md-toolbar>' +
138+
' <md-content></md-content>' +
139+
'</div>'
140+
);
141+
142+
expect(element.find('md-content').attr('scroll-shrink')).toEqual(undefined);
143+
}));
144+
134145
it('enables scroll shrink when the attribute has no value', function() {
135146
build(
136147
'<div>' +
@@ -142,9 +153,37 @@ describe('<md-toolbar>', function() {
142153
expect(element.find('md-content').attr('scroll-shrink')).toEqual('true');
143154
});
144155

145-
function build(template) {
156+
it('disables scroll shrink if the expression evaluates to false', function() {
157+
var pageScope = $rootScope.$new();
158+
159+
// Set the value to false
160+
pageScope.$apply('someValue = false');
161+
162+
// Build the element
163+
build(
164+
// Pass our template
165+
'<div>' +
166+
' <md-toolbar md-scroll-shrink="someValue"></md-toolbar>' +
167+
' <md-content></md-content>' +
168+
'</div>',
169+
170+
// Pass our custom pageScope
171+
pageScope
172+
);
173+
174+
// Check that scroll shrink is disabled
175+
expect(element.find('md-content').attr('scroll-shrink')).toEqual('false');
176+
});
177+
178+
179+
function build(template, scope) {
146180
inject(function($compile) {
147-
pageScope = $rootScope.$new();
181+
if (scope) {
182+
pageScope = scope
183+
} else {
184+
pageScope = $rootScope.$new();
185+
}
186+
148187
element = $compile(template)(pageScope);
149188
controller = element.controller('mdToolbar');
150189

0 commit comments

Comments
 (0)