forked from PolymerElements/app-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-header.html
More file actions
715 lines (613 loc) · 21.2 KB
/
app-header.html
File metadata and controls
715 lines (613 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../app-scroll-effects/app-scroll-effects-behavior.html">
<!--
app-header is container element for app-toolbars at the top of the screen that can have scroll
effects. By default, an app-header moves away from the viewport when scrolling down and
if using `reveals`, the header slides back when scrolling back up. For example:
```html
<app-header reveals>
<app-toolbar>
<div main-title>App name</div>
</app-toolbar>
</app-header>
```
app-header can also condense when scrolling down. To achieve this behavior, the header
must have a larger height than the `sticky` element in the light DOM. For example:
```html
<app-header style="height: 96px;" condenses fixed>
<app-toolbar style="height: 64px;">
<div main-title>App name</div>
</app-toolbar>
</app-header>
```
In this case the header is initially `96px` tall, and it shrinks to `64px` when scrolling down.
That is what is meant by "condensing".
### Sticky element
The element that is positioned fixed to top of the header's `scrollTarget` when a threshold
is reached, similar to `position: sticky` in CSS. This element **must** be an immediate
child of app-header. By default, the `sticky` element is the first `app-toolbar that
is an immediate child of app-header.
```html
<app-header condenses>
<app-toolbar> Sticky element </app-toolbar>
</app-header>
```
#### Customizing the sticky element
```html
<app-header condenses>
<app-toolbar></app-toolbar>
<app-toolbar sticky> Sticky element </app-toolbar>
</app-header>
```
### Scroll target
The app-header's `scrollTarget` property allows to customize the scrollable element to which
the header responds when the user scrolls. By default, app-header uses the document as
the scroll target, but you can customize this property by setting the id of the element, e.g.
```html
<div id="scrollingRegion" style="overflow-y: auto;">
<app-header scroll-target="scrollingRegion">
</app-header>
</div>
```
In this case, the `scrollTarget` property points to the outer div element. Alternatively,
you can set this property programmatically:
```js
appHeader.scrollTarget = document.querySelector("#scrollingRegion");
```
## Backgrounds
app-header has two background layers that can be used for styling when the header is condensed
or when the scrollable element is scrolled to the top.
## Scroll effects
Scroll effects are _optional_ visual effects applied in app-header based on scroll position. For example,
The [Material Design scrolling techniques](https://www.google.com/design/spec/patterns/scrolling-techniques.html)
recommends effects that can be installed via the `effects` property. e.g.
```html
<app-header effects="waterfall">
<app-toolbar>App name</app-toolbar>
</app-header>
```
#### Importing the effects
To use the scroll effects, you must explicitly import them in addition to `app-header`:
```html
<link rel="import" href="/bower_components/app-layout/app-scroll-effects/app-scroll-effects.html">
```
#### List of effects
* **blend-background**
Fades in/out two background elements by applying CSS opacity based on scroll position.
You can use this effect to smoothly change the background color or image of the header.
For example, using the mixin `--app-header-background-rear-layer` lets you assign a different
background when the header is condensed:
```css
app-header {
background-color: red;
--app-header-background-rear-layer: {
/* The header is blue when condensed */
background-color: blue;
};
}
```
* **fade-background**
Upon scrolling past a threshold, this effect will trigger an opacity transition to
fade in/out the backgrounds. Compared to the `blend-background` effect,
this effect doesn't interpolate the opacity based on scroll position.
* **parallax-background**
A simple parallax effect that vertically translates the backgrounds based on a fraction
of the scroll position. For example:
```css
app-header {
--app-header-background-front-layer: {
background-image: url(...);
};
}
```
```html
<app-header style="height: 300px;" effects="parallax-background">
<app-toolbar>App name</app-toolbar>
</app-header>
```
The fraction determines how far the background moves relative to the scroll position.
This value can be assigned via the `scalar` config value and it is typically a value
between 0 and 1 inclusive. If `scalar=0`, the background doesn't move away from the header.
* **resize-title**
Progressively interpolates the size of the title from the element with the `main-title` attribute
to the element with the `condensed-title` attribute as the header condenses. For example:
```html
<app-header condenses reveals effects="resize-title">
<app-toolbar>
<h4 condensed-title>App name</h4>
</app-toolbar>
<app-toolbar>
<h1 main-title>App name</h1>
</app-toolbar>
</app-header>
```
* **resize-snapped-title**
Upon scrolling past a threshold, this effect fades in/out the titles using opacity transitions.
Similarly to `resize-title`, the `main-title` and `condensed-title` elements must be placed in the
light DOM.
* **waterfall**
Toggles the shadow property in app-header to create a sense of depth (as recommended in the
MD spec) between the header and the underneath content. You can change the shadow by
customizing the `--app-header-shadow` mixin. For example:
```css
app-header {
--app-header-shadow: {
box-shadow: inset 0px 5px 2px -3px rgba(0, 0, 0, 0.2);
};
}
```
```html
<app-header condenses reveals effects="waterfall">
<app-toolbar>
<h1 main-title>App name</h1>
</app-toolbar>
</app-header>
```
* **material**
Installs the waterfall, resize-title, blend-background and parallax-background effects.
### Content attributes
Attribute | Description | Default
----------|---------------------|----------------------------------------
`sticky` | Element that remains at the top when the header condenses. | The first app-toolbar in the light DOM.
## Styling
Mixin | Description | Default
------|-------------|----------
`--app-header-background-front-layer` | Applies to the front layer of the background. | {}
`--app-header-background-rear-layer` | Applies to the rear layer of the background. | {}
`--app-header-shadow` | Applies to the shadow. | {}
@group App Elements
@element app-header
@demo app-header/demo/blend-background-1.html Blend Background Image
@demo app-header/demo/blend-background-2.html Blend 2 Background Images
@demo app-header/demo/blend-background-3.html Blend Background Colors
@demo app-header/demo/contacts.html Contacts Demo
@demo app-header/demo/give.html Resize Snapped Title Demo
@demo app-header/demo/music.html Reveals Demo
@demo app-header/demo/no-effects.html Condenses and Reveals Demo
@demo app-header/demo/notes.html Fixed with Dynamic Shadow Demo
@demo app-header/demo/custom-sticky-element-1.html Custom Sticky Element Demo 1
@demo app-header/demo/custom-sticky-element-2.html Custom Sticky Element Demo 2
-->
<dom-module id="app-header">
<template>
<style>
:host {
position: relative;
display: block;
transition-timing-function: linear;
transition-property: -webkit-transform;
transition-property: transform;
}
:host::before {
position: absolute;
right: 0px;
bottom: -5px;
left: 0px;
width: 100%;
height: 5px;
content: "";
transition: opacity 0.4s;
pointer-events: none;
opacity: 0;
box-shadow: inset 0px 5px 6px -3px rgba(0, 0, 0, 0.4);
will-change: opacity;
@apply(--app-header-shadow);
}
:host([shadow])::before {
opacity: 1;
}
::content [main-title],
::content [condensed-title] {
-webkit-transform-origin: left top;
transform-origin: left top;
white-space: nowrap;
}
::content [condensed-title] {
opacity: 0;
}
#background {
@apply(--layout-fit);
overflow: hidden;
}
#backgroundFrontLayer,
#backgroundRearLayer {
@apply(--layout-fit);
height: 100%;
pointer-events: none;
background-size: cover;
}
#backgroundFrontLayer {
@apply(--app-header-background-front-layer);
}
#backgroundRearLayer {
opacity: 0;
@apply(--app-header-background-rear-layer);
}
#contentContainer {
position: relative;
width: 100%;
height: 100%;
}
:host([disabled]),
:host([disabled])::after,
:host([disabled]) #backgroundFrontLayer,
:host([disabled]) #backgroundRearLayer,
:host([disabled]) ::content > app-toolbar:first-of-type,
:host([disabled]) ::content > [sticky],
/* Silent scrolling should not run CSS transitions */
:host-context(.app-layout-silent-scroll),
:host-context(.app-layout-silent-scroll)::after,
:host-context(.app-layout-silent-scroll) #backgroundFrontLayer,
:host-context(.app-layout-silent-scroll) #backgroundRearLayer,
:host-context(.app-layout-silent-scroll) ::content > app-toolbar:first-of-type,
:host-context(.app-layout-silent-scroll) ::content > [sticky] {
transition: none !important;
}
</style>
<div id="contentContainer">
<content id="content"></content>
</div>
</template>
<script>
Polymer({
is: 'app-header',
behaviors: [
Polymer.AppScrollEffectsBehavior,
Polymer.IronResizableBehavior
],
properties: {
/**
* If true, the header will automatically collapse when scrolling down.
* That is, the `sticky` element remains visible when the header is fully condensed
* whereas the rest of the elements will collapse below `sticky` element.
*
* By default, the `sticky` element is the first toolbar in the light DOM:
*
*```html
* <app-header condenses>
* <app-toolbar>This toolbar remains on top</app-toolbar>
* <app-toolbar></app-toolbar>
* <app-toolbar></app-toolbar>
* </app-header>
* ```
*
* Additionally, you can specify which toolbar or element remains visible in condensed mode
* by adding the `sticky` attribute to that element. For example: if we want the last
* toolbar to remain visible, we can add the `sticky` attribute to it.
*
*```html
* <app-header condenses>
* <app-toolbar></app-toolbar>
* <app-toolbar></app-toolbar>
* <app-toolbar sticky>This toolbar remains on top</app-toolbar>
* </app-header>
* ```
*
* Note the `sticky` element must be a direct child of `app-header`.
*/
condenses: {
type: Boolean,
value: false
},
/**
* Mantains the header fixed at the top so it never moves away.
*/
fixed: {
type: Boolean,
value: false
},
/**
* Slides back the header when scrolling back up.
*/
reveals: {
type: Boolean,
value: false
},
/**
* Displays a shadow below the header.
*/
shadow: {
type: Boolean,
reflectToAttribute: true,
value: false
}
},
observers: [
'resetLayout(isAttached, condenses, fixed)'
],
listeners: {
'iron-resize': '_resizeHandler'
},
/**
* A cached offsetHeight of the current element.
*
* @type {number}
*/
_height: 0,
/**
* The distance in pixels the header will be translated to when scrolling.
*
* @type {number}
*/
_dHeight: 0,
/**
* The offsetTop of `_stickyEl`
*
* @type {number}
*/
_stickyElTop: 0,
/**
* The element that remains visible when the header condenses.
*
* @type {HTMLElement}
*/
_stickyEl: null,
/**
* The header's top value used for the `transformY`
*
* @type {number}
*/
_top: 0,
/**
* The current scroll progress.
*
* @type {number}
*/
_progress: 0,
_wasScrollingDown: false,
_initScrollTop: 0,
_initTimestamp: 0,
_lastTimestamp: 0,
_lastScrollTop: 0,
/**
* The distance the header is allowed to move away.
*
* @type {number}
*/
get _maxHeaderTop() {
return this.fixed ? this._dHeight : this._height + 5;
},
/**
* Returns a reference to the sticky element.
*
* @return {HTMLElement}?
*/
_getStickyEl: function() {
/** @type {HTMLElement} */
var stickyEl;
var nodes = Polymer.dom(this.$.content).getDistributedNodes();
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType === Node.ELEMENT_NODE) {
var node = /** @type {HTMLElement} */ (nodes[i]);
if (node.hasAttribute('sticky')) {
stickyEl = node;
break;
} else if (!stickyEl) {
stickyEl = node;
}
}
}
return stickyEl;
},
/**
* Resets the layout. If you changed the size of app-header via CSS
* you can notify the changes by either firing the `iron-resize` event
* or calling `resetLayout` directly.
*
* @method resetLayout
*/
resetLayout: function() {
this.debounce('_resetLayout', function() {
// noop if the header isn't visible
if (this.offsetWidth === 0 && this.offsetHeight === 0) {
return;
}
var scrollTop = this._clampedScrollTop;
var firstSetup = this._height === 0 || scrollTop === 0;
var currentDisabled = this.disabled;
this._height = this.offsetHeight;
this._stickyEl = this._getStickyEl();
this.disabled = true;
// prepare for measurement
if (!firstSetup) {
this._updateScrollState(0, true);
}
if (this._mayMove()) {
this._dHeight = this._stickyEl ? this._height - this._stickyEl.offsetHeight : 0;
} else {
this._dHeight = 0;
}
this._stickyElTop = this._stickyEl ? this._stickyEl.offsetTop : 0;
this._setUpEffect();
if (firstSetup) {
this._updateScrollState(scrollTop, true);
} else {
this._updateScrollState(this._lastScrollTop, true);
this._layoutIfDirty();
}
// restore no transition
this.disabled = currentDisabled;
this.fire('app-header-reset-layout');
});
},
/**
* Updates the scroll state.
*
* @param {number} scrollTop
* @param {boolean=} forceUpdate (default: false)
*/
_updateScrollState: function(scrollTop, forceUpdate) {
if (this._height === 0) {
return;
}
var progress = 0;
var top = 0;
var lastTop = this._top;
var lastScrollTop = this._lastScrollTop;
var maxHeaderTop = this._maxHeaderTop;
var dScrollTop = scrollTop - this._lastScrollTop;
var absDScrollTop = Math.abs(dScrollTop);
var isScrollingDown = scrollTop > this._lastScrollTop;
var now = performance.now();
if (this._mayMove()) {
top = this._clamp(this.reveals ? lastTop + dScrollTop : scrollTop, 0, maxHeaderTop);
}
if (scrollTop >= this._dHeight) {
top = this.condenses && !this.fixed ? Math.max(this._dHeight, top) : top;
this.style.transitionDuration = '0ms';
}
if (this.reveals && !this.disabled && absDScrollTop < 100) {
// set the initial scroll position
if (now - this._initTimestamp > 300 || this._wasScrollingDown !== isScrollingDown) {
this._initScrollTop = scrollTop;
this._initTimestamp = now;
}
if (scrollTop >= maxHeaderTop) {
// check if the header is allowed to snap
if (Math.abs(this._initScrollTop - scrollTop) > 30 || absDScrollTop > 10) {
if (isScrollingDown && scrollTop >= maxHeaderTop) {
top = maxHeaderTop;
} else if (!isScrollingDown && scrollTop >= this._dHeight) {
top = this.condenses && !this.fixed ? this._dHeight : 0;
}
var scrollVelocity = dScrollTop / (now - this._lastTimestamp);
this.style.transitionDuration = this._clamp((top - lastTop) / scrollVelocity, 0, 300) + 'ms';
} else {
top = this._top;
}
}
}
if (this._dHeight === 0) {
progress = scrollTop > 0 ? 1 : 0;
} else {
progress = top / this._dHeight;
}
if (!forceUpdate) {
this._lastScrollTop = scrollTop;
this._top = top;
this._wasScrollingDown = isScrollingDown;
this._lastTimestamp = now;
}
if (forceUpdate || progress !== this._progress || lastTop !== top || scrollTop === 0) {
this._progress = progress;
this._runEffects(progress, top);
this._transformHeader(top);
}
},
/**
* Returns true if the current header is allowed to move as the user scrolls.
*
* @return {boolean}
*/
_mayMove: function() {
return this.condenses || !this.fixed;
},
/**
* Returns true if the current header will condense based on the size of the header
* and the `consenses` property.
*
* @return {boolean}
*/
willCondense: function() {
return this._dHeight > 0 && this.condenses;
},
/**
* Returns true if the current element is on the screen.
* That is, visible in the current viewport.
*
* @method isOnScreen
* @return {boolean}
*/
isOnScreen: function() {
return this._height !== 0 && this._top < this._height;
},
/**
* Returns true if there's content below the current element.
*
* @method isContentBelow
* @return {boolean}
*/
isContentBelow: function() {
if (this._top === 0) {
return this._clampedScrollTop > 0;
}
return this._clampedScrollTop - this._maxHeaderTop >= 0;
},
/**
* Transforms the header.
*
* @param {number} y
*/
_transformHeader: function(y) {
this.translate3d(0, (-y) + 'px', 0);
if (this._stickyEl) {
this.translate3d(0, this.condenses && y >= this._stickyElTop ?
(Math.min(y, this._dHeight) - this._stickyElTop) + 'px' : 0, 0, this._stickyEl);
}
},
_resizeHandler: function() {
this.resetLayout();
},
_clamp: function(v, min, max) {
return Math.min(max, Math.max(min, v));
},
_ensureBgContainers: function() {
if (!this._bgContainer) {
this._bgContainer = document.createElement('div');
this._bgContainer.id = 'background';
this._bgRear = document.createElement('div');
this._bgRear.id = 'backgroundRearLayer';
this._bgContainer.appendChild(this._bgRear);
this._bgFront = document.createElement('div');
this._bgFront.id = 'backgroundFrontLayer';
this._bgContainer.appendChild(this._bgFront);
Polymer.dom(this.root).insertBefore(this._bgContainer, this.$.contentContainer);
}
},
_getDOMRef: function(id) {
switch (id) {
case 'backgroundFrontLayer':
this._ensureBgContainers();
return this._bgFront;
case 'backgroundRearLayer':
this._ensureBgContainers();
return this._bgRear;
case 'background':
this._ensureBgContainers();
return this._bgContainer;
case 'mainTitle':
return Polymer.dom(this).querySelector('[main-title]');
case 'condensedTitle':
return Polymer.dom(this).querySelector('[condensed-title]');
}
return null;
},
/**
* Returns an object containing the progress value of the scroll effects
* and the top position of the header.
*
* @method getScrollState
* @return {Object}
*/
getScrollState: function() {
return { progress: this._progress, top: this._top };
}
/**
* Fires when the layout of `app-header` changed.
*
* @event app-header-reset-layout
*/
});
</script>
</dom-module>