Building a simple dropdown menu with keyboard support using Ariakit.
ALT Programming and testing with mouse and keyboard a dropdown menu widget. The state is provided through the useMenuState hook imported from the Ariakit package, and the components are MenuButton, MenuButtonArrow, Menu, MenuItem, and MenuSeparator. The code uses React and JSX.
Doing some experiments with a custom select grid component. Some key features:
- Bidimensional arrow key navigation.
- Typeahead support even when the popup is closed.
- Changes the value on mouse hover.
- Resets the value on Escape.
ALT Using a custom 3x3 select grid component with the mouse and keyboard to control the position of a black text inside a yellow background.
The Ariakit Select renders a hidden <select> element that's kept in sync with the custom element.
This is so we can support things like native form submission and browser autofill.
ALT Showing a custom select element and a native select element. When the value of one changes, the other is automatically updated.
"I know we've spent several days on this custom single-select component, but now the requirements have changed, and we need a multi-select."
Okay 👌
ALT Using a single-select element. Then, changing its default value from a string to an array of strings immediately turns the element into a multi-select.
The Ariakit Select component can optionally set the value when an item receives focus, not only on click, similar to how native selects work on Windows.
Mouse hover is ignored. And pressing Escape will reset the value.
🧵
ALT Using a custom select component with mouse and keyboard
You're given a design with complex components with very specific requirements.
You look for a library to make your work easier.
After a few days of puzzling over the options, you realize that you won't be able to implement the component exactly how it was designed.
[1/6]
Replicating Twitter hovercard with keyboard support using Ariakit. ✨
The disclosure button appears when the hovercard anchor receives keyboard focus.
ALT Programming a hovercard component similar to the Twitter one with more information about a user. Testing the component with the mouse by hovering over the username anchor, and then with keyboard, by clicking on a disclosure button that appears when the username anchor receives keyboard focus.
Had a lot of fun building this example with a few Ariakit components: a Select with custom items, including one that opens a Dialog with a Form. ✨
+ Keyboard and screen reader support
+ Automatic focus management
+ Your creativity
= Infinite possibilities 😄
ALT Using a custom select element labeled as "Account". The value has the selected user's photo, name, and email address. When the Select is open, there are two account options and a third option to add another account. When the third option is activated, a dialog opens with an input labeled as "Email". Once the form is submitted, the dialog closes and the focus moves back to the Select element with the newly added user as the value. Opening the Select popup again will also show the newly added user.
Testing a multi-selectable Combobox component with a screen reader.
It announces "selected" and "not selected" (because of the "aria-selected" attribute).
Focusing on the container element announces the selected items (because of the "aria-multiselectable" attribute).
Testing the new Ariakit Select component with the other components in the library.
It works great with Combobox, which means we can easily create this custom select with a search field. 😍
What else would you like to see?
ALT Using a custom select component with a search field with mouse and keyboard.
Working on the new Select component.
- Proper semantics and screen reader support.
- Keyboard navigation.
- Typeahead with the popup open or closed.
- And more (will share more details soon).
ALT Using a custom select component using the mouse and the keyboard (arrow keys and characters).
Another update on the Form component: dynamic array fields.
- Built-in state & focus management.
- FormRemove & FormPush components.
- Supports native browser validation on each field.
- Supports nested objects.
And more... 🧵
ALT Creating array fields with the Ariakit Form component.
The Form component is finally happening:
- TypeScript support for field names.
- Accessible by default.
- Focus management.
- Native browser validation.
And more... 🧵
ALT Coding a Form component by importing components from the ariakit package. The form has one field (name) with a label. Then, a field error element is added. Finally, a submit handler is added.