Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g-overlay update: simplify and add basic management for focus and z-index. #46

Merged
merged 4 commits into from
Dec 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/css/g-overlay.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
* license that can be found in the LICENSE file.
*/

@host {
position: fixed;
z-index: 10;
outline: none;
display: none;
}

/*
TODO(sorvell): include a reasonable set of default overlay opening
animations. What's here right now is ad hoc.
Animation styles:
g-overlay-fade
g-overlay-scale-slideup
g-overlay-shake
*/

@host {
position: fixed;
z-index: 10;
outline: none;
@host.opened {
display: block;
}

/* g-overlay-fade */
/*
transitions must be revealed before transition is applied
*/
@host.g-overlay-fade.revealed {
display: block;
}

@host.g-overlay-fade {
opacity: 0;
Expand All @@ -34,7 +35,9 @@
opacity: 1;
}

/* g-overlay-scale-slideup */
@host.g-overlay-scale-slideup.revealed {
display: block;
}

@host.g-overlay-scale-slideup {
opacity: 0.0;
Expand All @@ -53,8 +56,6 @@
-webkit-transition: all 1s;
}

/* g-overlay-shake */

@-webkit-keyframes g-overlay-shakeFadeIn {
0% {
display: block;
Expand Down Expand Up @@ -106,12 +107,14 @@
}

@host.g-overlay-shake.opened {
display: block;
-webkit-animation-duration: 0.5s;
-webkit-animation-fill-mode: both;
-webkit-animation-name: g-overlay-shakeFadeIn;
}

@host.g-overlay-shake.animating:not(.opened) {
display: block;
-webkit-animation-duration: 0.3s;
-webkit-animation-fill-mode: both;
-webkit-animation-name: g-overlay-shakeFadeOut;
Expand Down
111 changes: 74 additions & 37 deletions src/g-overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
-->
<element name="g-overlay" attributes="opened"
handlers="click: clickHandler, keydown: keydownHandler,
webkitAnimationEnd: animationEnd, webkitTransitionEnd: animationEnd">
webkitAnimationEnd: openedStylingAnimationEnd,
webkitTransitionEnd: openedStylingAnimationEnd">
<link rel="components" href="g-component.html">
<link rel="stylesheet" href="css/g-overlay.css">
<template>
Expand All @@ -16,66 +17,101 @@
<script>
var ESCAPE_KEY = 27;

// track overlays for z-index and focus managemant
var overlays = [];
var trackOverlays = function(inOverlay) {
if (inOverlay.opened) {
var z0 = overlayZ();
overlays.push(inOverlay);
var z1 = overlayZ();
if (z1 <= z0) {
inOverlay.node.style.zIndex = z0+1;
}
} else {
var i = overlays.indexOf(inOverlay);
if (i >= 0) {
overlays.splice(i, 1);
inOverlay.node.style.zIndex = null;
}
}
}

var currentOverlay = function() {
return overlays[overlays.length-1];
}

var overlayZ = function() {
var z = -1;
var current = currentOverlay();
if (current) {
var z1 = window.getComputedStyle(current.node).zIndex;
if (!isNaN(z1)) {
z = Number(z1);
}
}
return z;
}

var focusOverlay = function() {
var current = currentOverlay();
if (current) {
current.applyFocus();
}
}

/**
* The overlay component is hidden by default and can be opened to display
* its content. It's common to animate an overlay opened and closed. This
* can be achieved by styling the overlay node via the `opened` and
* `animating` attributes.
*/

this.component({
publish: {
//* Timeout (ms) for animation. After timeout, any opening animation will
//* be aborted and overlay will be set to opened (or not) and
//* not animating.
timeout: 1000,
//* Toggle the opened state of the overlay.
toggle: function() {
this.node.opened = !this.opened;
}
},
opened: false,
ready: function() {
this.node.hidden = true;
// make focusable; note that default tabIndex == -1 but node is
// only focusable if tabIndex attr is set.
if (this.node.tabIndex < 0) {
// make focusable
if (!this.node.hasAttribute('tabindex')) {
this.node.tabIndex = -1;
}
},
openedChanged: function() {
this.startAnimation();
// TODO(sorvell): need a controllable api for this, including
// maybe focusElement.
this.renderOpened();
trackOverlays(this);
this.asyncMethod('applyFocus');
this.fireEvent('opened', this.opened);
},
getFocusNode: function() {
return this.node.querySelector('[autofocus]') || this.node;
},
// TODO(sorvell): nodes stay focused when they become un-focusable due to
// an ancestory becoming display: none; file bug.
applyFocus: function() {
var focusNode = this.getFocusNode();
if (this.opened) {
this.node.focus();
focusNode.focus();
} else {
focusNode.blur();
focusOverlay();
}
this.fireEvent('openedChanged', this.opened);
},
startAnimation: function() {
this.cancelAnimation();
this.node.hidden = false;
// force animationEnd after timeout ms
this._animating = this.asyncMethod('animationEnd', null, this.timeout);
this.asyncMethod('styleAnimationStart');
renderOpened: function() {
this.node.classList.add('revealed');
// continue styling after delay so display state can change without
// aborting transitions
this.asyncMethod('continueRenderOpened');
},
styleAnimationStart: function() {
continueRenderOpened: function() {
this.node.classList.toggle('opened', this.opened);
this.node.classList.add('animating');
},
animationEnd: function() {
if (this._animating) {
this.cancelAnimation();
this.node.classList.remove('animating');
if (!this.opened) {
this.node.hidden = true;
}
}
},
cancelAnimation: function() {
if (this._animating) {
clearTimeout(this._animating);
this._animating = null;
}
openedStylingAnimationEnd: function() {
this.node.classList.remove('animating');
this.node.classList.toggle('revealed', this.opened);
},
clickHandler: function(e) {
if (e.target && e.target.hasAttribute('overlay-toggle')) {
Expand All @@ -84,7 +120,8 @@
},
keydownHandler: function(e) {
if (e.keyCode == ESCAPE_KEY) {
this.opened = false;
this.node.opened = false;
e.stopPropagation();
}
}
});
Expand Down
34 changes: 26 additions & 8 deletions workbench/overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,42 @@
opacity: 0;
}

#dialog, #dialog2 {
font-family: Arial, Helvetica, sans-serif;
g-overlay {
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
-webkit-user-select: none;
position: fixed;
overflow: hidden;
background: white;
padding:30px 42px;
outline: 1px solid rgba(0,0,0,0.2);
box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

#dialog, #dialog2 {
top: 72px;
left: 50%;
width: 512px;
margin-left: -256px;
padding:30px 42px;
outline: 1px solid rgba(0,0,0,0.2);
box-shadow: 0 4px 16px rgba(0,0,0,0.2);
z-index: 100;
}

#confirmation {
position: absolute;
left: 50%;
top: 100px;
text-align: center;
width: 150px;
margin-left: -75px;
}
</style>
</head>
<body>
<script>
$ = document.querySelector.bind(document);

somethingCheck = function() {
$('#confirmation').opened = (event.target.value == 'something');
}
</script>

<button onclick="$('#dialog').toggle()">Toggle Dialog</button>
Expand All @@ -40,8 +55,11 @@ <h2>Dialog</h2>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla sapien sed enim sollicitudin laoreet. Suspendisse suscipit, metus ac volutpat sodales, libero magna semper lacus, molestie fringilla massa orci ut arcu. Nullam sodales urna sit amet odio vehicula mattis.</div><br><br>
<div>Ut aliquam vulputate congue. Vestibulum pretium pretium nulla quis sollicitudin. Praesent lacinia congue erat nec mattis. Fusce commodo lacus est. Duis turpis eros, ultrices sed aliquet non, blandit egestas velit. Integer a augue nec lorem tristique hendrerit. Curabitur imperdiet risus id enim bibendum vestibulum. Integer id magna at arcu faucibus fermentum vel a augue. Sed fringilla venenatis dolor, in blandit magna molestie luctus. Vestibulum dignissim posuere ultrices. Aenean urna nisl, tincidunt vitae iaculis ut, pharetra nec eros.</div><br><br>
<div>
<g-checkbox></g-checkbox>
<input placeholder="say something..." autofocus oninput="somethingCheck()"></input><br>
I agree with this wholeheartedly.
<g-overlay id="confirmation">
Thank you.
</g-overlay>
</div><br><br>
<button overlay-toggle>OK</button>
</g-overlay>
Expand Down