This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(top-app-bar): Add --fixed variant to top app bar (#2474)
- Loading branch information
1 parent
797c7e6
commit 1d40fa9
Showing
8 changed files
with
272 additions
and
21 deletions.
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
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