forked from googlearchive/core-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-layout.html
More file actions
288 lines (234 loc) · 8.35 KB
/
core-layout.html
File metadata and controls
288 lines (234 loc) · 8.35 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
<!--
Copyright (c) 2014 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
-->
<!--
The `core-layout` element is a helper for using
[CSS3 Flexible Boxes](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes).
A `core-layout` element enables a set of css selectors for easy flexbox styling.
Example:
<core-layout>
<div>Left</div>
<div core-flex>Main</div>
<div>Right</div>
</core-layout>
Renders something like this:
---------------------------------
|-------------------------------|
||Left| Main |Right||
|-------------------------------|
---------------------------------
__Note__: the `core-layout` element applies layout to itself if it has children or to
its parent element, if it does not. This feature allows you to apply layout to an
arbitrary parent.
Elements can layout horizontally, such that their items stack
left to right or vertically, such that their items stack top to bottom. The
default is horizontal. Set `vertical` to true to layout the elements vertically.
To make a particular child _flexible_, use the `core-flex` attribute.
You can control flexibility from 1 to 3 by giving the attribute a
corresponding value. For other values, apply the css manually.
It's common in flexbox parlance to hear the terms _main axis_ and _cross axis_.
For a horizontal layout the main axis is horizontal and the cross axis is vertical.
These are exchanged for a vertical layout.
To effect alignment in the main axis, use the `justify` attribute. The
supported values are `start`, `center`, `end`, and `between`.
To effect alignment in the cross axis, use the `align` attribute. The
supported values are `start`, `center`, and `end`.
Note, it's also possible to include the `core-layout.css` stylesheet separate
from the `core-layout` element. Including the element automatically includes
the stylesheet. To use the stylesheet independent of the element, css classes
should be used of the following form: `core-h`, `core-v`, `core-flex`,
`core-justify-start`, and `core-align-start`.
The `core-layout` and css file also provide a few commonly needed layout
behaviors. Apply the `core-fit` class to fit an element to its container. To
ensure a container will contain an element inside it with the `core-fit` class
give it the `core-relative` class.
More examples:
<core-layout vertical>
<div>Header</div>
<div core-flex>Body</div>
<div>Footer</div>
</core-layout>
----------
||------||
||Header||
||------||
||Body ||
|| ||
|| ||
|| ||
|| ||
|| ||
|| ||
||------||
||Footer||
||------||
----------
Justify:
<core-layout justify="end">
<div core-flex>Left</div>
<div>Main</div>
<div>Right</div>
</core-layout>
---------------------------------
|-------------------------------|
|| Left|Main|Right||
|-------------------------------|
---------------------------------
Align:
<core-layout align="center">
<div>Left</div>
<div core-flex>Main</div>
<div>Right</div>
</core-layout>
---------------------------------
|-------------------------------|
|| | | ||
||Left| Main |Right||
|| | | ||
|-------------------------------|
---------------------------------
To layout contents of a parent element, place a `core-layout` inside of it:
<some-element>
<core-layout></core-layout>
<div>Left</div>
<div core-flex>Main</div>
<div>Right</div>
<some-element>
---------------------------------
|-------------------------------|
||Left| Main |Right||
|-------------------------------|
---------------------------------
You may also use the `core-layout` stylesheet directly:
<link rel="stylesheet" href="../core-layout/core-layout.css">
<div class="core-h core-justify-end">
<div core-flex>Left</div>
<div>Main</div>
<div>Right</div>
</div>
---------------------------------
|-------------------------------|
|| Left|Main|Right||
|-------------------------------|
---------------------------------
@group Polymer Core Elements
@element core-layout
-->
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="core-layout" attributes="vertical justify align isContainer reverse">
<template>
<link no-shim rel="stylesheet" href="core-layout.css">
<link no-shim rel="stylesheet" href="core-layout-host.css">
</template>
<script>
(function() {
Polymer('core-layout', {
isContainer: false,
/**
* Controls if the element lays out vertically or not.
*
* @attribute vertical
* @type boolean
* @default false
*/
vertical: false,
/**
* Controls how the items are aligned in the main-axis direction. For
* example for a horizontal layout, this controls how each item is aligned
* horizontally.
*
* @attribute justify
* @type string start|center|end|between
* @default ''
*/
justify: '',
/**
* Controls how the items are aligned in cross-axis direction. For
* example for a horizontal layout, this controls how each item is aligned
* vertically.
*
* @attribute align
* @type string start|center|end
* @default ''
*/
align: '',
/**
* Controls whether or not the items layout in reverse order.
*
* @attribute reverse
* @type boolean
* @default false
*/
reverse: false,
layoutPrefix: 'core-',
// NOTE: include template so that styles are loaded, but remove
// so that we can decide dynamically what part to include
registerCallback: function(polymerElement) {
var template = polymerElement.querySelector('template');
this.styles = template.content.querySelectorAll('style').array();
this.styles.forEach(function(s) {
s.removeAttribute('no-shim');
})
},
fetchTemplate: function() {
return null;
},
attached: function() {
this.installScopeStyle(this.styles[0]);
if (this.children.length) {
this.isContainer = true;
}
var container = this.isContainer ? this : this.parentNode;
// detect if laying out a shadowRoot host.
var forHost = container instanceof ShadowRoot;
if (forHost) {
this.installScopeStyle(this.styles[1], 'host');
container = container.host || document.body;
}
this.layoutContainer = container;
},
detached: function() {
this.layoutContainer = null;
},
layoutContainerChanged: function(old) {
this.style.display = this.layoutContainer === this ? null : 'none';
this.verticalChanged();
this.alignChanged();
this.justifyChanged();
},
setLayoutClass: function(prefix, old, newValue) {
if (this.layoutContainer) {
prefix = this.layoutPrefix + prefix;
if (old) {
this.layoutContainer.classList.remove(prefix + old);
}
if (newValue) {
this.layoutContainer.classList.add(prefix + newValue);
}
}
},
verticalChanged: function(old) {
old = old ? 'v' : 'h';
var vertical = this.vertical ? 'v' : 'h';
this.setLayoutClass('', old, vertical);
},
alignChanged: function(old) {
this.setLayoutClass('align-', old, this.align);
},
justifyChanged: function(old) {
this.setLayoutClass('justify-', old, this.justify);
},
reverseChanged: function(old) {
old = old ? 'reverse' : '';
var newValue = this.reverse ? 'reverse' : '';
this.setLayoutClass('', old, newValue);
}
});
})();
</script>
</polymer-element>