<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tota11y.dev</title>
    <description>The latest articles on DEV Community by tota11y.dev (@tota11ydev).</description>
    <link>https://dev.to/tota11ydev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F7559%2Fec11ef24-a2d1-4a77-baa2-0631428bdd83.png</url>
      <title>DEV Community: tota11y.dev</title>
      <link>https://dev.to/tota11ydev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tota11ydev"/>
    <language>en</language>
    <item>
      <title>Accordions: 1, 5 and 10 minute versions...all accessible [quicka11y]</title>
      <dc:creator>GrahamTheDev</dc:creator>
      <pubDate>Tue, 26 Sep 2023 13:43:24 +0000</pubDate>
      <link>https://dev.to/tota11ydev/accordions-1-5-and-10-minute-versionsall-accessible-quicka11y-2d3b</link>
      <guid>https://dev.to/tota11ydev/accordions-1-5-and-10-minute-versionsall-accessible-quicka11y-2d3b</guid>
      <description>&lt;p&gt;I saw a post a few days ago that was a "CSS only accordion". &lt;/p&gt;

&lt;p&gt;Sadly the techniques used were not very accessible. Plus, they made things so very difficult for themselves!&lt;/p&gt;

&lt;p&gt;However, I did at least get inspired by that post.&lt;/p&gt;

&lt;p&gt;So if you liked that post, then this post will blow your mind, I am going to create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a HTML only accordion, &lt;/li&gt;
&lt;li&gt;a pretty one with just HTML and CSS &lt;/li&gt;
&lt;li&gt;a nice animated one using HTML, CSS and JS. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And to make it more fun, we will do it in the "1 minute, 5 minute, 10 minute" format!&lt;/p&gt;

&lt;h2&gt;
  
  
  The 1 minute accordion (HTML only)
&lt;/h2&gt;

&lt;p&gt;Wow 1 minute isn't long to build an accordion now is it?&lt;/p&gt;

&lt;p&gt;I mean, I have already wasted 15 seconds saying that. &lt;/p&gt;

&lt;p&gt;You know what though, I am not worried, accordions are super easy in HTML. &lt;/p&gt;

&lt;p&gt;Yes, I am still talking, I really do have time.&lt;/p&gt;

&lt;p&gt;OK, enough stalling, here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Accordion item 1&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
  Accordion 1 content
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Accordion item 2&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
  Accordion 2 content
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Accordion item 3&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
  Accordion 3 content
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Phew, 12 seconds to spare!&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait, what? That is an accordion?
&lt;/h3&gt;

&lt;p&gt;You bet your sweet behind it is! &lt;/p&gt;

&lt;p&gt;A fully functioning and pretty accessible accordion!&lt;/p&gt;

&lt;p&gt;Wanna see it in action?&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/GrahamTheDev/embed/zYyPdKQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;It is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;keyboard accessible:&lt;/strong&gt; you can tab through each accordion item and open and close it with the Enter key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;keyboard friendly:&lt;/strong&gt; it has built-in focus indicators, for free!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;functional:&lt;/strong&gt; No JS and yet it can open and close (and exposes the state to assistive technology, making it more accessible!), even more free goodies! 😱&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is pretty amazing, except for one big problem...it is ugly AF. Ewwwww. Let's fix that!&lt;/p&gt;

&lt;h2&gt;
  
  
  The 5 minute accordion (HTML and CSS)
&lt;/h2&gt;

&lt;p&gt;Ok so we have our base HTML, what can we do with it?&lt;/p&gt;

&lt;p&gt;Well luckily the &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;detail&amp;gt;&lt;/code&gt; elements are quite easy to style!&lt;/p&gt;

&lt;p&gt;The only thing that has changed in the HTML here is that we have added a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; around all of the content that is hidden, to make it easier to position it.&lt;/p&gt;

&lt;p&gt;Oh and I also added a single attribute "&lt;code&gt;open&lt;/code&gt;" to the second item, to show another trick that the &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; element has! A single attribute to set the open state!&lt;/p&gt;

