forked from PolymerLabs/iron-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iron-overlay-renderer.html
213 lines (184 loc) · 6.3 KB
/
iron-overlay-renderer.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
<!--
@license
Copyright (c) 2016 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-element.html">
<link rel="import" href="iron-overlay-renderer-shared-styles.html">
<!--
`iron-overlay-renderer` renders the overlay content in the center of the screen,
and handles the switch from opened state to closed state & viceversa.
### Styling
`iron-overlay` sets its renderer attribute `data-overlay` to its id, so that
styling can be done like this:
<style is="custom-style">
iron-overlay-renderer {
--iron-overlay-background-color: yellow;
}
[data-overlay="overlay1"] {
--iron-overlay-background-color: orange;
}
</style>
<iron-overlay-container></iron-overlay-container>
<iron-overlay>
<template>Overlay Content</template>
</iron-overlay>
<iron-overlay id="overlay1">
<template>Overlay 1 Content</template>
</iron-overlay>
@demo demo/index.html
-->
<dom-module id="iron-overlay-renderer">
<template strip-whitespace>
<style include="iron-overlay-renderer-shared-styles"></style>
<div id="backdrop"></div>
<div id="overlay">
<slot></slot>
</div>
</template>
<script>
(() => {
'use strict';
class IronOverlayRenderer extends Polymer.Element {
static get is() {
return 'iron-overlay-renderer';
}
static get properties() {
return {
/**
* True if the overlay is currently displayed.
*/
opened: {
type: Boolean,
observer: '_openedChanged'
},
/**
* True if the overlay has open/close animation.
*/
animated: {
type: Boolean
},
/**
* True if the overlay should have a backdrop.
*/
withBackdrop: {
type: Boolean
},
/**
* Is true during the transition from close to open and open to close.
*/
transitioning: {
type: Boolean,
notify: true,
readOnly: true
}
};
}
constructor() {
super();
// For a performant transition from open to close and close to open.
this._pendingRAF = null;
}
ready() {
super.ready();
// If delegatesFocus is not supported or class is extended but doesn't
// have a shadow root, make the element focusable but not tabbable.
if (!this.shadowRoot || !this.shadowRoot.delegatesFocus) {
this.setAttribute('tabindex', '-1');
}
this.$.overlay.addEventListener('transitionend', this._onTransitionEnd.bind(this));
}
/**
* Overridden from Polymer.Element to ensure the shadow root delegates focus.
* See Polymer/polymer#3988
* @protected
*/
_attachDom(dom) {
// Create shadowRoot only when there is content to be added
if (dom && this.attachShadow && !this.shadowRoot) {
this.attachShadow({
mode: 'open',
delegatesFocus: true
});
}
return super._attachDom(dom);
}
/**
* Tasks to be performed to actually open/close. Override this to play
* animations, and call `_finishRenderOpenedChanged()` when those are done.
* @protected
*/
_renderOpenedChanged() {
// Doesn't wait for animations if animated is false.
if (!this.animated) {
this._finishRenderOpenedChanged();
}
}
/**
* @protected
*/
_finishRenderOpenedChanged() {
this._setTransitioning(false);
}
/**
* @param {boolean} opened
*/
_openedChanged(opened) {
this._onNextAF(this._startRenderOpenedChanged);
}
/**
* Starts the update from open to closed & viceversa.
*/
_startRenderOpenedChanged() {
this._setTransitioning(true);
// Update interactions with element.
this.style.pointerEvents = this.opened ? '' : 'none';
// Need to force layout for any animation to trigger.
if (this.animated) {
this._forceLayout();
}
// Update classes for overlay and backdrop.
this.$.overlay.classList.toggle('opened', this.opened);
// Check if there is a backdrop in the shadow dom.
this.$.backdrop && this.$.backdrop.classList.toggle('opened', this.opened);
this._renderOpenedChanged();
}
/**
* Forces layout.
*/
_forceLayout() {
return this.offsetWidth;
}
/**
* Callback for transitionend, ends the animation.
* @param {!Event} event
*/
_onTransitionEnd(event) {
if (event.target === this.$.overlay && this.transitioning) {
// Since there might be multiple animations (e.g. opacity + transition),
// call finish function only once (reuse RAF).
this._onNextAF(this._finishRenderOpenedChanged);
}
}
/**
* Executes the callback at the next available animation frame.
* @param {!Function} callback Its scope will be the renderer itself
*/
_onNextAF(callback) {
this._pendingRAF && window.cancelAnimationFrame(this._pendingRAF);
const self = this;
this._pendingRAF = window.requestAnimationFrame(function nextAnimationFrame() {
self._pendingRAF = null;
callback.call(self);
});
}
}
Polymer.IronOverlayRenderer = IronOverlayRenderer;
customElements.define(IronOverlayRenderer.is, IronOverlayRenderer);
})();
</script>
</dom-module>