-
Notifications
You must be signed in to change notification settings - Fork 0
/
rula-expandable-card.html
289 lines (247 loc) · 8.4 KB
/
rula-expandable-card.html
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
<!--
@license
Copyright (c) 2018 Ryerson University Library & Archives. All rights reserved.
This code may only be used under the BSD style license included with this project.
-->
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-styles/shadow.html">
<!--
`rula-expandable-card` is an elements combining aspects of Material Design
[Cards](https://www.google.com/design/spec/components/cards.html)
and [Dialogs](https://www.google.com/design/spec/components/dialogs.html).
It displays static content in similar fashion to a Card, and when clicked by the
user it expands to fill the screen like a modal dialog while showing additional content.
### Example
```html
<rula-expandable-card>
<div main-content>
<h2>Here's a question</h2>
<span>How much wood can a woodchuck chuck if a woodchuck chould chuck wood?</span>
</div>
<div extra-content>
A woodchuck would chuck as much wood as a woodchuck could chuck if a woodchuck could chuck wood!
</div>
<rula-expandable-card>
```
## Styling
The following custom properties and mxing are available for styling:
Custom propery | Description | Default
---------------|-------------|----------
`--rula-expandable-card-scrim` | Mixin applied to the background when the card is open | `{}`
`--rula-expandable-card-body` | Mixin applied to the body of the card when closed | `{}`
`--rula-expandable-card-modal` | Mixin applied to the modal container when open | `{}`
`--rula-expandable-card` | Mixin applied to the element | `{}`
@group RULA WebComponents
@rula-expandable-card
@demo demo/index.html
-->
<dom-module id="rula-expandable-card">
<template>
<style>
* {
box-sizing: border-box;
}
:host {
@apply --shadow-elevation-2dp;
display: inline-block;
padding-top: 70%;
position: relative;
width: 100%;
@apply --rula-expandable-card;
}
:host([open]) {
box-shadow: none;
}
#body {
bottom: 0;
height: 100%;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
@apply --rula-expandable-card-body;
}
#body[open] {
display: none;
}
#scrim {
background-color: rgba(0, 0, 0, 0.4);
bottom: 0;
left: 0;
opacity: 0;
position: fixed;
right: 0;
top: 0;
transform: translateZ(0);
transition-duration: 200ms;
transition-property: opacity;
z-index: 999;
@apply --rula-expandable-card-scrim;
}
#scrim[hidden] {
visibility: hidden;
}
#scrim[open] {
opacity: 1;
}
#modal {
background-color: #ffffff;
border-radius: 2px;
position: fixed;
display: none;
visibility: hidden;
flex-direction: column;
box-sizing: border-box;
overflow: hidden;
@apply --rula-expandable-card-modal;
}
#modal[open] {
@apply --shadow-elevation-8dp;
}
#modal[visible] {
visibility: visible;
display: flex;
z-index: 1000;
}
#modal[transition] {
transition-property: left, width, top, height, box-shadow;
transition-delay: 0s, 0s, 0.06s, 0.06s, 0s;
transition-duration: 0.28s;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
</style>
<div id="scrim"></div>
<div id="body"></div>
<div id="modal">
<slot></slot>
</div>
</template>
<script>
/**
* `rula-expandable-card`
* An element that can go between a closed and open state with a morph inbetween.
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class RulaExpandableCard extends Polymer.Element {
static get is() {
return 'rula-expandable-card';
}
static get properties() {
return {
open: {
type: Boolean,
value: false,
reflectToAttribute: true,
}
}
};
ready() {
super.ready();
// Add necessary event handlers
this.$.body.addEventListener('click', () => this._open());
this.$.modal.addEventListener('click', () => this._close());
this.$.scrim.addEventListener('click', () => this._close());
this.$.scrim.addEventListener('scroll', e => e.preventDefault());
this.$.modal.addEventListener('transitionend',
(e) => this._onTransitionEnd(e));
// Set initial attributes to hide the scrim.
this.$.scrim.setAttribute('hidden', '');
// Copy the content of the header div into the card. Remove the
// attribute to avoid duplicate styles.
const headerContent = this.querySelector('[main-content]');
const clone = headerContent.cloneNode(true);
clone.removeAttribute('header');
this.$.body.appendChild(clone);
// Add opacity transition to the content element.
const content = this.querySelector('[extra-content]');
content.style.transitionDuration = '0.2s';
content.style.transitionDelay = '0.1s';
content.style.transitionProperty = 'opacity';
};
/**
* Opens the modal component of this expandable card.
*/
_open() {
this.open = true;
// Get the current size of this element. The modal will be positioned
// on top of it and expanded from there. The modal element does not have
// CSS transition properties so this takes place instantly.
const b = this.getBoundingClientRect();
const m = this.$.modal;
m.style.top = b.y + 'px';
m.style.left = b.x + 'px';
m.style.width = b.width + 'px';
m.style.height = b.height + 'px';
// Set the modal and scrim to visible while hiding the card. This also
// happens instantly.
m.setAttribute('visible', '');
this.$.body.setAttribute('open', '');
this.$.scrim.removeAttribute('hidden');
// Ensure the content of the modal is transparent, it will be faded in
// as part of expanding the modal.
const content = this.querySelector('[extra-content]');
content.style.opacity = 0;
const mh = this.querySelector('[main-content]');
mh.style.height = b.height + 'px';
mh.style.minHeight = b.height + 'px';
Polymer.RenderStatus.afterNextRender(this, () => {
m.setAttribute('transition', '');
m.setAttribute('open', '');
this.$.scrim.setAttribute('open', '');
m.style.top = '10%';
m.style.left = '10%';
m.style.width = '80%';
m.style.height = '80%';
content.style.opacity = 1;
});
};
/**
* Closes the modal component of this expandable card.
*/
_close() {
this.open = false;
// Get the bounding rectangle of this element, in case the size has
// changed for some reason between open and close.
const b = this.getBoundingClientRect();
const m = this.$.modal;
m.style.top = b.y + 'px';
m.style.left = b.x + 'px';
m.style.width = b.width + 'px';
m.style.height = b.height + 'px';
//
const content = this.querySelector('[extra-content]');
content.style.opacity = 0;
// Begin the close transition for the scrim and modal.
this.$.scrim.removeAttribute('open');
m.removeAttribute('open');
};
/**
* Handles when a CSS transition finishes.
*
* @param {Event} e The triggering event.
*/
_onTransitionEnd(e) {
if (!this.open && e.propertyName === 'height') {
// Only concerned about when the height transition is finished. There
// are other transitions that finish earlier. The modal can be fully
// hidden until the last transition is done.
const m = this.$.modal;
// Now that the closing transitions have finished:
// - Hide the modal
// - Remove the modal transition properties
// - Hide the scrim
// - Show the card
m.removeAttribute('transition');
m.removeAttribute('visible');
this.$.body.removeAttribute('open');
this.$.scrim.setAttribute('hidden', '');
}
};
}
window.customElements.define(RulaExpandableCard.is, RulaExpandableCard);
</script>
</dom-module>