&lt;p&gt;After a few minutes of styling, we have a (in my opinion) attractive accordion with just HTML and CSS.&lt;/p&gt;

&lt;p&gt;The only thing that is unusual here is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;details&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;summary&lt;/span&gt;&lt;span class="nd"&gt;::marker&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;details&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;summary&lt;/span&gt;&lt;span class="nd"&gt;::-webkit-details-marker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which allows us to hide the default open and close marker so we can create a custom one using an SVG background image (in this instance).&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/GrahamTheDev/embed/MWZOvoL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Looking good...yes you, also the accordion isn't looking bad either! &lt;/p&gt;

&lt;p&gt;Shall we make it all fancy now?&lt;/p&gt;

&lt;h2&gt;
  
  
  The 10 minute accordion (HTML, CSS and JS)
&lt;/h2&gt;

&lt;p&gt;The pure HTML and CSS accordion is great, but it would be nice if we had a subtle animation when we open and close each &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; element that makes up the accordion.&lt;/p&gt;

&lt;p&gt;Luckily with a little JS magic we can achieve this. &lt;/p&gt;

&lt;p&gt;Here is the JS I came up with (and partially stole! Hey, I only had 5 minutes to add JS functionality...give me a break!).&lt;/p&gt;

&lt;p&gt;The code below includes some explanations at key points:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Accordion&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;summary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isClosing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isExpanding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;overflow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isClosing&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isExpanding&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shrink&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;shrink&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isClosing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;//we need to grab the height of the summary and the overall element before we shrink it.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;px`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;endHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;px`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;//stop previous animations.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//start the new animation, starting at full height and reducing down to the minimum&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;startHeight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endHeight&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ease-out&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;


    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onfinish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onAnimationFinish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oncancel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isClosing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//important step, we grab the current height and set it, this could be mid animation so we need to set it each time.&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;px`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expand&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;expand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isExpanding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;px`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;endHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;px`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;startHeight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endHeight&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ease-out&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onfinish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onAnimationFinish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oncancel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isExpanding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;onAnimationFinish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;animation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isClosing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isExpanding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;overflow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;details&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Accordion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And putting it all together we get this beautiful accordion:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/GrahamTheDev/embed/QWzmgeN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And there you have it, a 1 minute, 5 minute and 10 minute accordion. But we are not done yet!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary on &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Yes...I had to call this section that, I am sorry (not sorry!) 😂.&lt;/p&gt;

&lt;p&gt;As you can see, in just a few short minutes we can create a beautiful accordion. &lt;/p&gt;

&lt;p&gt;I will admit, the CSS only one actually took about 7 minutes and the JS took about 15 minutes...but I am slow at coding OK?&lt;/p&gt;

&lt;p&gt;Either way though, we created an accessible and easy to maintain accordion.&lt;/p&gt;

&lt;p&gt;And no, we are still not done yet...let's level it up a bit further! 💪🏼&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus Copy and Paste Component version
&lt;/h2&gt;

&lt;p&gt;As an added bonus for making it this far, here is our accordion, restructured as a component you can reuse on your site. (this is the super secret "1 hour" version! 😂)&lt;/p&gt;

&lt;p&gt;I even added an optional parameter that allows you to make it so that when you open one section the other sections close!&lt;/p&gt;

&lt;p&gt;Additionally I added one more important feature: we test for &lt;code&gt;prefers-reduced-motion&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;prefers-reduced-motion&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the previous example we had an animation on open and close.&lt;/p&gt;

&lt;p&gt;Now, the problem with animations is that for certain people they can be unsettling and or cause them to feel nauseous and or dizzy.&lt;/p&gt;

&lt;p&gt;So the browser provides us with a media query we can use to check if that person has a preference for reduced motion.&lt;/p&gt;

&lt;p&gt;By querying that media query in our JS we can activate or remove the &lt;code&gt;eventListeners&lt;/code&gt; that are responsible for the animation and respect their preferences / needs.&lt;/p&gt;

&lt;p&gt;You can read more about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion" rel="noopener noreferrer"&gt;&lt;code&gt;prefers-reduced-motion&lt;/code&gt; on MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So with that in mind, let's check out how to use the component and the options!&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use the component!
&lt;/h3&gt;

