Focus Manager Node Plugin: Accessible TabView
This example illustrates how to create an accessible tabview widget using the Focus Manager Node Plugin, Event's delegation support, and Node's support for the WAI-ARIA Roles and States.
Today's News
Top Stories
-
Pakistan native admits to Times Square bomb plot (AP)
-
Security slip let suspect on plane, near takeoff (AP)
-
GOP Coats wins Ind. primary; NC, Ohio vote (AP)
-
Nashville waters receding; crews search for bodies (AP)
-
Paranoia, anxiety grow over Gulf Coast oil spill (AP)
-
Woman in Target stabbing lost custody of child (AP)
-
Mummified baby corpse missing from NH grave site (AP)
-
Coroner: Haim died of pneumonia complications (AP)
-
Mass. woman with no arms preparing for black belt (AP)
-
Tigers' longtime broadcaster Harwell dead at 92 (AP)
World News
-
Iraq's Shiites unite to try to form new government (AP)
-
Iran's president defiant in face of new sanctions (AP)
-
Iceland's ash may keep choking Europe's air travel (AP)
-
Grenada man hauls bucket of human heads to police (AP)
-
Shiite militia reviving in post-election Iraq (AP)
-
Kim Jong Il visits China despite South Korean ship anger (Time.com)
-
General strike in debt-ridden Greece over new cuts (AP)
-
(AP)
-
US sends 2 Gitmo detainees to Spain, 1 to Bulgaria (AP)
-
US envoy visits Sudan for first time since elections (AFP)
Entertainment News
-
Despite floods, Grand Ole Opry held across town (AP)
-
Coroner: Haim died of pneumonia complications (AP)
-
Doctor: Bret Michaels released from hospital (AP)
-
'Idol' viewership down as Cowell nears finale (AP)
-
TV producer sentenced in Letterman blackmail plot (AP)
-
`Seinfeld' actress is tickled by Walk of Fame typo (AP)
-
`Seinfeld' actress is tickled by Walk of Fame typo (AP)
-
Coroner: Haim died of pneumonia complications (AP)
-
Despite floods, Grand Ole Opry held across town (AP)
-
Thomson Reuters beats Street, CEO sees pickup (Reuters)
Sports News
-
Tigers' longtime broadcaster Harwell dead at 92 (AP)
-
Police report details murder suspects 2008 arrest (AP)
-
Woods trying to hit his stride (AP)
-
Philadelphia chief backs officer who Tasered fan (AP)
-
Nationals promote No. 1 pick Strasburg to Triple-A (AP)
-
Pacman Jones gets 2nd tryout with Bengals (AP)
-
Willis gets 5-year contract extension from 49ers (AP)
-
Braves manager Bobby Cox honored on Capitol Hill (AP)
-
Hornets interview Johnson, Casey for coaching job (AP)
-
Texas A&M grid coach visiting troops in Iraq (AP)
Technology News
-
Feds to look at Apple policy on programming tools (AP)
-
Roger Ebert, Jim Carrey among Webby Award winners (AP)
-
Time Warner Cable, Cox asked to drop ad claims (AP)
-
Kindle upgrade adds Facebook and Twitter (Christopher Null)
-
Craigslist faces new prostitution investigation (Christopher Null)
-
Airport security speedup program to relaunch (AP)
-
Billboard opens submissions for first Music App Awards (Macworld.com)
-
T-Mobile Shows Off New Android 3G Phone: myTouch 3G Slide (NewsFactor)
-
Studios still trying to figure out mobile video (Macworld.com)
-
For First Time in Six Years, Nintendo's Profits Drop (NewsFactor)
Setting Up the HTML
The tabs in the tabview widget will be represented by a list of
<a>
elements whose href
attribute is set to
the id of an <div>
element that contains its content.
Therefore, without JavaScript and CSS, the tabs function as in-page links.
<h3 id="tabview-heading">Today's News</h3> <div id="tabview-1"> <ul> <li class="yui3-tab yui-tab-selected"><a href="#top-stories"><em>Top Stories</em></a></li> <li class="yui3-tab"><a href="#world-news"><em>World</em></a></li> <li class="yui3-tab"><a href="#entertainment-news"><em>Entertainment</em></a></li> <li class="yui3-tab"><a href="#sports-news"><em>Sports</em></a></li> <li class="yui3-tab"><a href="#technology-news"><em>Technology</em></a></li> </ul> <div> <div class="yui3-tabpanel yui-tabpanel-selected" id="top-stories"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="world-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="entertainment-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="sports-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="technology-news"> <!-- Tab Panel Content Here --> </div> </div> </div>
<h3 id="tabview-heading">Today's News</h3> <div id="tabview-1"> <ul> <li class="yui3-tab yui-tab-selected"><a href="#top-stories"><em>Top Stories</em></a></li> <li class="yui3-tab"><a href="#world-news"><em>World</em></a></li> <li class="yui3-tab"><a href="#entertainment-news"><em>Entertainment</em></a></li> <li class="yui3-tab"><a href="#sports-news"><em>Sports</em></a></li> <li class="yui3-tab"><a href="#technology-news"><em>Technology</em></a></li> </ul> <div> <div class="yui3-tabpanel yui-tabpanel-selected" id="top-stories"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="world-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="entertainment-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="sports-news"> <!-- Tab Panel Content Here --> </div> <div class="yui3-tabpanel" id="technology-news"> <!-- Tab Panel Content Here --> </div> </div> </div>
For this example the content of each tab panel is created on the server using the YQL API to fetch the title and URL for news stories made available from the various Yahoo! News RSS feeds. Here's the PHP:
function getFeed($sFeed) { $params = array( "q" => ('select title,link from rss where url="http://rss.news.yahoo.com/rss/$sFeed"'), "format" => "json" ); $encoded_params = array(); foreach ($params as $k => $v) { $encoded_params[] = urlencode($k)."=".urlencode($v); } $url = "http://query.yahooapis.com/v1/public/yql?".implode("&", $encoded_params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $rsp = curl_exec($ch); curl_close($ch); if ($rsp !== false) { $rsp_obj = json_decode($rsp, true); $results = $rsp_obj["query"]["results"]["item"]; $list = ""; // HTML output $nResults = count($results); if ($nResults > 10) { $nResults = 9; } for ($i = 0; $i<= $nResults; $i++) { $result = $results[$i]; $list.= <<< END_OF_HTML <li> <a href="${result["link"]}"><q>${result["title"]}</q></a> </li> END_OF_HTML; } return ("<ul>" . $list . "</ul>"); } }
function getFeed($sFeed) { $params = array( "q" => ('select title,link from rss where url="http://rss.news.yahoo.com/rss/$sFeed"'), "format" => "json" ); $encoded_params = array(); foreach ($params as $k => $v) { $encoded_params[] = urlencode($k)."=".urlencode($v); } $url = "http://query.yahooapis.com/v1/public/yql?".implode("&", $encoded_params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $rsp = curl_exec($ch); curl_close($ch); if ($rsp !== false) { $rsp_obj = json_decode($rsp, true); $results = $rsp_obj["query"]["results"]["item"]; $list = ""; // HTML output $nResults = count($results); if ($nResults > 10) { $nResults = 9; } for ($i = 0; $i<= $nResults; $i++) { $result = $results[$i]; $list.= <<< END_OF_HTML <li> <a href="${result["link"]}"><q>${result["title"]}</q></a> </li> END_OF_HTML; } return ("<ul>" . $list . "</ul>"); } }
Progressive Enhancement
The markup above will be transformed using both CSS and JavaScript. To account
for the scenario where the user has CSS enabled in their browser but JavaScript
is disabled, the CSS used to style the tabview will be loaded via JavaScript
using the YUI instance's built-in Loader.
Additionally, a small block of JavaScript will be placed in the
<head>
used to temporarily hide the markup
while the JavaScript and CSS are in the process of loading to prevent the user
from seeing a flash unstyled content.
YUI({ base: "../../build/", modules: { "tabviewcss": { type: "css", fullpath: "assets/tabview.css" }, "tabviewjs": { type: "js", fullpath: "assets/tabview.js", requires: ["node-focusmanager", "tabviewcss"] } }, timeout: 10000 }).use("tabviewjs", function(Y, result) { // The callback supplied to use() will be executed regardless of // whether the operation was successful or not. The second parameter // is a result object that has the status of the operation. We can // use this to try to recover from failures or timeouts. if (!result.success) { Y.log("Load failure: " + result.msg, "warn", "Example"); // Show the tabview HTML if the loader failed that way the // original unskinned tabview will be visible so that the // user can interact with it either way. document.documentElement.className = ""; } });
YUI({ base: "../../build/", modules: { "tabviewcss": { type: "css", fullpath: "assets/tabview.css" }, "tabviewjs": { type: "js", fullpath: "assets/tabview.js", requires: ["node-focusmanager", "tabviewcss"] } }, timeout: 10000 }).use("tabviewjs", function(Y, result) { // The callback supplied to use() will be executed regardless of // whether the operation was successful or not. The second parameter // is a result object that has the status of the operation. We can // use this to try to recover from failures or timeouts. if (!result.success) { Y.log("Load failure: " + result.msg, "warn", "Example"); // Show the tabview HTML if the loader failed that way the // original unskinned tabview will be visible so that the // user can interact with it either way. document.documentElement.className = ""; } });
ARIA Support
Through the use of CSS and JavaScript the HTML for the tabview can be transformed into something that looks and behaves like a desktop tab control, but users of screen readers won't perceive it as an atomic widget, but rather simply as a set of HTML elements. However, through the application of the WAI-ARIA Roles and States, it is possible to improve the semantics of the markup such that users of screen readers perceive it as a tab control.
Keyboard Functionality
The keyboard functionality for the tabview widget will be provided by the
Focus Manager Node Plugin. The Focus Manager's
descendants
attribute is set to a value of ".yui3-tab>a", so that only one tab in the tabview
is in the browser's default tab flow. This allows users navigating via the
keyboard to use the tab key to quickly move into and out of the tabview. Once
the tabview has focus, the user can move focus among each tab using the left
and right arrows keys, as defined by the value of the
keys
attribute. Lastly, the
focusClass
attribute is used to apply a class of yui-tab-focus
to the parent
<li>
of each <a>
when it is focused,
making it easy to style the tab's focused state in each of the
A-Grade browsers.
YUI().use("*", function (Y) { var tabView = Y.one("#tabview-1"), tabList = tabView.one("ul"), tabHeading = Y.one("#tabview-heading"), sInstructionalText = tabHeading.get("innerHTML"); selectedTabAnchor = tabView.one(".yui3-tab-selected>a"), bGeckoIEWin = ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1), panelMap = {}; tabView.addClass("yui-tabview"); // Remove the "yui-loading" class from the documentElement // now that the necessary YUI dependencies are loaded and the // tabview has been skinned. tabView.get("ownerDocument").get("documentElement").removeClass("yui-loading"); // Apply the ARIA roles, states and properties. // Add some instructional text to the heading that will be read by // the screen reader when the first tab in the tabview is focused. tabHeading.set("innerHTML", (sInstructionalText + " <em>Press the enter key to load the content of each tab.</em>")); tabList.setAttrs({ "aria-labelledby": "tabview-heading", role: "tablist" }); tabView.one("div").set("role", "presentation"); tabView.plug(Y.Plugin.NodeFocusManager, { descendants: ".yui3-tab>a", keys: { next: "down:39", // Right arrow previous: "down:37" }, // Left arrow focusClass: { className: "yui-tab-focus", fn: function (node) { return node.get("parentNode"); } }, circular: true }); // If the list of tabs loses focus, set the activeDescendant // attribute to the currently selected tab. tabView.focusManager.after("focusedChange", function (event) { if (!event.newVal) { // The list of tabs has lost focus this.set("activeDescendant", selectedTabAnchor); } }); tabView.all(".yui3-tab>a").each(function (anchor) { var sHref = anchor.getAttribute("href", 2), sPanelID = sHref.substring(1, sHref.length), panel; // Apply the ARIA roles, states and properties to each tab anchor.set("role", "tab"); anchor.get("parentNode").set("role", "presentation"); // Remove the "href" attribute from the anchor element to // prevent JAWS and NVDA from reading the value of the "href" // attribute when the anchor is focused if (bGeckoIEWin) { anchor.removeAttribute("href"); } // Cache a reference to id of the tab's corresponding panel // element so that it can be made visible when the tab // is clicked. panelMap[anchor.get("id")] = sPanelID; // Apply the ARIA roles, states and properties to each panel panel = Y.one(("#" + sPanelID)); panel.setAttrs({ role: "tabpanel", "aria-labelledby": anchor.get("id") }); }); // Use the "delegate" custom event to listen for the "click" event // of each tab's <A> element. tabView.delegate("click", function (event) { var selectedPanel, sID = this.get("id"); // Deselect the currently selected tab and hide its // corresponding panel. if (selectedTabAnchor) { selectedTabAnchor.get("parentNode").removeClass("yui-tab-selected"); Y.one(("#" + panelMap[selectedTabAnchor.get("id")])).removeClass("yui-tabpanel-selected"); } selectedTabAnchor = this; selectedTabAnchor.get("parentNode").addClass("yui-tab-selected"); selectedPanel = Y.one(("#" + panelMap[sID])); selectedPanel.addClass("yui-tabpanel-selected"); creatingPaging(selectedPanel); // Prevent the browser from following the URL specified by the // anchor's "href" attribute when clicked. event.preventDefault(); }, ".yui3-tab>a"); // Since the anchor's "href" attribute has been removed, the // element will not fire the click event in Firefox when the // user presses the enter key. To fix this, dispatch the // "click" event to the anchor when the user presses the // enter key. if (bGeckoIEWin) { tabView.delegate("keydown", function (event) { if (event.charCode === 13) { this.simulate("click"); } }, ">ul>li>a"); } });
YUI().use("*", function (Y) { var tabView = Y.one("#tabview-1"), tabList = tabView.one("ul"), tabHeading = Y.one("#tabview-heading"), sInstructionalText = tabHeading.get("innerHTML"); selectedTabAnchor = tabView.one(".yui3-tab-selected>a"), bGeckoIEWin = ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1), panelMap = {}; tabView.addClass("yui-tabview"); // Remove the "yui-loading" class from the documentElement // now that the necessary YUI dependencies are loaded and the // tabview has been skinned. tabView.get("ownerDocument").get("documentElement").removeClass("yui-loading"); // Apply the ARIA roles, states and properties. // Add some instructional text to the heading that will be read by // the screen reader when the first tab in the tabview is focused. tabHeading.set("innerHTML", (sInstructionalText + " <em>Press the enter key to load the content of each tab.</em>")); tabList.setAttrs({ "aria-labelledby": "tabview-heading", role: "tablist" }); tabView.one("div").set("role", "presentation"); tabView.plug(Y.Plugin.NodeFocusManager, { descendants: ".yui3-tab>a", keys: { next: "down:39", // Right arrow previous: "down:37" }, // Left arrow focusClass: { className: "yui-tab-focus", fn: function (node) { return node.get("parentNode"); } }, circular: true }); // If the list of tabs loses focus, set the activeDescendant // attribute to the currently selected tab. tabView.focusManager.after("focusedChange", function (event) { if (!event.newVal) { // The list of tabs has lost focus this.set("activeDescendant", selectedTabAnchor); } }); tabView.all(".yui3-tab>a").each(function (anchor) { var sHref = anchor.getAttribute("href", 2), sPanelID = sHref.substring(1, sHref.length), panel; // Apply the ARIA roles, states and properties to each tab anchor.set("role", "tab"); anchor.get("parentNode").set("role", "presentation"); // Remove the "href" attribute from the anchor element to // prevent JAWS and NVDA from reading the value of the "href" // attribute when the anchor is focused if (bGeckoIEWin) { anchor.removeAttribute("href"); } // Cache a reference to id of the tab's corresponding panel // element so that it can be made visible when the tab // is clicked. panelMap[anchor.get("id")] = sPanelID; // Apply the ARIA roles, states and properties to each panel panel = Y.one(("#" + sPanelID)); panel.setAttrs({ role: "tabpanel", "aria-labelledby": anchor.get("id") }); }); // Use the "delegate" custom event to listen for the "click" event // of each tab's <A> element. tabView.delegate("click", function (event) { var selectedPanel, sID = this.get("id"); // Deselect the currently selected tab and hide its // corresponding panel. if (selectedTabAnchor) { selectedTabAnchor.get("parentNode").removeClass("yui-tab-selected"); Y.one(("#" + panelMap[selectedTabAnchor.get("id")])).removeClass("yui-tabpanel-selected"); } selectedTabAnchor = this; selectedTabAnchor.get("parentNode").addClass("yui-tab-selected"); selectedPanel = Y.one(("#" + panelMap[sID])); selectedPanel.addClass("yui-tabpanel-selected"); creatingPaging(selectedPanel); // Prevent the browser from following the URL specified by the // anchor's "href" attribute when clicked. event.preventDefault(); }, ".yui3-tab>a"); // Since the anchor's "href" attribute has been removed, the // element will not fire the click event in Firefox when the // user presses the enter key. To fix this, dispatch the // "click" event to the anchor when the user presses the // enter key. if (bGeckoIEWin) { tabView.delegate("keydown", function (event) { if (event.charCode === 13) { this.simulate("click"); } }, ">ul>li>a"); } });
Accessibility Sugar
One of the challenges faced by users of screen readers is knowing when you've left the context of a given control. In the case of this tabview, if it were adjacent to another ARIA-enabled widget, the user would know they've left the tabview when the screen reader announces the role of the adjacent widget. However, if the tabview is sitting alongside standard HTML content, it would be really difficult for the user to know when they've left the context of the active panel.
One solution to this problem is to add some additional navigation as the last child of each tab panel that allows the user to move to the previous and next panel in the tabview. This will not only help alert users of screen readers that they've reached the end of the tab's panel, but allow all keyboard users to move more quickly to the next/previous panel. Without this additionally navigation, keyboard users would typically have to press shift+tab to navigate back up to the list of tabs to move to the next/previous tab.
var creatingPaging = function (panel) { var listitem, sHTML, paging; if (!panel.one(".paging")) { listitem = selectedTabAnchor.get("parentNode"); sHTML = ""; if (listitem.previous()) { sHTML += '<button type="button" class="yui3-tabview-prevbtn">Previous Tab Panel</button>'; } if (listitem.next()) { sHTML += '<button type="button" class="yui3-tabview-nextbtn">Next Tab Panel</button>'; } paging = Y.Node.create('<div class="paging">' + sHTML + '</div>'); panel.appendChild(paging); } }; creatingPaging(Y.one(".yui3-tabpanel-selected")); tabView.delegate("click", function (event) { var node = selectedTabAnchor.get("parentNode").previous().one("a"); tabView.focusManager.focus(node); node.simulate("click"); }, ".yui3-tabview-prevbtn"); tabView.delegate("click", function (event) { var node = selectedTabAnchor.get("parentNode").next().one("a"); tabView.focusManager.focus(node); node.simulate("click"); }, ".yui3-tabview-nextbtn");
var creatingPaging = function (panel) { var listitem, sHTML, paging; if (!panel.one(".paging")) { listitem = selectedTabAnchor.get("parentNode"); sHTML = ""; if (listitem.previous()) { sHTML += '<button type="button" class="yui3-tabview-prevbtn">Previous Tab Panel</button>'; } if (listitem.next()) { sHTML += '<button type="button" class="yui3-tabview-nextbtn">Next Tab Panel</button>'; } paging = Y.Node.create('<div class="paging">' + sHTML + '</div>'); panel.appendChild(paging); } }; creatingPaging(Y.one(".yui3-tabpanel-selected")); tabView.delegate("click", function (event) { var node = selectedTabAnchor.get("parentNode").previous().one("a"); tabView.focusManager.focus(node); node.simulate("click"); }, ".yui3-tabview-prevbtn"); tabView.delegate("click", function (event) { var node = selectedTabAnchor.get("parentNode").next().one("a"); tabView.focusManager.focus(node); node.simulate("click"); }, ".yui3-tabview-nextbtn");