How ChatGPT can help your code be accessible
Tags: a11y, chatgpt, development, ai
Published: 2023-01-27 17:59:13

Sometimes [accessibility](https://www.w3.org/WAI/standards-guidelines/wcag/) (also known as a11y) can be a bit of a puzzle.

Whether you're a new developer still learning the ropes or a seasoned dev properly labeling your code, there's a chance you still rely on Google to help you out. For instance, you may know you need to add aria-* attributes somewhere, but you don't want to overdo it because the screen-reader may get confused. You want to make sure you touch on all types of accessibility like sighted individuals, hearing impaired as well as anyone using screen-readers. Well, look no further. In this post, we'll talk about a popular tool-ChatGPT- and how it can help you piece together the accessibility puzzle.

## Whats ChatGPT? 
[ChatGPT](https://openai.com/blog/chatgpt/) is a computer program that can understand and respond to human language. You can think of it like a robot that can talk to you and help you find information.

## How?
We’ve all heard of the amazing things ChatGPT can do for us, so why not use it to our advantage as developers!? One of the best skills a developer can have is learning how to ask the right questions. This goes for asking Google, stack-overflow, or your own teammates. You’ll need this skill for ChatGPT to work for you AND with you.

Let me walk you through an example of using ChatGPT, and how it can help us work towards accessibility.

Say we want to make a simple button accessible. You can always just ask ChatGPT how to do that.

> _How can I make a simple HTML button accessible?_

Asking this question will give you some pretty good information about the basic accessibility of a button, but it may not give you exactly what you need. When asking ChatGPT to do something, you need to be really specific. This can mean breaking your question down into multiple questions that are based off each other or the previous answer.

## Some good things to think about are:

- What coding language are you using?
- Are you using any frameworks? 
- What type of a11y? (i.e. visually impaired, hearing impaired, screen-readers etc.)
- What level of a11y? WCAG 2.1 A (lowest), AA (mid-range), and AAA (highest)
- Do you want any specific verbiage or colors to be used?

The more specific you are, the better the output will be.

## Lets practice!
![ChatGPT prompt without code "write me a script using the following React code and create a green "Read More" button using React and Tailwind CSS that is completely 508 compliant and accessible visually and to screen-readers. import React from 'react'"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7vd9bsrmj8btvtoa378.png)

In the example above, I asked ChatGPT to write the code for a green “Read More” button and provided it the language, color, framework, and the type of accessibility I’m looking for. If you want your components or code written in a specific way, you can even give it some starter code to work with like below.

![ChatGPT prompt using react starter code "write me a script using the following React code and create a green "Read More" button using React and Tailwind CSS that is completely 508 compliant and accessible visually and to screen-readers. import React from 'react'"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd3vkuutudc926ew67vh.png)

I suggest going this route so you're still writing your own code as things might get more complex.

## Here's what ChatGPT came up with:

```react
import React from 'react';

const App = () => {
  return (
    <div>
      <button 
        type="button"
        className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-full focus:outline-none focus:border-green-800 focus:shadow-outline"
        aria-label="Read More"
        tabIndex={0}
      >
        Read More
      </button>
    </div>
  );
};

export default App;

```

As you can see below, it even goes and explains what each piece of code it added means, and how it relates to accessibility. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwuabhruooep4cxzfgt5.png)

## Don't forget...
While this is a great start and a great place to learn, as always it's important to do your own accessibility testing with many different tools (see links below) to ensure that it's fully accessible.

Take what ChatGPT says as advice and make sure to double-check how things get implemented. 

**Always always always ** proofread! Check that the React code is written properly and the Tailwind CSS properties make sense. Remember, ChatGPT is a tool to learn from and not a tool to use as a crutch! 

I hope this helps you both learn how to use a new technology, as well as solve that accessibility puzzle a little easier!

If you want to learn more about accessibility(a11y), or Tailwind CSS and A11y, check out my blog post about that [here](https://dev.to/devsatasurion/is-tailwind-css-accessible-52dc)

**Helpful links** 
Free A11y tools:

- [NVDA Screen Reader
](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwj7uqDNm_D6AhUiEFkFHfvrB9QQFnoECBYQAQ&url=https%3A%2F%2Fwww.nvaccess.org%2Fdownload%2F&usg=AOvVaw3AAc5nrFrg0lylpULGjjxr)
- [ANDI Accessibility Testing Tool
](https://www.ssa.gov/accessibility/andi/help/install.html)
- [WAVE Browser Extension
](https://wave.webaim.org/extension/)

A11y Info:

- [WCAG Standard Guidelines
](https://www.w3.org/WAI/standards-guidelines/wcag/)
- [A11y Project
](https://www.a11yproject.com/)