Skip to content

Commit 54b6c84

Browse files
authored
Sticky header demo code (#6033)
* add lib files for sticky-header add chose parent add support to 'optional 'cdkStickyRegion' input ' add app-demo for sticky-header fix bugs and deleted unused tag id in HTML files modify fix some code according to PR review comments change some format to pass TSlint check add '_' before private elements delete @Injectable for StickyHeaderDirective. Because we do not need @Injectable refine code encapsulate 'set style for element' change @input() Delete 'Observable.fromEvent(this.upperScrollableContainer, 'scroll')' add const STICK_START_CLASS and STICK_END_CLASS Add doc for [cdkStickyRegion] and 'unstuckElement()'. Delete 'detach()' function, add its content into 'ngOnDestroy()'. change 'MdStickyHeaderModule' to 'CdkStickyHeaderModule'; encapsulate reset css style operation for sticky header. delete unnecessary gloable variables delete global variable '_width' Add doc for 'sticker()' function. explained how it works. add more doc for 'sticker()', explaining 'isStuck' flag 2 space for indent fix delete sticky-header demo part from this branch revert firebase file change code according to comments in PR revert firbaserc revert demo-app.ts revert routes.ts revert demo-app-module.ts change fix the problem of : 'this.stickyParent' might be 'null' change doc Change the constructor of 'cdkStickyRegion' to 'constructor(public readonly _elementRef: ElementRef) { }' Added prefix 'mat-' for CSS class Delete 'public' before variables Object.assign isn't supported in IE11; use extendObject from src/lib/core/util. IE11 will have trouble with `translate3d(0, 0, 0);', change to `translate3d(0px, 0px, 0px);' Added docs for all variables extract 'generate CSS style' created a generateStyleCSS() function, let it be responsible for generating all those CSS styles. reformat add debounce to solve 'getBoundingClientRect() cause slow down' problem. add position:sticky and check whether browser support it. If not , use the naive implementation Removed unused 'scrollableRegion' and 'parentRegion' removed commented lines default public Add comments about why setting style top and position for iPhone and not IE. And extract detectBrowser() as a new function format consider all circumstances of browser. use "===" instead of '==' make 'navigator.userAgent.toLocaleLowerCase()' a local variable optimize Added comments on const 'STICK_START_CLASS' and 'STICK_END_CLASS'. change their content to cdk-sticky-header-start and cdk-sticky-header-end Added comments for STICK_START_CLASS and STICK_END_CLASS. Changed the format of one-line JsDoc unsubscribe sbscriptions onDestory Use what modernizr does on compatibility instead of get the browser version directly. add 'padding' and 'stickyRegionHeight' variables to avoid calling 'getComputedStyle()' too many times (which is expensive). move docs above @directive removed the underscore in'_element: ElementRef', expand 'reg' to 'region' use 'if (this.isIE)' instead of 'if(this.isIE === true)' added more newlines between params in 'generateCssStyle()' function to make it easier to understand. Added reference link to Modernizer in docs of getSupportList() Deleted "_supportList" variable renamed 'isIE' to 'isStickyPositionSupported', and removed extra space before Observable Set debounce time as a const variable Added docs for 'const DEBOUNCE_TIME: number = 5;' Changed ' if(this.stickyParent == null)' to ' if(!this.stickyParent)' Removed the @param and @returns and make sure the types are correct in the function signature in 'generateCssStyle(...)' function Added docs for `isStickyPositionSupported` variable changed '+=' to '=' of 'stickyText' in getSupportList() function nit added " " between 'if' and '(' nit Added comments deleted unused import change comments optimize comments deleted unnecessary global variables(padding and stickyRegionHeight) Added check whether we are on browser Array<string> to string[] test? try to reopen the old PR fix after rebase revert list.ts test test 222 revert demo revert list.ts second time Move code to 'src/cdk' revert 'move code to 'src/cdk'' , it should be done in a new PR revert avoid calling 'getComputedStyle()' too many times. rename as sticky-header.ts Add blank lines between these top-level symbol make '_isStickyPositionSupported' private Changed the originalCSS to private and use '{} as CSSStyleDeclaration' instead of ''any. Rename '_containerStart' to '_stickyRegionTop' rename optimize discription for '_stickyRegionBottomThreshold' private _originalStyles = { position: '', top: '', right: '', left: '', bottom: '', width: '', zIndex: ''}; Deleted 'generateCssStyle()' and 'getCssNumber()' function Deleted 'getCssValue()' function fix CSSStyleDeclaration change sticky width to 'this.upperScrollableContainer.clientWidth' fix nit Added the 'isPositionStickySupported() ' function to src/cdk/platform/features.ts. Consume that function in this component and just always use both the webkit and unprefixed styles. nit update doc 'Debounce time in milliseconds for events that affect the sticky positioning (e.g. scroll, resize, touch move). Set as 5 milliseconds which is the highest delay that doesn't drastically affect the positioning adversely.' changed the doc to '/** z-index to be applied to the sticky header (default is 10). */' fix tslint error for comment 'Can you evaluate each method to make sure their accessor privacy is right? E.g. see which functions need to be public, private, static, etc' Deleted variable 'elemHeight' Chaned to 'if (!this.stickyParent)' Simplified Docs for 'sticker()'. set 'defineRestriction()' function to private use 'RxChain' rename to '_isPositionStickySupported' Use // for comments, /* */ for docs @angular/cdk rename : values -> headerStyles Move closing brace to the next line optimized: [this._onScrollSubscription, this._onScrollSubscription, this._onResizeSubscription] .forEach(s => s && s.unsubscribe()); You should be able to do just '0' instead of '0px' Format like TODO(sllethe): ... private _attachEventListeners? Add a description like "Add listeners for events that affect sticky positioning." optimize doc Rename 'defineRestrictions' to '_measureStickyRegionBounds' rename: private _resetElementStyles let stuckRight: any = this.upperScrollableContainer.getBoundingClientRect().right; chaned 'any' to 'number' nit change doc '/** * Unsticks the header so that it goes back to scrolling normally. * * This should be called when the element reaches the bottom of its cdkStickyRegion so that it * smoothly scrolls out of view as the next sticky-header moves in. */' _unstuckElement -> _unstickElement rename 'sticker()' to '_applyStickyPositionStyles()' rename 'defineRestrictionsAndStick()' to '_updateStickyPositioning()' Add demo to sticky-header add lib files for sticky-header add chose parent add support to 'optional 'cdkStickyRegion' input ' add app-demo for sticky-header fix bugs and deleted unused tag id in HTML files modify fix some code according to PR review comments change some format to pass TSlint check add '_' before private elements delete @Injectable for StickyHeaderDirective. Because we do not need @Injectable * # This is a combination of 7 commits. # This is the 1st commit message: # This is a combination of 3 commits. # This is the 1st commit message: # This is a combination of 6 commits. # This is the 1st commit message: refine code encapsulate 'set style for element' change @input() Delete 'Observable.fromEvent(this.upperScrollableContainer, 'scroll')' add const STICK_START_CLASS and STICK_END_CLASS Add doc for [cdkStickyRegion] and 'unstuckElement()'. Delete 'detach()' function, add its content into 'ngOnDestroy()'. encapsulate reset css style operation for sticky header. delete unnecessary gloable variables delete global variable '_width' Add doc for 'sticker()' function. explained how it works. add more doc for 'sticker()', explaining 'isStuck' flag 2 space for indent fix delete sticky-header demo part from this branch # This is the commit message #2: revert firebase file # This is the commit message #3: change code according to comments in PR # This is the commit message #4: revert firbaserc # This is the commit message #5: revert demo-app.ts # This is the commit message #6: revert routes.ts # This is the commit message #2: change # This is the commit message #3: fix the problem of : 'this.stickyParent' might be 'null' # This is the commit message #2: change doc # This is the commit message #3: Change the constructor of 'cdkStickyRegion' to 'constructor(public readonly _elementRef: ElementRef) { }' # This is the commit message #4: Added prefix 'mat-' for CSS class # This is the commit message #5: Delete 'public' before variables # This is the commit message #6: Object.assign isn't supported in IE11; use extendObject from src/lib/core/util. # This is the commit message #7: IE11 will have trouble with `translate3d(0, 0, 0);', change to `translate3d(0px, 0px, 0px);' * refine code encapsulate 'set style for element' change @input() Delete 'Observable.fromEvent(this.upperScrollableContainer, 'scroll')' add const STICK_START_CLASS and STICK_END_CLASS Add doc for [cdkStickyRegion] and 'unstuckElement()'. Delete 'detach()' function, add its content into 'ngOnDestroy()'. encapsulate reset css style operation for sticky header. delete unnecessary gloable variables delete global variable '_width' Add doc for 'sticker()' function. explained how it works. add more doc for 'sticker()', explaining 'isStuck' flag 2 space for indent fix delete sticky-header demo part from this branch revert firebase file change code according to comments in PR revert firbaserc revert demo-app.ts revert routes.ts change fix the problem of : 'this.stickyParent' might be 'null' change doc Change the constructor of 'cdkStickyRegion' to 'constructor(public readonly _elementRef: ElementRef) { }' Added prefix 'mat-' for CSS class Delete 'public' before variables Object.assign isn't supported in IE11; use extendObject from src/lib/core/util. IE11 will have trouble with `translate3d(0, 0, 0);', change to `translate3d(0px, 0px, 0px);' Added docs for all variables extract 'generate CSS style' created a generateStyleCSS() function, let it be responsible for generating all those CSS styles. reformat add debounce to solve 'getBoundingClientRect() cause slow down' problem. add position:sticky and check whether browser support it. If not , use the naive implementation removed unused import Removed unused 'scrollableRegion' and 'parentRegion' removed commented lines default public Add comments about why setting style top and position for iPhone and not IE. And extract detectBrowser() as a new function format consider all circumstances of browser. use "===" instead of '==' make 'navigator.userAgent.toLocaleLowerCase()' a local variable optimize Added comments on const 'STICK_START_CLASS' and 'STICK_END_CLASS'. change their content to cdk-sticky-header-start and cdk-sticky-header-end Added comments for STICK_START_CLASS and STICK_END_CLASS. Changed the format of one-line JsDoc unsubscribe sbscriptions onDestory Use what modernizr does on compatibility instead of get the browser version directly. add 'padding' and 'stickyRegionHeight' variables to avoid calling 'getComputedStyle()' too many times (which is expensive). move docs above @directive removed the underscore in'_element: ElementRef', expand 'reg' to 'region' use 'if (this.isIE)' instead of 'if(this.isIE === true)' added more newlines between params in 'generateCssStyle()' function to make it easier to understand. Added reference link to Modernizer in docs of getSupportList() Deleted "_supportList" variable renamed 'isIE' to 'isStickyPositionSupported', and removed extra space before Observable Set debounce time as a const variable Added docs for 'const DEBOUNCE_TIME: number = 5;' Changed ' if(this.stickyParent == null)' to ' if(!this.stickyParent)' Removed the @param and @returns and make sure the types are correct in the function signature in 'generateCssStyle(...)' function Added docs for `isStickyPositionSupported` variable changed '+=' to '=' of 'stickyText' in getSupportList() function nit added " " between 'if' and '(' nit Added comments deleted unused import change comments optimize comments deleted unnecessary global variables(padding and stickyRegionHeight) Added check whether we are on browser Array<string> to string[] test? try to reopen the old PR fix after rebase revert list.ts test test 222 revert demo revert list.ts second time Move code to 'src/cdk' revert 'move code to 'src/cdk'' , it should be done in a new PR revert avoid calling 'getComputedStyle()' too many times. rename as sticky-header.ts imported PlatformModule Add blank lines between these top-level symbol make '_isStickyPositionSupported' private Changed the originalCSS to private and use '{} as CSSStyleDeclaration' instead of ''any. Rename '_containerStart' to '_stickyRegionTop' rename optimize discription for '_stickyRegionBottomThreshold' private _originalStyles = { position: '', top: '', right: '', left: '', bottom: '', width: '', zIndex: ''}; Deleted 'generateCssStyle()' and 'getCssNumber()' function Deleted 'getCssValue()' function fix CSSStyleDeclaration change sticky width to 'this.upperScrollableContainer.clientWidth' fix nit Added the 'isPositionStickySupported() ' function to src/cdk/platform/features.ts. Consume that function in this component and just always use both the webkit and unprefixed styles. nit nit update doc 'Debounce time in milliseconds for events that affect the sticky positioning (e.g. scroll, resize, touch move). Set as 5 milliseconds which is the highest delay that doesn't drastically affect the positioning adversely.' changed the doc to '/** z-index to be applied to the sticky header (default is 10). */' fix tslint error for comment 'Can you evaluate each method to make sure their accessor privacy is right? E.g. see which functions need to be public, private, static, etc' Deleted variable 'elemHeight' Chaned to 'if (!this.stickyParent)' Simplified Docs for 'sticker()'. set 'defineRestriction()' function to private use 'RxChain' deleted unused 'tableModule' in modules.ts rename to '_isPositionStickySupported' Use // for comments, /* */ for docs @angular/cdk rename : values -> headerStyles Move closing brace to the next line optimized: [this._onScrollSubscription, this._onScrollSubscription, this._onResizeSubscription] .forEach(s => s && s.unsubscribe()); You should be able to do just '0' instead of '0px' Format like TODO(sllethe): ... private _attachEventListeners? Add a description like "Add listeners for events that affect sticky positioning." optimize doc Rename 'defineRestrictions' to '_measureStickyRegionBounds' rename: private _resetElementStyles let stuckRight: any = this.upperScrollableContainer.getBoundingClientRect().right; chaned 'any' to 'number' nit change doc '/** * Unsticks the header so that it goes back to scrolling normally. * * This should be called when the element reaches the bottom of its cdkStickyRegion so that it * smoothly scrolls out of view as the next sticky-header moves in. */' _unstuckElement -> _unstickElement rename 'sticker()' to '_applyStickyPositionStyles()' rename 'defineRestrictionsAndStick()' to '_updateStickyPositioning()' * Add demo to sticky-header * revert * deleted HEAD file * removed unused 'id' * changed class name to a more meaningful name * removed unused CSS * You have lots of duplicate of -webkit-appearance: none;padding: 20px;. Merge them into one * nit * nit: use header-style and header-color1 * very nit: add some different text so we can see the changes? * fix according to comments in pr * fix white space between different <div>s * P -> p
1 parent 4d31485 commit 54b6c84

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

src/demo-app/demo-app-module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ import {
7676
MdToolbarModule,
7777
MdTooltipModule,
7878
OverlayContainer,
79-
StyleModule
79+
StyleModule,
80+
StickyHeaderModule
8081
} from '@angular/material';
8182
import {CdkTableModule} from '@angular/cdk';
8283
import {TableHeaderDemo} from './table/table-header-demo';
84+
import {StickyHeaderDemo} from './sticky-header/sticky-header-demo';
8385

8486
/**
8587
* NgModule that includes all Material modules that are required to serve the demo-app.
@@ -118,7 +120,8 @@ import {TableHeaderDemo} from './table/table-header-demo';
118120
MdTooltipModule,
119121
MdNativeDateModule,
120122
CdkTableModule,
121-
StyleModule
123+
StyleModule,
124+
StickyHeaderModule
122125
]
123126
})
124127
export class DemoMaterialModule {}
@@ -184,6 +187,7 @@ export class DemoMaterialModule {}
184187
PlatformDemo,
185188
TypographyDemo,
186189
ExpansionDemo,
190+
StickyHeaderDemo
187191
],
188192
providers: [
189193
{provide: OverlayContainer, useClass: FullscreenOverlayContainer},

src/demo-app/demo-app/demo-app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export class DemoApp {
6767
{name: 'Tooltip', route: 'tooltip'},
6868
{name: 'Platform', route: 'platform'},
6969
{name: 'Style', route: 'style'},
70-
{name: 'Typography', route: 'typography'}
70+
{name: 'Typography', route: 'typography'},
71+
{name: 'Sticky Header', route: 'sticky-header'}
7172
];
7273

7374
constructor(private _element: ElementRef) {

src/demo-app/demo-app/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {DatepickerDemo} from '../datepicker/datepicker-demo';
3636
import {TableDemo} from '../table/table-demo';
3737
import {TypographyDemo} from '../typography/typography-demo';
3838
import {ExpansionDemo} from '../expansion/expansion-demo';
39+
import {StickyHeaderDemo} from '../sticky-header/sticky-header-demo';
3940

4041
export const DEMO_APP_ROUTES: Routes = [
4142
{path: '', component: Home},
@@ -74,4 +75,5 @@ export const DEMO_APP_ROUTES: Routes = [
7475
{path: 'style', component: StyleDemo},
7576
{path: 'typography', component: TypographyDemo},
7677
{path: 'expansion', component: ExpansionDemo},
78+
{path: 'sticky-header', component: StickyHeaderDemo}
7779
];
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div class="demo-sticky">
2+
<div class="demo-centered" cdk-scrollable>
3+
<div>
4+
<h1>Sticky-header Demo</h1>
5+
</div>
6+
7+
<div class="demo-container">
8+
9+
<div>
10+
<div cdkStickyHeader class="demo-header-style demo-header-color3" >
11+
<h2>Unread Messages</h2>
12+
</div>
13+
<p *ngFor="let item of items">{{item.name}} : {{item.messages}}</p>
14+
</div>
15+
16+
<div cdkStickyRegion >
17+
<div cdkStickyHeader class="demo-header-style demo-header-color2">
18+
<h2>Late Messages</h2>
19+
</div>
20+
<p *ngFor="let item of items">{{item.name}} : {{item.messages}}</p>
21+
<p *ngFor="let item of items">{{item.name}} : {{item.messages}}</p>
22+
</div>
23+
24+
<div cdkStickyRegion>
25+
<div cdkStickyHeader class="demo-header-style demo-header-color1" >
26+
<h2>Read Messages</h2>
27+
</div>
28+
<p *ngFor="let item of items">{{item.name}} : {{item.messages}}</p>
29+
<p *ngFor="let item of items">{{item.name}} : {{item.messages}}</p>
30+
</div>
31+
</div>
32+
</div>
33+
</div>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.demo-sticky {
2+
.demo-centered {
3+
text-align: left;
4+
-webkit-appearance: none;
5+
-moz-appearance: none;
6+
height: 500px;
7+
overflow: auto;}
8+
9+
button {
10+
position: fixed;
11+
margin: 16px;
12+
z-index: 99;
13+
}
14+
15+
* {
16+
margin: 0;
17+
padding: 0;
18+
}
19+
20+
body {
21+
font: 14px/1.5 Helvetica, Arial, sans-serif;
22+
-webkit-appearance: none;
23+
-moz-appearance: none;
24+
25+
}
26+
27+
h1 {
28+
font-size: 20px;
29+
padding: 20px;
30+
-webkit-appearance: none;
31+
-moz-appearance: none;
32+
}
33+
34+
h2 {
35+
font-size: 20px;
36+
padding: 20px;
37+
-webkit-appearance: none;
38+
-moz-appearance: none;
39+
}
40+
41+
p {
42+
text-indent: 4em;
43+
padding:15px;
44+
-webkit-appearance: none;
45+
-moz-appearance: none;
46+
}
47+
48+
div{
49+
margin: 0; border: 0; padding: 0;
50+
-webkit-appearance: none;
51+
-moz-appearance: none;
52+
}
53+
}
54+
55+
.mat-radio-button {
56+
display: block;
57+
}
58+
59+
.demo-container {
60+
margin: 0;
61+
-webkit-appearance: none;
62+
-moz-appearance: none;
63+
}
64+
65+
.demo-header-style{
66+
-webkit-appearance: none;
67+
padding: 20px;
68+
}
69+
70+
.demo-header-color1 {
71+
background: #cecece;
72+
}
73+
74+
.demo-header-color2 {
75+
background: #cea7a7;
76+
}
77+
78+
.demo-header-color3 {
79+
background: #c2eae5;
80+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Component} from '@angular/core';
2+
import {BrowserModule} from '@angular/platform-browser';
3+
import {CdkStickyRegion, CdkStickyHeader} from '@angular/material';
4+
5+
6+
@Component({
7+
moduleId: module.id,
8+
selector: 'sticky-header-demo',
9+
templateUrl: 'sticky-header-demo.html',
10+
styleUrls: ['sticky-header-demo.css'],
11+
})
12+
13+
export class StickyHeaderDemo {
14+
items: any[] = [
15+
{name: 'Min', messages: 'Brunch is this weekend?'},
16+
{name: 'Li', messages: 'Yes'},
17+
{name: 'Chan', messages: 'Looking'},
18+
{name: 'Chan', messages: 'Forward'},
19+
{name: 'Chan', messages: 'To It !'},
20+
{name: 'Min', messages: 'Branch is this weekend?'},
21+
{name: 'Eat', messages: 'Green Peppers'},
22+
{name: 'Chan', messages: 'Where?'},
23+
{name: 'Jack', messages: 'Pirate!'},
24+
{name: 'Jone', messages: 'Black pearl'},
25+
{name: 'Jack', messages: 'Back to the sea!'},
26+
];
27+
}

0 commit comments

Comments
 (0)