<?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: Lochy</title>
    <description>The latest articles on DEV Community by Lochy (@lochyb).</description>
    <link>https://dev.to/lochyb</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%2Fuser%2Fprofile_image%2F990191%2F0c1385da-719d-481f-8420-b360aaaa8e12.png</url>
      <title>DEV Community: Lochy</title>
      <link>https://dev.to/lochyb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lochyb"/>
    <language>en</language>
    <item>
      <title>Why use TailwindCSS &amp; Aria</title>
      <dc:creator>Lochy</dc:creator>
      <pubDate>Thu, 15 Dec 2022 00:00:32 +0000</pubDate>
      <link>https://dev.to/lochyb/why-use-tailwindcss-aria-b3f</link>
      <guid>https://dev.to/lochyb/why-use-tailwindcss-aria-b3f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Assumed knowledge.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML/CSS basics &lt;/li&gt;
&lt;li&gt;TailwindCSS basics &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why use TailwindCSS &amp;amp; Aria
&lt;/h3&gt;

&lt;p&gt;Maintaining clean and easily understandable code that still gets the job done can often be a difficult task. With minimal rules and a wide variety of ways of implement HTML and CSS it can often be difficult to strike a balance between function and form. My aim is to provide you with another tool to find balance between these two important aspects of front end development. &lt;/p&gt;

&lt;h4&gt;
  
  
  Firstly lets quickly explain what we mean by Aria.
&lt;/h4&gt;

&lt;p&gt;Aria is an acronym for &lt;strong&gt;Accessible Rich Internet Applications&lt;/strong&gt;. Its main function is to provide extra information about elements on the webpage. This extra information can be used by accessibility tools to improve user experiences. &lt;br&gt;
Reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA"&gt;MDN Web Docs - Aria&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers can also access these Aria labels and use them to control aspects of the website.&lt;/p&gt;

&lt;p&gt;This video is a 1000% funnier and explains the reason for Aria in a much better way. &lt;a href="https://briefs.video/videos/what-is-aria-even-for/"&gt;What Is ARIA Even For?&lt;/a&gt;  &lt;/p&gt;
&lt;h2&gt;
  
  
  Code Examples
&lt;/h2&gt;

&lt;p&gt;We will implement a simple example of a button that can be toggled on and off. We will create this in a few different ways to explain more about aria labels. &lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1 (Vanilla CSS)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- page.html --&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- Click to toggle the button --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"toggle_button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Click Me!
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* styles.css */&lt;/span&gt;

&lt;span class="nc"&gt;.toggle_button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;changed&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;background&lt;/span&gt; &lt;span class="nt"&gt;color&lt;/span&gt; &lt;span class="nt"&gt;when&lt;/span&gt; &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;pressed&lt;/span&gt;
&lt;span class="nc"&gt;.toggle_button.toggle_button--pressed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;darkgrey&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;We can use simple JS to toggle the &lt;code&gt;.toggle_button--pressed&lt;/code&gt; class. I wont explain how to use JS to toggle this. Please check this article for more information. &lt;a href="https://www.w3schools.com/js/js_htmldom_eventlistener.asp"&gt;w3Schools event listeners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Screen readers can not tell from the HTML what state this button is in. Is it clicked or not clicked. The only way to tell is to either visually view the button or check the css classes. Not good for anyone using a screen reader.  &lt;/p&gt;
&lt;h3&gt;
  
  
  Example 2 (Vanilla CSS with Aria)
&lt;/h3&gt;

&lt;p&gt;First we will add the Aria property to this button for better HTML semantics. We will again use Javascript to control this aria-pressed state. We can also now use this aria information in our css styles.&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="c"&gt;&amp;lt;!-- page.html --&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- Click to toggle the button --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"toggle_button"&lt;/span&gt; &lt;span class="na"&gt;aria-pressed=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Click Me!
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* styles.css */&lt;/span&gt;

&lt;span class="nc"&gt;.toggle_button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.toggle_button&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;aria-pressed&lt;/span&gt;&lt;span class="o"&gt;]=&lt;/span&gt;&lt;span class="s1"&gt;"true"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;darkgray&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;We managed to improve accessibility and also reduced the need to think of extra css class names to handle stateful stylistic changes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3 (CSS With TailwindCSS &amp;amp; React)
&lt;/h3&gt;

&lt;p&gt;In this example we will use TailwindCSS and react to replicate the same results as previous example. &lt;/p&gt;

&lt;p&gt;The TailwindCSS team has implemented Aria classed directly into the framework allowing use to direct access the more common aria labels. &lt;a href="https://tailwindcss.com/docs/hover-focus-and-other-states#aria-states"&gt;TailwindCSS Aria&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isPressed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsPressed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
    &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"bg-gray-500 aria-pressed:bg-gray-800"&lt;/span&gt;
    &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setIsPressed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isPressed&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; 
    &lt;span class="na"&gt;aria-pressed&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isPressed&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    Click Me!
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Using TailwindCSS in my option leads to cleaner code. With no need to create custom classes or the pain of thinking in BEM. There is also no need to go searching for where the css files are locate the styles inside each css file. &lt;/p&gt;

&lt;p&gt;TailwindCSS also implements other HTML states like &lt;code&gt;focus&lt;/code&gt; and &lt;code&gt;hover&lt;/code&gt;.  All with the benefit of the styles being directly attached to their HTML elements, making it quicker and easier to understand. &lt;/p&gt;

&lt;p&gt;Implementing styling based on aria values allows you to control even more styles all while adding usability and creating cleaner code. &lt;/p&gt;

&lt;p&gt;Keeping all of your styling in one place instead of scattering it throughout the project make its easier for everyone to understand. Using the available tools in the TailwindCSS toolbox before needing to resort to other methods will help the flow of your project to stay more clear and concise. &lt;/p&gt;

&lt;p&gt;Now before you go out and add aria-labels to everything I would like yo share with you this quote.&lt;/p&gt;

&lt;p&gt;_There is a saying "No ARIA is better than bad ARIA." _&lt;/p&gt;

&lt;p&gt;Adding aria-labels purely to style css is never a good idea. You should always think 'what will the user gain from this aria label' before deciding to add any aria labels. If you decide to add aria labels, then why not use them to help you style your next project. &lt;/p&gt;

</description>
      <category>css</category>
      <category>tailwindcss</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
