<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stephen Cunningham on Medium]]></title>
        <description><![CDATA[Stories by Stephen Cunningham on Medium]]></description>
        <link>https://medium.com/@stevecunningham_15009?source=rss-13bd47e6a961------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BcCzYpGyo4LwxDaBV9tJhA.jpeg</url>
            <title>Stories by Stephen Cunningham on Medium</title>
            <link>https://medium.com/@stevecunningham_15009?source=rss-13bd47e6a961------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:16:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stevecunningham_15009/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Keyboard Traps and Tabbing Order with React]]></title>
            <link>https://medium.com/@stevecunningham_15009/keyboard-traps-and-tabbing-order-with-react-9ae82fd43c?source=rss-13bd47e6a961------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ae82fd43c</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Stephen Cunningham]]></dc:creator>
            <pubDate>Sun, 23 Jul 2023 00:32:36 GMT</pubDate>
            <atom:updated>2023-07-23T00:32:36.355Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Here are the solutions to the keyboard accessibility issues I faced while using React.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ILhkFFrDfLvKqAnhCEyZ-A.jpeg" /><figcaption>Keyboard Vectors by V<a href="https://www.vecteezy.com/free-vector/keyboard">ecteezy</a></figcaption></figure><p><a href="https://webaim.org/techniques/keyboard/">Keyboard accessibility</a> is important for developing products with a diversity of users. I struggled with two particular cases using <a href="https://react.dev/">React</a> that impeded certain aspects of keyboard accessibility. In this article, I will discuss what I did to solve these two interesting corner cases of keyboard accessibility with React.</p><p><strong>Tab Order Issue</strong></p><p>Consider we have a list of objects with different states that we use to render input components. The inputs for each object in the list is conditionally rendered based on a boolean state variable. An example can be shown below.</p><pre>const [boolCondition, setBoolCondition] = useState(false);<br>const [anotherBoolCondition, setAnotherBoolCondition] = useState(false);<br><br>const exampleList = [<br>{title: first example, showExample: boolCondition, value: someValue, deSelect: () =&gt; setBoolCondition(false)},<br>{title: second example, showExample: anotherBoolCondition, deSelect: () =&gt; setAnotherBoolCondition(false), value: someValue}<br>]<br><br>const renderFunction = (data) =&gt; (<br><br>{data.showExample &amp;&amp;<br>&lt;div&gt;<br>&lt;input&gt;data.title&lt;/input&gt;<br>&lt;button onClick={data.deSelect()}&gt;Remove&lt;/button&gt;<br>&lt;/div&gt;<br>}<br>)<br><br>return (<br>&lt;form&gt;<br>exampleList.map((example) =&gt; renderFunction(example);<br><br>&lt;/form&gt;<br>)</pre><p>Although conditionally rendering inputs based on the state variable was functionally successful, it resulted in an improper <a href="https://accessibility.digital.gov/ux/tab-order/">tabbing order</a>.</p><p>By tabbing order I mean I could not go through the input boxes by hitting the tab key. Pressing the tab key allow focus to shift in a logical order. But, conditional rendering this way disrupted the natural tab order, and violates accessibility concerns with product. The reason is most likely <a href="https://react.dev/learn/preserving-and-resetting-state">React not preserving the state of input once it is removed</a> or position in the UI tree has changed.</p><p>It is tempting to alter the tabindex to manually place focus in a logical order. An example would be</p><p>tabIndex = {example.showInput ? &#39;0&#39; : &#39;-1&#39;} on the div for each input.</p><p>But, inputs should have a natural logical tab order based on their placement in the document. Altering a tab index on interactive elements (like, buttons and links) <a href="https://www.a11yproject.com/posts/how-to-use-the-tabindex-attribute/">may lead to unexpected behavior from the browser</a>.</p><p>Since the order the interactive element, in our case, the input, is rendered is important for tab order, conditional rendering elements in the list may confuse the render order.<strong> Another way to conditionally render is with CSS</strong>.</p><p>The input will then look more like:</p><p>&lt;div id={data.showExample ? &#39;showing&#39; : &#39;not-showing&#39;}</p><p>Then all I need is a CSS class with only a hidden visibility:</p><p>#not-showing: { visibility: hidden; }</p><p>Altogether, the result would look like this:</p><pre>const [boolCondition, setBoolCondition] = useState(false);<br>const [anotherBoolCondition, setAnotherBoolCondition] = useState(false);<br><br>const exampleList = [<br>{title: first example, showExample: boolCondition, value: someValue, deSelect: () =&gt; setBoolCondition(false)},<br>{title: second example, showExample: anotherBoolCondition, deSelect: () =&gt; setAnotherBoolCondition(false), value: someValue}<br>]<br><br>const renderFunction = (data) =&gt; (<br><br><br>&lt;div id={data.showExample ? &#39;showing&#39; : &#39;not-showing&#39;}&gt;<br>&lt;input&gt;data.title&lt;/input&gt;<br>&lt;button onClick={data.deSelect()}&gt;Remove&lt;/button&gt;<br>&lt;/div&gt;<br><br>)<br><br>return (<br>&lt;form&gt;<br>exampleList.map((example) =&gt; renderFunction(example);<br><br>&lt;/form&gt;<br>)</pre><p>Now, I will discuss rendering a list of an unknown size, and the tabbing issues tackled in that situation.</p><p><strong>Keyboard Trap Issue</strong></p><p>The following is code that lead to a <a href="https://www.csun.edu/universal-design-center/web-accessibility-criteria-keyboard-traps#:~:text=Keyboard%20traps%20occur%20when%20a,menus%2C%20or%20even%20on%20hyperlinks.">keyboard trap</a>, or focus remaining stuck on an interactive element. This code rendered a table with the rows being populated from input data. For the example, I reduced all the code to just the checkboxes input.</p><pre>export default function KeyBoardTrapExample( data) {<br><br>const [mockCopyList, setMockCopyList] = useState(data.mockList);<br><br>const IncorrectRenderTableBody = (props) =&gt; {<br><br>return (<br><br>data.mockList.map((element, index) =&gt; {<br><br>const [boxIsChecked, setBoxIsChecked] = useState(element.FirstChecked);<br>const [nextBoxIsChecked, setNextBoxIsChecked] = useState(element.SecondChecked);<br><br>return (<br><br>&lt;tr&gt;<br>  &lt;td&gt;<br>    &lt;input type=&quot;checkbox&quot;<br>          onChange={() =&gt; setBoxIsChecked(e.target.checked)}&gt;&lt;/input&gt;<br>  &lt;/td&gt;<br>  &lt;td&gt;<br>  &lt;input type=&quot;checkbox&quot;<br>          onChange={() =&gt; setNextBoxIsChecked(e.target.checked)}&gt;&lt;/input&gt;<br>  &lt;/td&gt;<br>&lt;/tr&gt;<br><br>)<br><br>});<br><br><br>}<br><br>return (<br>&lt;Table&gt;<br>  &lt;tbody&gt;<br>  <br>  &lt;IncorrectRenderTableBody /&gt;  <br><br>  &lt;/tbody&gt;<br>&lt;/Table&gt;<br><br>)<br><br>}</pre><p>The choice to place the table body as its own component was enticing because the IncorrectRenderTableBody was able to manage the state of the checkboxes on change. However, this results in a “crazy losing focus behavior” as cited <a href="https://levelup.gitconnected.com/react-text-input-losing-focus-after-each-keypress-dd6eabfe2f7c">here</a>. In this case, it was a keyboard trap where focus got stuck on the exit button and submit button (not shown here in code).</p><p>The solution, was similar to the <a href="https://levelup.gitconnected.com/react-text-input-losing-focus-after-each-keypress-dd6eabfe2f7c">article cited above</a>, by changing the React component IncorrectRenderTableBody to be a “top-level” function.</p><p>The problem was that now that IncorrectRenderTableBody is a function, the state of the checkboxes was not updating. In other words, the checkboxes were not “checking”. I would have to check another box to initiate a re-render that showed the previously checked box with a check mark.</p><p>The solution was to use React’s onKeyDown and onMouseDown methods to update the state of the input checkboxes.</p><p>The keyDown events would check if the Enter button or Spacebar button were pressed to ‘check’ the box. The Enter button and Spacebar buttons were identified by their javascript key codes, which can be found <a href="https://www.toptal.com/developers/keycode">here</a>. Once it was determined that one of those buttons was pressed, the code iterated through the list and updated the state of the element in the list and thus, updated the checkbox. The example is shown below.</p><pre>const keyDown = (event, givenIndex) =&gt; {<br><br>if(event.keyCode === 32 || e.keyCode === 13) {<br><br>  setMockCopyList(<br>      mockCopyList.map((item, index) =&gt; {<br><br>    if (index === givenIndex) {<br>      return {item, FirstChecked: !item.FirstChecked}<br>}<br><br>}<br><br>}<br><br>}</pre><p>The mouse down will have the same logic but without the keyCode ‘if’ statement.</p><p>The reason is mainly that in an onChange event, the event is not triggered until the interactive element is out of focus. <a href="https://www.willmaster.com/library/tutorials/onkeydown-onkeyup-onclick-and-onchange-javascript-events.php">Only once the field is out of focus will Javascript know that the action is complete</a>. But, with a key-down or mouse-down event, the event is triggered as soon as the action occurs.</p><p>The result is as follows</p><pre>export default function KeyBoardTrapExample( data) {<br><br>const [mockCopyList, setMockCopyList] = useState(data.mockList);<br><br><br>const keyDown = (event, givenIndex) =&gt; {<br><br>if(event.keyCode === 32 || e.keyCode === 13) {<br><br>  setMockCopyList(<br>      mockCopyList.map((item, index) =&gt; {<br><br>    if (index === givenIndex) {<br>      return {item, FirstChecked: !item.FirstChecked};<br>      }<br><br>      })<br><br>    }<br><br>}<br><br>const nextKeyDown = (event, givenIndex) =&gt; {<br><br>if(event.keyCode === 32 || e.keyCode === 13) {<br><br>  setMockCopyList(<br>      mockCopyList.map((item, index) =&gt; {<br><br>    if (index === givenIndex) {<br>      return {item, SecondChecked: !item.SecondChecked};<br>      }<br><br>      })<br><br>    }<br><br>}<br><br>const CorrectRenderTableBody = (props) =&gt; {<br><br>return (<br><br>data.mockList.map((element, index) =&gt; {<br><br>return (<br><br>&lt;tr&gt;<br>  &lt;td&gt;<br>    &lt;input type=&quot;checkbox&quot;<br>          onKeyDown={(e) =&gt; keyDown(e, index)}<br>           onMouseDown={() =&gt; {<br>          setMockCopyList(mockCopyList.map((item, iterator) =&gt; {<br>      <br>                if(index === iterator) {<br>                    return {item, FirstChecked: !item.FirstChecked};<br>                    }<br>                }<br>              }<br>            }&gt;&lt;/input&gt;<br>  &lt;/td&gt;<br>  &lt;td&gt;<br>    &lt;input type=&quot;checkbox&quot;<br>          onKeyDown={(e) =&gt; nextKeyDown(e, index)}<br>           onMouseDown={() =&gt; {<br>          setMockCopyList(mockCopyList.map((item, iterator) =&gt; {<br>      <br>                if(index === iterator) {<br>                    return {item, SecondChecked: !item.SecondChecked};<br>                    }<br>                }<br>              }<br>            }&gt;&lt;/input&gt;<br>  &lt;/td&gt;<br>&lt;/tr&gt;<br><br>)<br><br>});<br><br>}<br><br>return (<br>&lt;Table&gt;<br>  &lt;tbody&gt;<br>  <br>  {CorrectRenderTableBody()} <br><br>  &lt;/tbody&gt;<br>&lt;/Table&gt;<br><br>)<br><br>}</pre><p><strong>Conclusion</strong></p><p>Conditional rendering with state variables altered the position of my elements in the UI tree and disrupted the natural, logical tab order for interactive elements. The solution was conditionally rendering with CSS elements. Also, rendering top-level functions helped to prevent keyboard traps. Additionally, understanding the nuances between the different built-in functions that handle change assisted with updating state appropriately for my checkboxes.</p><p>I may have oversimplified some of the code. This code is loosely inspired by accessibility errors I came across. But, hopefully there is something in here useful. Thanks for working to make coding products keyboard accessible to users.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ae82fd43c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>