&lt;p&gt;All you need to do is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grab the HTML and replace the &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; contents and the contents inside the &lt;code&gt;&amp;lt;div class="contents"&amp;gt;&lt;/code&gt; section with your own! (Make sure to grab the outer &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; with class &lt;code&gt;tota11y-accordion&lt;/code&gt; or it won't work!)
&lt;/li&gt;
&lt;li&gt;Include the CSS and JS!&lt;/li&gt;
&lt;li&gt;Add any options that you want to set (see section after CodePen).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/GrahamTheDev/embed/yLGKzPZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Configurable Options
&lt;/h3&gt;

&lt;p&gt;There are a couple of options you can change in the JS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// create a new Tota11yAccordion, passing the wrapper element and then the options.&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Tota11yAccordion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt;
      &lt;span class="c1"&gt;// close other items in the accordion when one is opened.&lt;/span&gt;
      &lt;span class="na"&gt;closeOthers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// default false&lt;/span&gt;

      &lt;span class="c1"&gt;// set the duration of the animation&lt;/span&gt;
      &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// default 400&lt;/span&gt;

      &lt;span class="c1"&gt;// set the animation easing&lt;/span&gt;
      &lt;span class="na"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ease-in&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// default ease-out&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and there are a few styling options that I have added as variables on the CSS &lt;code&gt;:root&lt;/code&gt; element, so you can do some styling there without having to dig through everything.&lt;/p&gt;

&lt;p&gt;As a final point of interest, I have prefixed all of the CSS with &lt;code&gt;.tota11y-accordion&lt;/code&gt; so that the styles will not bleed into the rest of your page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts!
&lt;/h2&gt;

&lt;p&gt;As you can see, the &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; elements are really powerful and also straight forward to use and style.&lt;/p&gt;

&lt;p&gt;And, most importantly, they are &lt;strong&gt;accessible&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Easier to create than other accordions, beautiful and inclusive components? That is the beauty of accessibility first thinking! 🚀&lt;/p&gt;

&lt;h3&gt;
  
  
  Speaking of Accessibility
&lt;/h3&gt;

&lt;p&gt;Have you heard of my business &lt;a href="https://tota11y.dev" rel="noopener noreferrer"&gt;totally.dev&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;If not, do you own a SaaS or manage a team of developers and want to learn how to make your applications accessible? &lt;/p&gt;

&lt;p&gt;Do you want to learn how to unlock a new audience of over 1 billion people with disabilities who want to use your product?&lt;/p&gt;

&lt;p&gt;If that sounds interesting then check out &lt;a href="https://tota11y.dev" rel="noopener noreferrer"&gt;totally.dev&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;We offer Accessibility as a Service (yes, it's abbreviation says "AaaS"...yes we lean into it in the marketing! 😂) so that you can take your product and team from accessibility zero to accessibility hero!&lt;/p&gt;

&lt;p&gt;Please do check out my AaaS...and let me know if you like it! (see...we lean into it hard lol).&lt;/p&gt;

&lt;h3&gt;
  
  
  Are you a developer wanting to learn about accessibility?
&lt;/h3&gt;

&lt;p&gt;I am running live streams on accessibility, coupled with some tutoring for those who are interested. If you want to take part, send me a DM on Twitter, my account is &lt;a href="https://twitter.com/GrahamTheDev" rel="noopener noreferrer"&gt;GrahamTheDev&lt;/a&gt;, I would love to have you as part of the #A11y100 💪🏼💗!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>webdev</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Sharing Code Snippets on Social Media, the accessible way! [Quicka11y]</title>
      <dc:creator>GrahamTheDev</dc:creator>
      <pubDate>Tue, 19 Sep 2023 11:17:08 +0000</pubDate>
      <link>https://dev.to/tota11ydev/sharing-code-snippets-on-social-media-the-accessible-way-quicka11y-cf</link>
      <guid>https://dev.to/tota11ydev/sharing-code-snippets-on-social-media-the-accessible-way-quicka11y-cf</guid>
      <description>&lt;p&gt;Welcome to the first "quick accessibility" post to quickly (quicka11y 😁) improve the accessibility of your content, websites etc.&lt;/p&gt;

&lt;p&gt;Today's tip is all around improving the accessibility of some of your social media posts, in less than 30 seconds! Let's jump right in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Edit / note!
&lt;/h2&gt;

&lt;p&gt;There was a little confusion in the comments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just for clarity, what I am suggesting here is targeted for social media.&lt;/strong&gt; This is due to the lack of flexibility you have in terms of markup and tools to make things accessible.&lt;/p&gt;

&lt;p&gt;Please, if you are displaying things on a site where you control the markup and or CSS, use properly marked up code blocks and use CSS to make them as pretty as you want (as long as you don't ruin the accessibility of course!).&lt;/p&gt;

&lt;p&gt;For example, on DEV, just use triple backticks and state the language used to get properly marked up code snippets...not images.&lt;/p&gt;

&lt;p&gt;For example, this markdown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;``` js
const array1 = [1, 4, 9, 16]; 
// Pass a function to map 

const map1 = array1.map((x) =&amp;gt; x * 2); 
console.log(map1); 

// Expected output: Array [2, 8, 18, 32]
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; 
&lt;span class="c1"&gt;// Pass a function to map &lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;map1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;map1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// Expected output: Array [2, 8, 18, 32]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and that is far more accessible and looks good enough!&lt;/p&gt;

&lt;p&gt;Hopefully that is clear, now on with the article!&lt;/p&gt;

&lt;h2&gt;
  
  
  What are we trying to improve?
&lt;/h2&gt;

&lt;p&gt;Loads of developers and companies share amazing, beautiful code snippets on social media every day!&lt;/p&gt;

&lt;p&gt;The problem is, the way that they share them is not accessible...and it provides a poor experience for everyone.&lt;/p&gt;

&lt;p&gt;Let me explain...&lt;/p&gt;

&lt;h2&gt;
  
  
  How most people / companies share snippets
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzw8g9f8lzfwuboh64krm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzw8g9f8lzfwuboh64krm.png" width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[Note: alt description deliberately omitted as an example]&lt;/p&gt;

&lt;p&gt;People share code snippets like the one in this above, using tools like "carbon" to create pretty code snippets.&lt;/p&gt;

&lt;p&gt;But an image of code is not as useful as actual code (and is not accessible).&lt;/p&gt;

&lt;p&gt;You see, anyone using assistive technology (for example a screen reader), cannot access the code in your image as they need the code as actual text for a screen reader to work!&lt;/p&gt;

&lt;p&gt;One way that people attempt to fix this is to add all of the code to the alt description on the image. 👎🏼&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding code to the alt description
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm045pvg1h2q5wd91iva5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm045pvg1h2q5wd91iva5.png" alt="const array1 = [1, 4, 9, 16]; // Pass a function to map const map1 = array1.map((x) =&amp;gt; x * 2); console.log(map1); // Expected output: Array [2, 8, 18, 32]" width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[note: the alt description in the above image is not accessible]&lt;/p&gt;

&lt;p&gt;It is well intentioned, but still not very accessible.&lt;/p&gt;

&lt;p&gt;You see alt text is a single line text attribute. &lt;/p&gt;

&lt;p&gt;So when you add all of that code to an alt description it gets "smushed" onto one line and is read as plain text, which is nearly impossible to understand. &lt;/p&gt;

&lt;p&gt;It ends up being like this (this is the alt of the above image):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 4, 9, 16]; // Pass a function to map const map1 = array1.map((x) =&amp;gt; x * 2); console.log(map1); // Expected output: Array [2, 8, 18, 32]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can imagine, it is very difficult to understand that code and know where comments end and being etc!&lt;/p&gt;

&lt;p&gt;So screen reader users cannot really use your snippet, even with the code in the alt.&lt;/p&gt;

&lt;p&gt;On top of that, everybody who wants to try your snippet will have to retype it, character by character into their IDE.&lt;/p&gt;

&lt;p&gt;Time consuming, prone to errors and a terrible User Experience (UX).&lt;/p&gt;

&lt;h2&gt;
  
  
  A better way
&lt;/h2&gt;

&lt;p&gt;So can we do better? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You bet we can!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is a straight-forward way to make your code snippets top-tier, easy to copy and super accessible!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; create your code image (same as before).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; add a description of what the code does (not the actual code) in the alt description.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Link to a GitHub Gist,CodePen or web page containing the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is it! &lt;/p&gt;

&lt;p&gt;Let's look at our earlier example!&lt;/p&gt;

&lt;p&gt;The template we will use is:&lt;/p&gt;

&lt;p&gt;[Explanation of code]&lt;/p&gt;

&lt;p&gt;[link to Gist]&lt;/p&gt;

&lt;p&gt;[image of code with alt description explaining, briefly, what the code is for]&lt;/p&gt;

&lt;p&gt;Let's see what that would look like.&lt;/p&gt;

&lt;h3&gt;
  
  
  Better way example
&lt;/h3&gt;

&lt;p&gt;The map() method of an Array instance creates a new array, populated with the results of calling a provided function on every element in the calling array. [MDN]&lt;/p&gt;

&lt;p&gt;Here is a demo: &lt;a href="https://gist.github.com/GrahamTheDevRel/9ba078625dd07acaee2765c86f43f911" rel="noopener noreferrer"&gt;https://gist.github.com/GrahamTheDevRel/9ba078625dd07acaee2765c86f43f911&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzst9vi49clgiua8svmvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzst9vi49clgiua8svmvf.png" alt="Code example for using the map function in JavaScript." width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[Note: The alt text in the above image reads "Code example for using the map function in JavaScript."]&lt;/p&gt;

&lt;h4&gt;
  
  
  Quick notes:
&lt;/h4&gt;

&lt;p&gt;First of all, above I have included the full URL as that is how social media tends to handle URLs. If sharing on a platform that allows you to change the text of a hyperlink, then please make your text more descriptive like "GitHub Gist demo for JS Map".&lt;/p&gt;

&lt;p&gt;Secondly, if you do use GitHub Gists, as I have here, don't forget to change from "secret gist" to "public gist" before sharing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficpog88r1uy5lh40tqqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficpog88r1uy5lh40tqqi.png" alt="Dropdown on GitHub Gists showing " width="493" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, if your code snippet URL is too long (for example if a long URL takes you over your character limit on X / Twitter) you can handle that slightly differently.&lt;/p&gt;

&lt;p&gt;In that case, it's acceptable to say "Code example in next post" and then post the URL as the first reply under the original:&lt;/p&gt;

&lt;p&gt;Post 1: explanation + "Code example in next post" + image (with alt!)&lt;/p&gt;

&lt;p&gt;Post 2: "Code snippet for JS map: [link]"&lt;/p&gt;

&lt;p&gt;That way, you can protect your character count while still providing an easy to find and use code example.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick win, with huge impact!
&lt;/h2&gt;

&lt;p&gt;So with all that in mind, how much extra time do you think that took me? &lt;/p&gt;

&lt;p&gt;It took about 30 seconds!&lt;/p&gt;

&lt;p&gt;I had to copy paste the code into a Gist, give it a title and a file name, grab the URL and paste it into my post.&lt;/p&gt;

&lt;p&gt;Very little work, but look at what we have achieved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Millions of people using Assistive Tech (Screen Readers) can now access the code snippet.&lt;/li&gt;
&lt;li&gt;✅ People who want to try your code snippet can follow the link and copy and paste it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A wider audience, inclusive practices and better UX? &lt;/p&gt;

&lt;p&gt;That is a great return on time invested!&lt;/p&gt;

&lt;h2&gt;
  
  
  What now?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create more accessible snippets going forward!&lt;/li&gt;
&lt;li&gt;If you see someone (particularly Companies!) sharing inaccessible code snippets on social media...share this post with them!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It only takes a few of us to make a huge impact on inclusion...I hope you join us! 💪🏼💗&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>socialmedia</category>
      <category>a11y</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
