Skip to content

Commit b38c00e

Browse files
committed
Update toggle utility to switch tabindex of the target's children
1 parent 0e197c7 commit b38c00e

3 files changed

Lines changed: 213 additions & 33 deletions

File tree

dist/toggle.html

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,57 @@ <h3 id="javascript">JavaScript</h3>
2121

2222
new Toggle();</code></pre>
2323
<h3 id="markup">Markup</h3>
24-
<pre><code>&lt;button aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;true&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;
24+
<p>Elements should have the hidden or active state set before initialization.</p>
25+
<p>
26+
<strong>Hidden</strong>
27+
</p>
28+
<pre><code>&lt;button aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;false&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;
29+
Toggle
30+
&lt;/button&gt;
31+
32+
&lt;div aria-hidden=&quot;true&quot; class=&quot;hidden&quot; id=&quot;toggle-target&quot;&gt;
33+
&lt;p&gt;Targeted Toggle Element&lt;/p&gt;
34+
&lt;/div&gt;</code></pre>
35+
<p>
36+
<strong>Active</strong>
37+
</p>
38+
<pre><code>&lt;button class=&quot;active&quot; aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;true&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;
2539
Toggle
2640
&lt;/button&gt;
2741

2842
&lt;div aria-hidden=&quot;false&quot; class=&quot;active&quot; id=&quot;toggle-target&quot;&gt;
29-
Targeted Toggle Element
43+
&lt;p&gt;Targeted Toggle Element&lt;/p&gt;
3044
&lt;/div&gt;</code></pre>
3145
<p>The use of the dynamic <code>aria-expanded</code> attribute on the toggling element is recommended for toggling elements as it will announce that the target of the toggle is “expanded” or “collapsed.” Optionally, the attribute <code>aria-pressed</code> can be used instead to announce that the toggle button is “pressed” or “not pressed”. These attributes provide different feedback to screenreaders and are appropriate for different component types. <code>aria-expanded</code> would be used for patterns such as <a href="https://inclusive-components.design/collapsible-sections/">
3246
<strong>collapsible sections</strong>
3347
</a> and <code>aria-pressed</code> would be used for <a href="https://inclusive-components.design/toggle-button/">
3448
<strong>toggle buttons</strong>
3549
</a> or <strong>switches</strong>. A full list of dynamic and static attributes is described below.</p>
3650
<p>Placement of the target should follow the toggling element so that it appears next in order on the page for screen readers. For targets that are far apart or appear in a different section of the page, the Anchor Toggle may be more appropriate.</p>
37-
<p>The Toggle Utility supports having more than one toggle element per toggle target.</p>
51+
<p>Elements that have aria-hidden set to <code>true</code> should not contain focusable elements. Setting their tabindex to <code>-1</code> will prevent them from being focused on. For convenience, child elements in the target element that have their <code>tabindex</code> set will be toggled.</p>
52+
<pre><code>&lt;button aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;false&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;
53+
Toggle
54+
&lt;/button&gt;
55+
56+
&lt;div aria-hidden=&quot;true&quot; class=&quot;hidden&quot; id=&quot;toggle-target&quot;&gt;
57+
&lt;p&gt;Targeted Toggle Element&lt;/p&gt;
58+
59+
&lt;a href=&#39;#&#39; tabindex=&quot;-1&quot;&gt;A Focusable Child Element&lt;/a&gt;
60+
&lt;/div&gt;</code></pre>
61+
<p>The Toggle Utility supports having more than one toggle element per toggle target. An example use case is for “close” buttons within dialogue elements.</p>
62+
<pre><code>&lt;button aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;false&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;
63+
Toggle
64+
&lt;/button&gt;
65+
66+
&lt;div aria-hidden=&quot;true&quot; class=&quot;hidden&quot; id=&quot;toggle-target&quot;&gt;
67+
&lt;p&gt;Targeted Toggle Element&lt;/p&gt;
68+
69+
&lt;button tabindex=&quot;-1&quot; aria-controls=&quot;main-menu&quot; aria-expanded=&quot;false&quot; data-js=&quot;toggle&quot;&gt;
70+
Close
71+
&lt;/button&gt;
72+
&lt;/div&gt;</code></pre>
3873
<h3 id="attributes">Attributes</h3>
39-
<p>Attributes such as <code>aria-controls</code>, <code>aria-expanded</code>, and <code>type</code> will help assistive technologies understand the relationship between the toggle element and the toggle target. These three attributes should be considered the bare minimum but they may be interchanged with others based on the use case. Below is an explanation of other possible attributes that can be used with the toggle utility. <em>Static</em> attributes will not change. <em>Dynamic</em> attributes will change when the toggle event is fired.</p>
74+
<p>Attributes on the Element, Target, and Target Children, such as <code>aria-hidden</code>, <code>aria-controls</code>, <code>aria-expanded</code>, <code>type</code>, and <code>tabindex</code> help assistive technologies understand the relationship between each element and their respective states of visibility. These attributes should be present but they may be interchanged with others based on the use case. Below is an explanation of all attributes that can be used with the toggle utility. <em>Static</em> attributes will not change. <em>Dynamic</em> attributes will change when the toggle event is fired.</p>
4075
<p>
4176
<strong>Toggling Element Attributes</strong>
4277
</p>
@@ -57,7 +92,9 @@ <h3 id="attributes">Attributes</h3>
5792
<td>
5893
<em>static</em>
5994
</td>
60-
<td>required</td>
95+
<td>
96+
<strong>required</strong>
97+
</td>
6198
<td>ID of the target element. Used by the toggle to select the target element.</td>
6299
</tr>
63100
<tr>
@@ -88,7 +125,7 @@ <h3 id="attributes">Attributes</h3>
88125
<em>dynamic</em>
89126
</td>
90127
<td>optional</td>
91-
<td>Boolean that announces that the toggling element is toggled. Not recommended for use with <code>aria-expanded</code>.</td>
128+
<td>Boolean that announces that the toggling element is toggled. Not recommended for use with <code>aria-expanded</code>. Commonly used with buttons that act as switches for options that are on or off.</td>
92129
</tr>
93130
<tr>
94131
<td>
@@ -147,6 +184,47 @@ <h3 id="attributes">Attributes</h3>
147184
</tr>
148185
</tbody>
149186
</table>
187+
<p>
188+
<strong>Target Element Child Attributes</strong>
189+
</p>
190+
<table>
191+
<thead>
192+
<tr>
193+
<th>Attribute</th>
194+
<th>State</th>
195+
<th>Importance</th>
196+
<th>Description</th>
197+
</tr>
198+
</thead>
199+
<tbody>
200+
<tr>
201+
<td>
202+
<code>tabindex</code>
203+
</td>
204+
<td>
205+
<em>dynamic</em>
206+
</td>
207+
<td>recommended</td>
208+
<td>Setting the toggle target’s focusable children’s <code>tabindex</code> attribute to “-1” will prevent them from being focused on when the parent is hidden. See the list below of potentially focusable elements that are supported.</td>
209+
</tr>
210+
<tr>
211+
<td>
212+
<code>data-toggle-tabindex</code>
213+
</td>
214+
<td>
215+
<em>static</em>
216+
</td>
217+
<td>optional</td>
218+
<td>If an child element has a <code>tabindex</code> that needs to be set when the parent target is visible then the default value can be stored in this data attribute.</td>
219+
</tr>
220+
</tbody>
221+
</table>
222+
<p>
223+
<strong>Potentially Focusable Elements</strong>
224+
</p>
225+
<pre><code>a, button, input, select, textarea, object, embed, form,
226+
fieldset, legend, label, area, audio, video, iframe, svg,
227+
details, table, [tabindex], [contenteditable], [usemap]</code></pre>
150228
<h3 id="configuration">Configuration</h3>
151229
<p>The Toggle Utility accepts an object <code>{}</code> with the following properties:</p>
152230
<table>
@@ -197,7 +275,7 @@ <h3 id="configuration">Configuration</h3>
197275
<em>function</em>
198276
</td>
199277
<td>optional</td>
200-
<td>A function that will be executed before the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class.</td>
278+
<td>A function that will be executed before the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class with several values that may be useful in the callback such as the settings, toggle element, toggle target, and initial click event.</td>
201279
</tr>
202280
<tr>
203281
<td>
@@ -207,7 +285,7 @@ <h3 id="configuration">Configuration</h3>
207285
<em>function</em>
208286
</td>
209287
<td>optional</td>
210-
<td>A function that will be executed after the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class.</td>
288+
<td>A function that will be executed after the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class with several values that may be useful in the callback such as the settings, toggle element, toggle target, and initial click event.</td>
211289
</tr>
212290
</tbody>
213291
</table>
@@ -219,6 +297,7 @@ <h2 id="demo">Demo</h2>
219297
<div aria-hidden="false" class="bg-color-blue-light p-4 text-center active" id="toggle-target">Targeted toggle element</div>
220298
</p> &lt;button aria-controls=&quot;toggle-target&quot; aria-expanded=&quot;true&quot; class=&quot;btn btn-primary mb-3&quot; data-js=&quot;toggle&quot; type=&quot;button&quot;&gt;Toggle&lt;/button&gt; &lt;div aria-hidden=&quot;false&quot; class=&quot;bg-color-blue-light p-4 text-center active&quot; id=&quot;toggle-target&quot;&gt;Targeted toggle element&lt;/div&gt;
221299
</main>
300+
<script src="/reload/reload.js"></script>
222301
</body>
223302

