forked from PolymerElements/app-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-header-layout.html
More file actions
228 lines (195 loc) · 6.34 KB
/
app-header-layout.html
File metadata and controls
228 lines (195 loc) · 6.34 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
<!--
@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">
<!--
app-header-layout is a wrapper element that positions an app-header and other content. This
element uses the document scroll by default, but it can also define its own scrolling region.
Using the document scroll:
```html
<app-header-layout>
<app-header fixed condenses effects="waterfall">
<app-toolbar>
<div main-title>App name</div>
</app-toolbar>
</app-header>
<div>
main content
</div>
</app-header-layout>
```
Using an own scrolling region:
```html
<app-header-layout has-scrolling-region style="width: 300px; height: 400px;">
<app-header fixed condenses effects="waterfall">
<app-toolbar>
<div main-title>App name</div>
</app-toolbar>
</app-header>
<div>
main content
</div>
</app-header-layout>
```
Add the `fullbleed` attribute to app-header-layout to make it fit the size of its container:
```html
<app-header-layout fullbleed>
...
</app-header-layout>
```
@group App Elements
@element app-header-layout
@demo app-header-layout/demo/simple.html Simple Demo
@demo app-header-layout/demo/scrolling-region.html Scrolling Region
@demo app-header-layout/demo/music.html Music Demo
@demo app-header-layout/demo/footer.html Footer Demo
-->
<dom-module id="app-header-layout">
<template>
<style>
:host {
display: block;
/**
* Force app-header-layout to have its own stacking context so that its parent can
* control the stacking of it relative to other elements (e.g. app-drawer-layout).
* This could be done using `isolation: isolate`, but that's not well supported
* across browsers.
*/
position: relative;
z-index: 0;
}
:host > ::content > app-header {
@apply(--layout-fixed-top);
z-index: 1;
}
:host([has-scrolling-region]) {
height: 100%;
}
:host([has-scrolling-region]) > ::content > app-header {
position: absolute;
}
:host([has-scrolling-region]) > #contentContainer {
@apply(--layout-fit);
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
:host([fullbleed]) {
@apply(--layout-vertical);
@apply(--layout-fit);
}
:host([fullbleed]) > #contentContainer {
@apply(--layout-vertical);
@apply(--layout-flex);
}
#contentContainer {
/* Create a stacking context here so that all children appear below the header. */
position: relative;
z-index: 0;
}
</style>
<content id="header" select="app-header"></content>
<div id="contentContainer">
<content></content>
</div>
</template>
<script>
Polymer({
is: 'app-header-layout',
behaviors: [
Polymer.IronResizableBehavior
],
properties: {
/**
* If true, the current element will have its own scrolling region.
* Otherwise, it will use the document scroll to control the header.
*/
hasScrollingRegion: {
type: Boolean,
value: false,
reflectToAttribute: true
}
},
listeners: {
'iron-resize': '_resizeHandler',
'app-header-reset-layout': '_resetLayoutHandler'
},
observers: [
'resetLayout(isAttached, hasScrollingRegion)'
],
/**
* A reference to the app-header element.
*
* @property header
*/
get header() {
return Polymer.dom(this.$.header).getDistributedNodes()[0];
},
/**
* Resets the layout. This method is automatically called when the element is attached
* to the DOM.
*
* @method resetLayout
*/
resetLayout: function() {
this._updateScroller();
this.debounce('_resetLayout', this._updateContentPosition);
},
_updateContentPosition: function() {
var header = this.header;
if (!this.isAttached || !header) {
return;
}
// Get header height here so that style reads are batched together before style writes
// (i.e. getBoundingClientRect() below).
var headerHeight = header.offsetHeight;
// Update the header position.
if (!this.hasScrollingRegion) {
var rect = this.getBoundingClientRect();
var rightOffset = document.documentElement.clientWidth - rect.right;
header.style.left = rect.left + 'px';
header.style.right = rightOffset + 'px';
} else {
header.style.left = '';
header.style.right = '';
}
// Update the content container position.
var containerStyle = this.$.contentContainer.style;
if (header.fixed && !header.willCondense() && this.hasScrollingRegion) {
// If the header size does not change and we're using a scrolling region, exclude
// the header area from the scrolling region so that the header doesn't overlap
// the scrollbar.
containerStyle.marginTop = headerHeight + 'px';
containerStyle.paddingTop = '';
} else {
containerStyle.paddingTop = headerHeight + 'px';
containerStyle.marginTop = '';
}
},
_updateScroller: function() {
if (!this.isAttached) {
return;
}
var header = this.header;
if (header) {
header.scrollTarget = this.hasScrollingRegion ?
this.$.contentContainer : this.ownerDocument.documentElement;
}
},
_resizeHandler: function() {
this.resetLayout();
},
_resetLayoutHandler: function(e) {
this.resetLayout();
e.stopPropagation();
}
});
</script>
</dom-module>