2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 <link rel=
"import" href=
"../polymer/polymer.html">
11 <link rel=
"import" href=
"../core-transition/core-transition.html">
12 <link rel=
"import" href=
"../core-resizable/core-resizable.html">
13 <link rel=
"import" href=
"core-key-helper.html">
14 <link rel=
"import" href=
"core-overlay-layer.html">
17 The `core-overlay` element displays overlayed on top of other content. It starts
18 out hidden and is displayed by setting its `opened` property to true.
19 A `core-overlay's` opened state can be toggled by calling the `toggle`
22 The `core-overlay` will, by default, show/hide itself when it's opened. The
23 `target` property may be set to another element to cause that element to
24 be shown when the overlay is opened.
26 It's common to want a `core-overlay` to animate to its opened
27 position. The `core-overlay` element uses a `core-transition` to handle
28 animation. The default transition is `core-transition-fade` which
29 causes the overlay to fade in when displayed. See
30 <a href="../core-transition/">`core-transition`</a> for more
31 information about customizing a `core-overlay's` opening animation. The
32 `backdrop` property can be set to true to show a backdrop behind the overlay
33 that will darken the rest of the window.
35 An element that should close the `core-overlay` will automatically
36 do so if it's given the `core-overlay-toggle` attribute. This attribute
37 can be customized with the `closeAttribute` property. You can also use
38 `closeSelector` if more general matching is needed.
40 By default `core-overlay` will close whenever the user taps outside it or
41 presses the escape key. This behavior can be turned off via the
42 `autoCloseDisabled` property.
46 <input placeholder="say something..." autofocus>
47 <div>I agree with this wholeheartedly.</div>
48 <button core-overlay-toggle>OK</button>
51 `core-overlay` will automatically size and position itself according to the
52 following rules. The overlay's size is constrained such that it does not
53 overflow the screen. This is done by setting maxHeight/maxWidth on the
54 `sizingTarget`. If the `sizingTarget` already has a setting for one of these
55 properties, it will not be overridden. The overlay should
56 be positioned via css or imperatively using the `core-overlay-position` event.
57 If the overlay is not positioned vertically via setting `top` or `bottom`, it
58 will be centered vertically. The same is true horizontally via a setting to
59 `left` or `right`. In addition, css `margin` can be used to provide some space
60 around the overlay. This can be used to ensure
61 that, for example, a drop shadow is always visible around the overlay.
68 Fired when the `core-overlay`'s `opened` property changes.
70 @event core-overlay-open
71 @param {Object} detail
72 @param {Object} detail.opened the opened state
75 Fired when the `core-overlay` has completely opened.
77 @event core-overlay-open-completed
80 Fired when the `core-overlay` has completely closed.
82 @event core-overlay-close-completed
85 Fired when the `core-overlay` needs to position itself. Optionally, implement
86 in order to position an overlay via code. If the overlay was not otherwise
87 positioned, it's important to indicate how the overlay has been positioned by
88 setting the `dimensions.position` object. For example, if the overlay has been
89 positioned via setting `right` and `top`, set dimensions.position to an
90 object like this: `{v: 'top', h: 'right'}`.
92 @event core-overlay-position
93 @param {Object} detail
94 @param {Object} detail.target the overlay target
95 @param {Object} detail.sizingTarget the overlay sizing target
96 @param {Object} detail.opened the opened state
99 .core-overlay-backdrop {
105 background-color: black;
107 transition: opacity
0.2s;
110 .core-overlay-backdrop.core-opened {
115 <polymer-element name=
"core-overlay">
119 Polymer(Polymer
.mixin({
123 * The target element that will be shown when the overlay is
124 * opened. If unspecified, the core-overlay itself is the target.
128 * @default the overlay element
134 * A `core-overlay`'s size is guaranteed to be
135 * constrained to the window size. To achieve this, the sizingElement
136 * is sized with a max-height/width. By default this element is the
137 * target element, but it can be specifically set to a specific element
138 * inside the target if that is more appropriate. This is useful, for
139 * example, when a region inside the overlay should scroll if needed.
141 * @attribute sizingTarget
143 * @default the target element
148 * Set opened to true to show an overlay and to false to hide it.
149 * A `core-overlay` may be made initially opened by setting its
150 * `opened` attribute.
158 * If true, the overlay has a backdrop darkening the rest of the screen.
159 * The backdrop element is attached to the document body and may be styled
160 * with the class `core-overlay-backdrop`. When opened the `core-opened`
163 * @attribute backdrop
170 * If true, the overlay is guaranteed to display above page content.
179 * By default an overlay will close automatically if the user
180 * taps outside it or presses the escape key. Disable this
181 * behavior by setting the `autoCloseDisabled` property to true.
182 * @attribute autoCloseDisabled
186 autoCloseDisabled
: false,
189 * By default an overlay will focus its target or an element inside
190 * it with the `autoFocus` attribute. Disable this
191 * behavior by setting the `autoFocusDisabled` property to true.
192 * @attribute autoFocusDisabled
196 autoFocusDisabled
: false,
199 * This property specifies an attribute on elements that should
200 * close the overlay on tap. Should not set `closeSelector` if this
203 * @attribute closeAttribute
205 * @default "core-overlay-toggle"
207 closeAttribute
: 'core-overlay-toggle',
210 * This property specifies a selector matching elements that should
211 * close the overlay on tap. Should not set `closeAttribute` if this
214 * @attribute closeSelector
221 * The transition property specifies a string which identifies a
222 * <a href="../core-transition/">`core-transition`</a> element that
223 * will be used to help the overlay open and close. The default
224 * `core-transition-fade` will cause the overlay to fade in and out.
226 * @attribute transition
228 * @default 'core-transition-fade'
230 transition
: 'core-transition-fade'
234 captureEventName
: 'tap',
237 'keydown': 'keydownHandler',
238 'core-transitionend': 'transitionend'
241 attached: function() {
242 this.resizerAttachedHandler();
245 detached: function() {
246 this.resizerDetachedHandler();
249 resizerShouldNotify: function() {
253 registerCallback: function(element
) {
254 this.layer
= document
.createElement('core-overlay-layer');
255 this.keyHelper
= document
.createElement('core-key-helper');
256 this.meta
= document
.createElement('core-transition');
257 this.scrim
= document
.createElement('div');
258 this.scrim
.className
= 'core-overlay-backdrop';
262 this.target
= this.target
|| this;
263 // flush to ensure styles are installed before paint
268 * Toggle the opened state of the overlay.
272 this.opened
= !this.opened
;
276 * Open the overlay. This is equivalent to setting the `opened`
285 * Close the overlay. This is equivalent to setting the `opened`
293 domReady: function() {
294 this.ensureTargetSetup();
297 targetChanged: function(old
) {
299 // really make sure tabIndex is set
300 if (this.target
.tabIndex
< 0) {
301 this.target
.tabIndex
= -1;
303 this.addElementListenerList(this.target
, this.targetListeners
);
304 this.target
.style
.display
= 'none';
305 this.target
.__overlaySetup
= false;
308 this.removeElementListenerList(old
, this.targetListeners
);
309 var transition
= this.getTransition();
311 transition
.teardown(old
);
313 old
.style
.position
= '';
314 old
.style
.outline
= '';
316 old
.style
.display
= '';
320 transitionChanged: function(old
) {
325 this.getTransition(old
).teardown(this.target
);
327 this.target
.__overlaySetup
= false;
330 // NOTE: wait to call this until we're as sure as possible that target
332 ensureTargetSetup: function() {
333 if (!this.target
|| this.target
.__overlaySetup
) {
336 if (!this.sizingTarget
) {
337 this.sizingTarget
= this.target
;
339 this.target
.__overlaySetup
= true;
340 this.target
.style
.display
= '';
341 var transition
= this.getTransition();
343 transition
.setup(this.target
);
345 var style
= this.target
.style
;
346 var computed
= getComputedStyle(this.target
);
347 if (computed
.position
=== 'static') {
348 style
.position
= 'fixed';
350 style
.outline
= 'none';
351 style
.display
= 'none';
354 openedChanged: function() {
355 this.transitioning
= true;
356 this.ensureTargetSetup();
357 this.prepareRenderOpened();
358 // async here to allow overlay layer to become visible.
359 this.async(function() {
360 this.target
.style
.display
= '';
361 // force layout to ensure transitions will go
362 this.target
.offsetWidth
;
365 this.fire('core-overlay-open', this.opened
);
368 // tasks which must occur before opening; e.g. making the element visible
369 prepareRenderOpened: function() {
373 this.prepareBackdrop();
374 // async so we don't auto-close immediately via a click.
375 this.async(function() {
376 if (!this.autoCloseDisabled
) {
377 this.enableElementListener(this.opened
, document
,
378 this.captureEventName
, 'captureHandler', true);
381 this.enableElementListener(this.opened
, window
, 'resize',
385 // force layout so SD Polyfill renders
386 this.target
.offsetHeight
;
387 this.discoverDimensions();
388 // if we are showing, then take care when positioning
389 this.preparePositioning();
390 this.positionTarget();
391 this.updateTargetDimensions();
392 this.finishPositioning();
394 this.layer
.addElement(this.target
);
395 this.layer
.opened
= this.opened
;
400 // tasks which cause the overlay to actually open; typically play an
402 renderOpened: function() {
404 var transition
= this.getTransition();
406 transition
.go(this.target
, {opened
: this.opened
});
408 this.transitionend();
410 this.renderBackdropOpened();
413 // finishing tasks; typically called via a transition
414 transitionend: function(e
) {
415 // make sure this is our transition event.
416 if (e
&& e
.target
!== this.target
) {
419 this.transitioning
= false;
421 this.resetTargetDimensions();
422 this.target
.style
.display
= 'none';
423 this.completeBackdrop();
426 if (!currentOverlay()) {
427 this.layer
.opened
= this.opened
;
429 this.layer
.removeElement(this.target
);
432 this.fire('core-overlay-' + (this.opened
? 'open' : 'close') +
437 prepareBackdrop: function() {
438 if (this.backdrop
&& this.opened
) {
439 if (!this.scrim
.parentNode
) {
440 document
.body
.appendChild(this.scrim
);
441 this.scrim
.style
.zIndex
= currentOverlayZ() - 1;
447 renderBackdropOpened: function() {
448 if (this.backdrop
&& getBackdrops().length
< 2) {
449 this.scrim
.classList
.toggle('core-opened', this.opened
);
453 completeBackdrop: function() {
456 if (getBackdrops().length
=== 0) {
457 this.scrim
.parentNode
.removeChild(this.scrim
);
462 preparePositioning: function() {
463 this.target
.style
.transition
= this.target
.style
.webkitTransition
= 'none';
464 this.target
.style
.transform
= this.target
.style
.webkitTransform
= 'none';
465 this.target
.style
.display
= '';
468 discoverDimensions: function() {
469 if (this.dimensions
) {
472 var target
= getComputedStyle(this.target
);
473 var sizer
= getComputedStyle(this.sizingTarget
);
476 v
: target
.top
!== 'auto' ? 'top' : (target
.bottom
!== 'auto' ?
478 h
: target
.left
!== 'auto' ? 'left' : (target
.right
!== 'auto' ?
483 v
: sizer
.maxHeight
!== 'none',
484 h
: sizer
.maxWidth
!== 'none'
487 top
: parseInt(target
.marginTop
) || 0,
488 right
: parseInt(target
.marginRight
) || 0,
489 bottom
: parseInt(target
.marginBottom
) || 0,
490 left
: parseInt(target
.marginLeft
) || 0
495 finishPositioning: function(target
) {
496 this.target
.style
.display
= 'none';
497 this.target
.style
.transform
= this.target
.style
.webkitTransform
= '';
498 // force layout to avoid application of transform
499 this.target
.offsetWidth
;
500 this.target
.style
.transition
= this.target
.style
.webkitTransition
= '';
503 getTransition: function(name
) {
504 return this.meta
.byId(name
|| this.transition
);
507 getFocusNode: function() {
508 return this.target
.querySelector('[autofocus]') || this.target
;
511 applyFocus: function() {
512 var focusNode
= this.getFocusNode();
514 if (!this.autoFocusDisabled
) {
519 if (currentOverlay() == this) {
520 console
.warn('Current core-overlay is attempting to focus itself as next! (bug)');
527 positionTarget: function() {
528 // fire positioning event
529 this.fire('core-overlay-position', {target
: this.target
,
530 sizingTarget
: this.sizingTarget
, opened
: this.opened
});
531 if (!this.dimensions
.position
.v
) {
532 this.target
.style
.top
= '0px';
534 if (!this.dimensions
.position
.h
) {
535 this.target
.style
.left
= '0px';
539 updateTargetDimensions: function() {
541 this.repositionTarget();
544 sizeTarget: function() {
545 this.sizingTarget
.style
.boxSizing
= 'border-box';
546 var dims
= this.dimensions
;
547 var rect
= this.target
.getBoundingClientRect();
549 this.sizeDimension(rect
, dims
.position
.v
, 'top', 'bottom', 'Height');
552 this.sizeDimension(rect
, dims
.position
.h
, 'left', 'right', 'Width');
556 sizeDimension: function(rect
, positionedBy
, start
, end
, extent
) {
557 var dims
= this.dimensions
;
558 var flip
= (positionedBy
=== end
);
559 var m
= flip
? start
: end
;
560 var ws
= window
['inner' + extent
];
561 var o
= dims
.margin
[m
] + (flip
? ws
- rect
[end
] :
563 var offset
= 'offset' + extent
;
564 var o2
= this.target
[offset
] - this.sizingTarget
[offset
];
565 this.sizingTarget
.style
['max' + extent
] = (ws
- o
- o2
) + 'px';
568 // vertically and horizontally center if not positioned
569 repositionTarget: function() {
570 // only center if position fixed.
571 if (this.dimensions
.position
.css
!== 'fixed') {
574 if (!this.dimensions
.position
.v
) {
575 var t
= (window
.innerHeight
- this.target
.offsetHeight
) / 2;
576 t
-= this.dimensions
.margin
.top
;
577 this.target
.style
.top
= t
+ 'px';
580 if (!this.dimensions
.position
.h
) {
581 var l
= (window
.innerWidth
- this.target
.offsetWidth
) / 2;
582 l
-= this.dimensions
.margin
.left
;
583 this.target
.style
.left
= l
+ 'px';
587 resetTargetDimensions: function() {
588 if (!this.dimensions
|| !this.dimensions
.size
.v
) {
589 this.sizingTarget
.style
.maxHeight
= '';
590 this.target
.style
.top
= '';
592 if (!this.dimensions
|| !this.dimensions
.size
.h
) {
593 this.sizingTarget
.style
.maxWidth
= '';
594 this.target
.style
.left
= '';
596 this.dimensions
= null;
599 tapHandler: function(e
) {
600 // closeSelector takes precedence since closeAttribute has a default non-null value.
602 (this.closeSelector
&& e
.target
.matches(this.closeSelector
)) ||
603 (this.closeAttribute
&& e
.target
.hasAttribute(this.closeAttribute
))) {
606 if (this.autoCloseJob
) {
607 this.autoCloseJob
.stop();
608 this.autoCloseJob
= null;
613 // We use the traditional approach of capturing events on document
614 // to to determine if the overlay needs to close. However, due to
615 // ShadowDOM event retargeting, the event target is not useful. Instead
616 // of using it, we attempt to close asynchronously and prevent the close
617 // if a tap event is immediately heard on the target.
618 // TODO(sorvell): This approach will not work with modal. For
619 // this we need a scrim.
620 captureHandler: function(e
) {
621 if (!this.autoCloseDisabled
&& (currentOverlay() == this)) {
622 this.autoCloseJob
= this.job(this.autoCloseJob
, function() {
628 keydownHandler: function(e
) {
629 if (!this.autoCloseDisabled
&& (e
.keyCode
== this.keyHelper
.ESCAPE_KEY
)) {
636 * Extensions of core-overlay should implement the `resizeHandler`
637 * method to adjust the size and position of the overlay when the
638 * browser window resizes.
639 * @method resizeHandler
641 resizeHandler: function() {
642 this.updateTargetDimensions();
645 // TODO(sorvell): these utility methods should not be here.
646 addElementListenerList: function(node
, events
) {
647 for (var i
in events
) {
648 this.addElementListener(node
, i
, events
[i
]);
652 removeElementListenerList: function(node
, events
) {
653 for (var i
in events
) {
654 this.removeElementListener(node
, i
, events
[i
]);
658 enableElementListener: function(enable
, node
, event
, methodName
, capture
) {
660 this.addElementListener(node
, event
, methodName
, capture
);
662 this.removeElementListener(node
, event
, methodName
, capture
);
666 addElementListener: function(node
, event
, methodName
, capture
) {
667 var fn
= this._makeBoundListener(methodName
);
669 Polymer
.addEventListener(node
, event
, fn
, capture
);
673 removeElementListener: function(node
, event
, methodName
, capture
) {
674 var fn
= this._makeBoundListener(methodName
);
676 Polymer
.removeEventListener(node
, event
, fn
, capture
);
680 _makeBoundListener: function(methodName
) {
681 var self
= this, method
= this[methodName
];
685 var bound
= '_bound' + methodName
;
687 this[bound
] = function(e
) {
688 method
.call(self
, e
);
694 }, Polymer
.CoreResizer
));
696 // TODO(sorvell): This should be an element with private state so it can
697 // be independent of overlay.
698 // track overlays for z-index and focus managemant
700 function addOverlay(overlay
) {
701 var z0
= currentOverlayZ();
702 overlays
.push(overlay
);
703 var z1
= currentOverlayZ();
705 applyOverlayZ(overlay
, z0
);
709 function removeOverlay(overlay
) {
710 var i
= overlays
.indexOf(overlay
);
712 overlays
.splice(i
, 1);
717 function applyOverlayZ(overlay
, aboveZ
) {
718 setZ(overlay
.target
, aboveZ
+ 2);
721 function setZ(element
, z
) {
722 element
.style
.zIndex
= z
;
725 function currentOverlay() {
726 return overlays
[overlays
.length
-1];
731 function currentOverlayZ() {
733 var current
= currentOverlay();
735 var z1
= window
.getComputedStyle(current
.target
).zIndex
;
740 return z
|| DEFAULT_Z
;
743 function focusOverlay() {
744 var current
= currentOverlay();
745 // We have to be careful to focus the next overlay _after_ any current
746 // transitions are complete (due to the state being toggled prior to the
747 // transition). Otherwise, we risk infinite recursion when a transitioning
748 // (closed) overlay becomes the current overlay.
750 // NOTE: We make the assumption that any overlay that completes a transition
751 // will call into focusOverlay to kick the process back off. Currently:
752 // transitionend -> applyFocus -> focusOverlay.
753 if (current
&& !current
.transitioning
) {
754 current
.applyFocus();
759 function trackBackdrop(element
) {
760 if (element
.opened
) {
761 backdrops
.push(element
);
763 var i
= backdrops
.indexOf(element
);
765 backdrops
.splice(i
, 1);
770 function getBackdrops() {