224303
</html>

src/utilities/toggle/readme.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,70 @@ The Toggle utility uses JavaScript to expand and collapse elements based on user
1212

1313
### Markup
1414

15-
<button aria-controls="toggle-target" aria-expanded="true" data-js="toggle" type="button">
15+
Elements should have the hidden or active state set before initialization.
16+
17+
**Hidden**
18+
19+
<button aria-controls="toggle-target" aria-expanded="false" data-js="toggle" type="button">
20+
Toggle
21+
</button>
22+
23+
<div aria-hidden="true" class="hidden" id="toggle-target">
24+
<p>Targeted Toggle Element</p>
25+
</div>
26+
27+
**Active**
28+
29+
<button class="active" aria-controls="toggle-target" aria-expanded="true" data-js="toggle" type="button">
1630
Toggle
1731
</button>
1832

1933
<div aria-hidden="false" class="active" id="toggle-target">
20-
Targeted Toggle Element
34+
<p>Targeted Toggle Element</p>
2135
</div>
2236

2337
The use of the dynamic `aria-expanded` attribute on the toggling element is recommended for toggling elements as it will announce that the target of the toggle is "expanded" or "collapsed." Optionally, the attribute `aria-pressed` can be used instead to announce that the toggle button is "pressed" or "not pressed". These attributes provide different feedback to screenreaders and are appropriate for different component types. `aria-expanded` would be used for patterns such as [**collapsible sections**](https://inclusive-components.design/collapsible-sections/) and `aria-pressed` would be used for [**toggle buttons**](https://inclusive-components.design/toggle-button/) or **switches**. A full list of dynamic and static attributes is described below.
2438

2539
Placement of the target should follow the toggling element so that it appears next in order on the page for screen readers. For targets that are far apart or appear in a different section of the page, the Anchor Toggle may be more appropriate.
2640

27-
The Toggle Utility supports having more than one toggle element per toggle target.
41+
Elements that have aria-hidden set to `true` should not contain focusable elements. Setting their tabindex to `-1` will prevent them from being focused on. For convenience, child elements in the target element that have their `tabindex` set will be toggled.
42+
43+
<button aria-controls="toggle-target" aria-expanded="false" data-js="toggle" type="button">
44+
Toggle
45+
</button>
46+
47+
<div aria-hidden="true" class="hidden" id="toggle-target">
48+
<p>Targeted Toggle Element</p>
49+
50+
<a href='#' tabindex="-1">A Focusable Child Element</a>
51+
</div>
52+
53+
The Toggle Utility supports having more than one toggle element per toggle target. An example use case is for "close" buttons within dialogue elements.
54+
55+
<button aria-controls="toggle-target" aria-expanded="false" data-js="toggle" type="button">
56+
Toggle
57+
</button>
58+
59+
<div aria-hidden="true" class="hidden" id="toggle-target">
60+
<p>Targeted Toggle Element</p>
61+
62+
<button tabindex="-1" aria-controls="main-menu" aria-expanded="false" data-js="toggle">
63+
Close
64+
</button>
65+
</div>
2866

2967
### Attributes
3068

31-
Attributes such as `aria-controls`, `aria-expanded`, and `type` will help assistive technologies understand the relationship between the toggle element and the toggle target. These three attributes should be considered the bare minimum but they may be interchanged with others based on the use case. Below is an explanation of other possible attributes that can be used with the toggle utility. *Static* attributes will not change. *Dynamic* attributes will change when the toggle event is fired.
69+
Attributes on the Element, Target, and Target Children, such as `aria-hidden`, `aria-controls`, `aria-expanded`, `type`, and `tabindex` help assistive technologies understand the relationship between each element and their respective states of visibility. These attributes should be present but they may be interchanged with others based on the use case. Below is an explanation of all attributes that can be used with the toggle utility. *Static* attributes will not change. *Dynamic* attributes will change when the toggle event is fired.
3270

3371
**Toggling Element Attributes**
3472

3573
Attribute | State | Importance | Description
3674
----------------|-----------|---------------|-
37-
`aria-controls` | *static* | required | ID of the target element. Used by the toggle to select the target element.
75+
`aria-controls` | *static* | **required** | ID of the target element. Used by the toggle to select the target element.
3876
`aria-expanded` | *dynamic* | recommended | Boolean that announces that target content is "expanded" or "collapsed" when the toggling element is clicked.
3977
`type` | *static* | recommended | Setting a `<button>` element type to "button" will distinguish it from other button types, such as "submit" and "reset," but only within `<form>` elements. By default, a `<button>` is the type "submit" within a form.
40-
`aria-pressed` | *dynamic* | optional | Boolean that announces that the toggling element is toggled. Not recommended for use with `aria-expanded`.
78+
`aria-pressed` | *dynamic* | optional | Boolean that announces that the toggling element is toggled. Not recommended for use with `aria-expanded`. Commonly used with buttons that act as switches for options that are on or off.
4179
`role` | *static* | optional | If the toggling element is not a `<button>` element, but looks and behaves like a button (see documentation for the [Button Element](/buttons)), then setting the `role` attribute to "button" is recommended. See [MDN documentation for the "button" role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role) for more information
4280

4381
**Target Element Attributes**
@@ -48,6 +86,19 @@ Attribute | State | Importance | Description
4886
`role` | *static* | optional | Setting the target element's `role` to "region" identifies the target as a significant area. See [MDN documentation for the "region" role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Region_role) for more information.
4987
`aria-labelledby` | *static* | optional | This is used along with the `role` attribute to label the content of a "region." This can be set to the toggling elements `id` but can also be set to a different elements `id`.
5088

89+
**Target Element Child Attributes**
90+
91+
Attribute | State | Importance | Description
92+
-----------------------|-----------|---------------|-
93+
`tabindex` | *dynamic* | recommended | Setting the toggle target's focusable children's `tabindex` attribute to "-1" will prevent them from being focused on when the parent is hidden. See the list below of potentially focusable elements that are supported.
94+
`data-toggle-tabindex` | *static* | optional | If an child element has a `tabindex` that needs to be set when the parent target is visible then the default value can be stored in this data attribute.
95+
96+
**Potentially Focusable Elements**
97+
98+
a, button, input, select, textarea, object, embed, form,
99+
fieldset, legend, label, area, audio, video, iframe, svg,
100+
details, table, [tabindex], [contenteditable], [usemap]
101+
51102
### Configuration
52103

53104
The Toggle Utility accepts an object `{}` with the following properties:
@@ -57,8 +108,8 @@ Option | Type | Importance | Description
57108
`selector` | *string* | optional | Full selector string of the toggle element (this is passed to the `.matches()` method).
58109
`inactiveClass` | *string/boolean* | optional | Single class name that will be toggled on the toggle and target element when the element is inactive or "collapsed." Pass "false" to skip toggling an inactive class (there is no inactive class for the toggle element).
59110
`activeClass` | *string/boolean* | optional | Single class name that will be toggled on the target element when the element is active or "expanded." Pass "false" to skip toggling an active class.
60-
`before` | *function* | optional | A function that will be executed before the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class.
61-
`after` | *function* | optional | A function that will be executed after the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class.
111+
`before` | *function* | optional | A function that will be executed before the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class with several values that may be useful in the callback such as the settings, toggle element, toggle target, and initial click event.
112+
`after` | *function* | optional | A function that will be executed after the toggling element and target classes and attributes are toggled. The function is passed the instance of the toggle class with several values that may be useful in the callback such as the settings, toggle element, toggle target, and initial click event.
62113

63114
## Polyfills
64115

0 commit comments

Comments
 (0)