-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(top-app-bar): Add --fixed variant to top app bar #2474
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e5140c8
feat(top-app-bar): Add --fixed variant to top app bar
williamernest ca4c688
feat(top-app-bar): Update for comments
williamernest e2a6008
feat(top-app-bar): Merge with master
williamernest 77e096a
feat(top-app-bar): Merge
williamernest 721df67
feat(top-app-bar): Cleanup
williamernest 68e400c
Merge branch 'master' into feat/top-app-bar/add-fixed
williamernest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {cssClasses} from '../constants'; | ||
import MDCTopAppBarAdapter from '../adapter'; | ||
import MDCTopAppBarFoundation from '../foundation'; | ||
|
||
/** | ||
* @extends {MDCTopAppBarFoundation<!MDCFixedTopAppBarFoundation>} | ||
* @final | ||
*/ | ||
class MDCFixedTopAppBarFoundation extends MDCTopAppBarFoundation { | ||
/** | ||
* @param {!MDCTopAppBarAdapter} adapter | ||
*/ | ||
constructor(adapter) { | ||
super(adapter); | ||
/** State variable for the previous scroll iteration top app bar state */ | ||
this.wasScrolled_ = false; | ||
|
||
this.scrollHandler_ = () => this.fixedScrollHandler_(); | ||
} | ||
|
||
init() { | ||
super.init(); | ||
this.adapter_.registerScrollHandler(this.scrollHandler_); | ||
} | ||
|
||
destroy() { | ||
super.destroy(); | ||
this.adapter_.deregisterScrollHandler(this.scrollHandler_); | ||
} | ||
|
||
/** | ||
* Scroll handler for applying/removing the modifier class | ||
* on the fixed top app bar. | ||
*/ | ||
fixedScrollHandler_() { | ||
const currentScroll = this.adapter_.getViewportScrollY(); | ||
|
||
if (currentScroll <= 0) { | ||
if (this.wasScrolled_) { | ||
this.adapter_.removeClass(cssClasses.FIXED_SCROLLED_CLASS); | ||
this.wasScrolled_ = false; | ||
} | ||
} else { | ||
if (!this.wasScrolled_) { | ||
this.adapter_.addClass(cssClasses.FIXED_SCROLLED_CLASS); | ||
this.wasScrolled_ = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default MDCFixedTopAppBarFoundation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,17 @@ | |
} | ||
} | ||
|
||
.mdc-top-app-bar--fixed { | ||
position: fixed; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want some kind of |
||
transition: box-shadow 200ms linear; | ||
} | ||
|
||
.mdc-top-app-bar--fixed-scrolled { | ||
@include mdc-elevation(4); | ||
|
||
transition: box-shadow 200ms linear; | ||
} | ||
|
||
// Specific styles for prominent and dense styled top app bar | ||
// stylelint-disable plugin/selector-bem-pattern | ||
.mdc-top-app-bar--dense.mdc-top-app-bar--prominent { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import td from 'testdouble'; | ||
|
||
import MDCFixedTopAppBarFoundation from '../../../packages/mdc-top-app-bar/fixed/foundation'; | ||
import MDCTopAppBarFoundation from '../../../packages/mdc-top-app-bar/foundation'; | ||
import {createMockRaf} from '../helpers/raf'; | ||
|
||
suite('MDCFixedTopAppBarFoundation'); | ||
|
||
const setupTest = () => { | ||
const mockAdapter = td.object(MDCTopAppBarFoundation.defaultAdapter); | ||
|
||
const foundation = new MDCFixedTopAppBarFoundation(mockAdapter); | ||
|
||
return {foundation, mockAdapter}; | ||
}; | ||
|
||
const createMockHandlers = (foundation, mockAdapter, mockRaf) => { | ||
let scrollHandler; | ||
td.when(mockAdapter.registerScrollHandler(td.matchers.isA(Function))).thenDo((fn) => { | ||
scrollHandler = fn; | ||
}); | ||
|
||
foundation.init(); | ||
mockRaf.flush(); | ||
td.reset(); | ||
return {scrollHandler}; | ||
}; | ||
|
||
test('fixed top app bar: scroll listener is registered on init', () => { | ||
const {foundation, mockAdapter} = setupTest(); | ||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_CLASS)).thenReturn(true); | ||
foundation.init(); | ||
td.verify(mockAdapter.registerScrollHandler(td.matchers.isA(Function)), {times: 1}); | ||
}); | ||
|
||
test('fixed top app bar: scroll listener is removed on destroy', () => { | ||
const {foundation, mockAdapter} = setupTest(); | ||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_CLASS)).thenReturn(true); | ||
foundation.init(); | ||
foundation.destroy(); | ||
td.verify(mockAdapter.deregisterScrollHandler(td.matchers.isA(Function)), {times: 1}); | ||
}); | ||
|
||
test('fixed top app bar: class is added once when page is scrolled from the top', () => { | ||
const {foundation, mockAdapter} = setupTest(); | ||
const mockRaf = createMockRaf(); | ||
|
||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_CLASS)).thenReturn(true); | ||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_SCROLLED_CLASS)).thenReturn(false); | ||
td.when(mockAdapter.getTotalActionItems()).thenReturn(0); | ||
td.when(mockAdapter.getViewportScrollY()).thenReturn(0); | ||
|
||
const {scrollHandler} = createMockHandlers(foundation, mockAdapter, mockRaf); | ||
td.when(mockAdapter.getViewportScrollY()).thenReturn(1); | ||
|
||
scrollHandler(); | ||
scrollHandler(); | ||
|
||
td.verify(mockAdapter.addClass(MDCTopAppBarFoundation.cssClasses.FIXED_SCROLLED_CLASS), {times: 1}); | ||
}); | ||
|
||
test('fixed top app bar: class is removed once when page is scrolled to the top', () => { | ||
const {foundation, mockAdapter} = setupTest(); | ||
const mockRaf = createMockRaf(); | ||
|
||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_CLASS)).thenReturn(true); | ||
td.when(mockAdapter.hasClass(MDCTopAppBarFoundation.cssClasses.FIXED_SCROLLED_CLASS)).thenReturn(false); | ||
td.when(mockAdapter.getTotalActionItems()).thenReturn(0); | ||
|
||
const {scrollHandler} = createMockHandlers(foundation, mockAdapter, mockRaf); | ||
// Apply the scrolled class | ||
td.when(mockAdapter.getViewportScrollY()).thenReturn(1); | ||
scrollHandler(); | ||
|
||
// Test removing it | ||
td.when(mockAdapter.getViewportScrollY()).thenReturn(0); | ||
scrollHandler(); | ||
scrollHandler(); | ||
|
||
td.verify(mockAdapter.removeClass(MDCTopAppBarFoundation.cssClasses.FIXED_SCROLLED_CLASS), {times: 1}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You might also want to toggle/remove
mdc-top-app-bar--fixed-scrolled
here, if it's not too hard to do.