Pay attention here. Widgets are important.
Widgets (by definition) are things a user can interact with. For example, a <button> is a widget. An <input> is also a widget.
Widgets are grouped into two categories according to the specs:
- Composite roles
- Non-composite roles
Composite roles are widget containers that contain two or more smaller widgets. Non-composite roles are the smaller widgets. There are 9 composite and 20 non-composite widget roles.
It makes more sense to group widget roles according to what they do, so I organised these 29 roles into 7 groups:
- Standalone Widgets
- Input widgets
- Trees and Grids
- Listbox
- Combobox
- Tablist
- Menus
Standalone widgets
Standalone widgets are small pieces of interactive elements. Theyโre quite obvious. Youโll know most of them from their names.
| Role | HTML Element |
|---|---|
| button | <button> |
| link | <a href="..."> |
| separator | - |
| progressbar | - |
| scrollbar | - |
Button triggers an action when a user clicks or presses it. The <button> element has an implicit button role.
<button>This is a button</button>Focusable separator is a separator that divides a component into two parts. Users can slide the separator to show more or less of each part. Itโs usually used for a before/after image slider.
<div role="separator" tabindex="0">...</div>
Link allows users to navigate to an external (or internal) resources. Itโs built into an <a> with a href attribute.
<a href="#"> Link </a>Progressbar is an element that tells the progress status for a task.
<div role="progressbar">...</div>Scrollbar is, well, a scrollbar! Youโll only use this if you decide to code a scrollbar yourself.
<div role="scrollbar">...</div>What to know here
- Know
buttons. - Know
links - Know the other roles exist, but donโt need to remember them.
Input widgets
Most of these widgets are built into the <input> element.
| Role | Element |
|---|---|
| checkbox | <input type="checkbox"> |
| switch | - |
| radio | <input type="radio"> |
| textbox | <input type="text"> |
| searchbox | <input type="search"> |
| slider | <input type="range"> |
| spinbutton | - |
Checkbox is an input that can be checked by a user. Its value can be true, false, or mixed. The checkbox role is built into <input type="checkbox">
<input type="checkbox" />Switch is a type of checkbox that accepts only on or off values. (Itโs like a cross-breed between a checkbox and a button). I donโt see the need to use switch when checkbox and button roles exist.
<button role="switch">...</button>Radio is a checkable input that comes in a group. Only one radio can be checked at any one time. The radio role is built into <input type="radio">.
Radio elements should be contained inside a radiogroup (You only need radiogroup if you use the radio role).
<input type="radio" />Textbox is an input that allows users to enter text. The textbox role is built into <input type="text"> and <textarea>.
<input type="text" />Searchbox is a textbox thatโs meant to help users search for something. The searchbox role is built into <input type="search">.
<input type="search" />Slider is an input that allows users to select a value from a given range. The slider role is built into <input type="range">
<input type="range" />Spinbutton is a type of range input that expects the user to select a value from several discrete choices. It usually comes with an Up/Down/Left/Right buttons.
<div role="spinbutton">...</div>What to know here
Be familiar with <input> with these types:
textradiocheckboxsearch
Know the rest, but donโt need to remember them.
Trees and grids
There are three composite roles in the grids and trees group. They are:
- tree
- grid
- treegrid
Tree
Tree is a specific type of list that contains collapsible lists.
Children elements of a tree can have two roles:
grouptreeitem
If the element is an <ul> or <ol>, that element should have a group role. If the element is a <li>, that element should have a treeitem role.
<ul role="tree"> <li role="treeitem"> <span> Name of group </span> <ul role="group"> <li role="treeitem">...</li> <li role="treeitem">...</li> <li role="treeitem">...</li> </ul> </li></ul>Grid
Grid contains content that can be grouped into columns and rows. Grids are tables (but they donโt have to look like tables). Grids contain interactive content while tables contain static content.
Grids can use the following roles:
rowrowgrouprowheadercolumnheadergridcell
<table role="grid"> <!-- table --> <thead> <!-- rowgroup --> <tr> <!-- row --> <th>Column 1</th> <!-- columnheader --> <th>Column 2</th> </tr> </thead> <tbody> <!-- rowgroup --> <tr> <!-- row --> <th>Row 1</th> <!-- rowheader --> <td>Item</td> <!-- cell --> </tr> </tbody></table>Itโs perfectly fine if you donโt want to use the <table> element. However, you still need to make sure:
gridcell,columnheader, androwheaderare wrapped inrowrowis wrapped inrowgrouporgrid
Treegrid
Treegrid combines grid and tree. I doubt youโd ever build a component that requires a treegrid role. It looks like this:
What to know here
Know these roles exist. But donโt need to remember them. You can do more research when you need to.
Listbox
A listbox let users to select one or more choices from a list. Each listbox item should have the option role. options can contain images.
The best example of a listbox is the list of predictions in Typeahead.
Know the listbox role exist. We wonโt dive into Typeahead so we wonโt use it (Iโll explain why at the end of this module).
Combobox
A combobox has two elements:
- A single-line text field (either
<input type="text">or<input type="search">) - An element that can be shown or hidden. This element must be one of the following roles:
listboxdialoggridtree
An example of a combobox is Typeahead.
Again, know the combobox role exist. We wonโt use it. (Iโll also explain why at the end of this module).
Tablist
Tablist is a tabbed component. They mean the same thing.
Tablist contains two kinds of children:
tab: The tabtabpanel: The content of the tab.
Weโll use tab and tabpanel to improve Tabby.
Menus
There are two composite roles in the menus category:
menumenubar
A menu offers a list of choices to the user. Itโs similar to listbox, but it should be presented in a way thatโs similar to a <select> element.
A menubar is a menu that always remains visible. It should work (and feel) the same as a menubar in most desktop applications.
Menus (and menubar) can have three types of children elements:
menuitem: A generic item inside a menumenuitemcheckbox: A checkbox inside a menumenuitemradio: A radio inside a menu
What to know here
Know the menu and menubar roles exist. But also understand you should NEVER use them. (Because they put screen readers into application mode).
Quick summary
There are lots of widget roles. It can be confusing! I wanted to show you everything so you have a sense of role fits what kind of components.
Just know these roles exist. You donโt need to remember any of them.
Weโll go through the important ones as we improve our components together.
Thatโs it!
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson