|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>The focus and blur Event Fix</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>The focus and blur Event Fix</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>The <code>event-focus</code> module supplies a behavior patch for the native |
|
|
32 |
<code>focus</code> and <code>blur</code> events to allow them to bubble for event delegation.</p> |
|
|
33 |
</div> |
|
|
34 |
|
|
|
35 |
<h2 id="bubble-time">Bubble time</h2> |
|
|
36 |
|
|
|
37 |
<p>Most DOM events bubble. If, for example, you click on a button in a form, |
|
|
38 |
the click event will be dispatched to event subscribers on the button, on the |
|
|
39 |
fieldset, on the form, on the div containing the form, and so on up to the |
|
|
40 |
<code>document</code>. You can subscribe to the click event at any level and your |
|
|
41 |
callback will be executed. But <code>focus</code> and <code>blur</code> aren't like that. They are |
|
|
42 |
only dispatched to subscribers on the element that received or lost focus.</p> |
|
|
43 |
|
|
|
44 |
<p>But <a href="delegation.html">we like event delegation</a> so we determined |
|
|
45 |
a way to synthesize bubbling for <code>focus</code> and <code>blur</code> using <a |
|
|
46 |
href="http://www.w3.org/TR/DOM-Level-3-Events/#event-flow">capture phase</a> |
|
|
47 |
subscriptions in non-IE browsers, and proprietary focus-related events in IE |
|
|
48 |
that do bubble. The result should be transparent for individual element |
|
|
49 |
<code>focus</code> and <code>blur</code> subscriptions, but make <code>focus</code> and <code>blur</code> work as you would |
|
|
50 |
expect when subscribing higher in the document tree.</p> |
|
|
51 |
|
|
|
52 |
<pre class="code prettyprint"><ul id="items"> |
|
|
53 |
<li><input value="form elements are focusable by default"></li> |
|
|
54 |
<li><a href="yahoo.com">Links are focusable by default</a></li> |
|
|
55 |
<li tabindex="2">Things with `tabindex` 0 or higher are made user focusable</li> |
|
|
56 |
<li tabindex="-1">Things with `tabindex` -1 can be focused programatically</li> |
|
|
57 |
<li>But by default none of those `focus` events will report to #items</li> |
|
|
58 |
</ul> |
|
|
59 |
<script> |
|
|
60 |
YUI().use('node-base', function (Y) { |
|
|
61 |
var items = Y.one('#items'); |
|
|
62 |
|
|
|
63 |
items.on('focus', function (e) { |
|
|
64 |
Y.log("The list does not have a tabindex, so this will never execute"); |
|
|
65 |
}); |
|
|
66 |
|
|
|
67 |
Y.use('event-focus', function () { |
|
|
68 |
items.on('focus', function (e) { |
|
|
69 |
Y.log("But now that event-focus is in place, future " + |
|
|
70 |
"subscriptions like this one will."); |
|
|
71 |
|
|
|
72 |
// e.target received focus |
|
|
73 |
}); |
|
|
74 |
}); |
|
|
75 |
}); |
|
|
76 |
</script></pre> |
|
|
77 |
|
|
|
78 |
</div> |
|
|
79 |
</div> |
|
|
80 |
</div> |
|
|
81 |
|
|
|
82 |
<div class="yui3-u-1-4"> |
|
|
83 |
<div class="sidebar"> |
|
|
84 |
|
|
|
85 |
<div id="toc" class="sidebox"> |
|
|
86 |
<div class="hd"> |
|
|
87 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
88 |
</div> |
|
|
89 |
|
|
|
90 |
<div class="bd"> |
|
|
91 |
<ul class="toc"> |
|
|
92 |
<li> |
|
|
93 |
<a href="#bubble-time">Bubble time</a> |
|
|
94 |
</li> |
|
|
95 |
</ul> |
|
|
96 |
</div> |
|
|
97 |
</div> |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
<div class="sidebox"> |
|
|
102 |
<div class="hd"> |
|
|
103 |
<h2 class="no-toc">Examples</h2> |
|
|
104 |
</div> |
|
|
105 |
|
|
|
106 |
<div class="bd"> |
|
|
107 |
<ul class="examples"> |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
111 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
112 |
</li> |
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
117 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
118 |
</li> |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
123 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
124 |
</li> |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
</ul> |
|
|
138 |
</div> |
|
|
139 |
</div> |
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
<div class="sidebox"> |
|
|
144 |
<div class="hd"> |
|
|
145 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
146 |
</div> |
|
|
147 |
|
|
|
148 |
<div class="bd"> |
|
|
149 |
<ul class="examples"> |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
159 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
160 |
</li> |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
165 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
166 |
</li> |
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
<li data-description="Example Photo Browser application."> |
|
|
171 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
172 |
</li> |
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
177 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
178 |
</li> |
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
<li data-description="Use IO to request data over HTTP."> |
|
|
183 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
184 |
</li> |
|
|
185 |
|
|
|
186 |
|
|
|
187 |
</ul> |
|
|
188 |
</div> |
|
|
189 |
</div> |
|
|
190 |
|
|
|
191 |
</div> |
|
|
192 |
</div> |
|
|
193 |
</div> |
|
|
194 |
</div> |
|
|
195 |
|
|
|
196 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
197 |
<script>prettyPrint();</script> |
|
|
198 |
|
|
|
199 |
<script> |
|
|
200 |
YUI.Env.Tests = { |
|
|
201 |
examples: [], |
|
|
202 |
project: '../assets', |
|
|
203 |
assets: '../assets/event', |
|
|
204 |
name: 'event', |
|
|
205 |
title: 'The focus and blur Event Fix', |
|
|
206 |
newWindow: '', |
|
|
207 |
auto: false |
|
|
208 |
}; |
|
|
209 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
210 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
211 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
212 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
213 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
214 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
215 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
216 |
YUI.Env.Tests.examples.push('get'); |
|
|
217 |
|
|
|
218 |
</script> |
|
|
219 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
</body> |
|
|
224 |
</html> |