React Aria Components
react-spectrum.adobe.com
react-spectrum.adobe.com
Couple questions/feedbacks if you’re still in the comments, Devon!
1. Radix implements an asChild prop to help merge the library components down into other components that might already have their own styling or behaviors. Did y’all consider supporting this sort of polymorphic approach? Or is there another way to merge components? The main spots where I’ve used it are buttons and dropdown triggers, so it might not be broadly applicable.
2. Is it easier to extend from the React Aria types with this interface now? My previous implementations with React Aria - I’ve always been a little confused about which inputs to the hooks are useful to include in the component’s props interface and how to correctly annotate them, whether that’s recreating the types or extending from the numerous options within the react-aria package. Radix at least has some suggestions for conventions, and I feel a bit more confident following them.
3. More on the feedback side - React Aria has typically implemented similar-but-different props for the common HTML form control attributes like “disabled” and “onChange” as “isDisabled” and “onValueChange” - I really appreciate these at one level for the internal consistency throughout the library, but I’ve found these choices make it a bit more annoying to integrate React Aria with other third-party libraries meant to “just work” with the regular attributes. E.g. spreading Formik form props for a field into a form control, but having to re-map the onChange prop to onValueChange. Definitely trade offs in either direction, but I’m curious how y’all made the choice and if there are some remediations y’all have found along the way?
Thanks so much for your (and the team’s) work on this! And on Parcel and the million other projects you’re building.
1. We decided to start simple for the first release but it’s definitely possible to add something like asChild if the need arises. There are some tradeoffs to this though, eg it’s easier to mess up the DOM structure required for accessibility. There are some other approaches we’ll be documenting for this as well.
2. TS should be easier now. All the docs examples are now written in TS so you can see where the interfaces are imported from.
3. Yeah prop naming is a trade off. When we started, we decided to make all of the components follow a consistent naming convention. The DOM is quite limited in its functionality (eg you can only submit strings and not more complex objects) and we knew we’d need quite a bit more capability so we decided not to follow it in some areas. But we know integrating with form libraries is a pain point and we might have some documentation or helpers for that in the future.
2. You're spoiling us. Thank you. Re-visiting the hooks docs, I think I was just meant to use the `Aria...Props` interfaces the whole time haha. I have a feeling I overcomplicated some things in that codebase.
3. "you can only submit strings and not more complex objects" - I think this is the crux of the tradeoff. Frankly, the only thing I use Formik for when I bring it in is binding Zod schemas to form state to get validation messages in a pinch. If React Aria had a first-class schema-driven Form component or similar, I'd be a satisfied "customer".
Had another random question overnight -
4. Have y'all considered implementing anything in the realm of the "CSS Spatial Navigation" proposal? The conversation around it looks like it has gone quiet in the past few years, but I think there's definitely an opportunity for non-grid-y 2D navigation in web UIs.
What I really wish is that there were something like this built on web components, so I could use it with any framework.
I don't know. Maybe I just don't like "ecosystems"
Am I out of touch? What do you do when you have tons of custom functionality and styles? I spent about an year in a huge react only shop and I swear these guys were spending most of their time debugging other people's crappy components. Most of them had your reaction when we talked
Let's take a modal, for example. The easy part is clicking a button and having something show up on top of everything else. But do you…
- make sure screen readers announce it correctly when it's opened?
- make sure it has the correct ARIA attributes?
- trap focus within the modal so that you can't tab to an element in the page behind it?
- allow it to be closed with escape?
- return focus to the correct element when it closes?
These aren't arbitrary requirements I'm making up. The W3C has detailed documentation [1] about how people should be able to interact with these components. It's not something I want to spend my time doing, and frankly when it comes to things like ARIA attributes and screen readers it's not something I'm confident I'll do well. That's why libraries like Radix and React Aria are so valuable. You get rich, accessible components basically for free.
UI libs definitely have their drawbacks, but using headless component libraries like Radix or react-aria with your styling solution of choice gets rid of 90% of the problems they generally have.
And not just those but bigger alternatives like Vue. I've been looking for soemthing as good as Radix UI or React Aria Components or Atlas UI to use with Vue but it's just not there.
Much as I like Vue 3 the ecosystem is winning with React
edit: grammar
If you need strict accessibility and date picking, you're either making people enter dates in a masked input field or you're using react-aria. I'm sure there's a few other options out there, but you have to dig long and deep for them.
I found that the React samples on Tailwind UI, and their base component library, cover pretty much angle of accessibility.
I was working on a legacy angular upgrade to React upgrade for my employer and they were concerned I would downgrade their current compliance levels moving to Tailwinds components. I actually ended up drastically improving their compliance levels by following Tailwinds lead, and nothing was lost. For example, moving from font awesome to hero icons allowed for the ability to inject a screen reader only tag into the icon, allowing the screen reader to output an icons function.
The page applies `overflow: hidden` to the root element, which isn’t a very good solution to the “you shouldn’t be able to scroll the document while a dropdown is open” problem. It tries to counteract the scrollbar shift problem this causes by applying corresponding padding to the root element (which is still yuck, because the scrollbar still visually disappears, which it shouldn’t), but the page uses `position: fixed` for the header and right ToC sidebar, which bypasses the document padding. End result, opening a dropdown shifts parts of the document, which is bad.
I’m not sure if there’s a proper solution that makes it behave how a <select> does on desktop platforms; historically there wasn’t, and every historical workaround causes worse problems than it solves, in my opinion. Also in my opinion, the problem was never particularly important; the only part that was even vaguely notable was scroll chaining, and you can now just specify `overscroll-behavior: contain` on the dropdown popup to categorically fix that. (Browser support: 7½ months in Safari, 5–6 years elsewhere.) Very recently, browsers have been getting better support for modal concepts, driven by the <dialog> element and the corresponding concept of a top layer, as the spec calls it. I haven’t played with it, but I suspect that this might finally be the actual solution to the problem of stopping scrolling underneath.
Popper supports putting popper elements somewhere else in the DOM, but that’s not their general recommendation (https://popper.js.org/docs/v2/performance/#attaching-element...).
React Aria puts its popups in such a separate place in the DOM like you’re saying.
But the part I and the parent were remarking upon is what you do about scrolling.
Popper’s (default) answer is “recalculate the preferred position [and size, I think?] on every scroll event”. This is perfectly fine, and sometimes preferable.
React Aria’s answer is “position once and then prevent scrolling”. This matches all platforms’ conventions for these sorts of elements, it’s just that the way they’ve implemented it has problems, problems that may be unavoidable at present.
In any case I'm sure they will fix it if you open an issue against it..
The “Favorite Animal” dropdown shown in the linked page can be done with <select> with no loss, and therefore I’d say probably should be. But you can often do much better by skipping past <select>:
• Use more than just plain text in each option (very simple example: https://react-spectrum.adobe.com/react-aria/Select.html#comp...; also think of things like adding icons)
• Support typing on all platforms (and no, native combo boxes à la <input list="…"> + <datalist> aren’t far off completely useless in practice).
• Support typing with more complex matching, e.g. in a country selector supporting US/USA/United States, NL/Netherlands/The Netherlands/Holland, &c.
Is there a limitation in CSS preventing it?
This runs counter to the behavior of it being anchored to a parent container.
-1-----------
| |
| -2--- |
| | D | |
| ----- |
| |
-------------
If you attach the dropdown options to the inner container (2), they are cropped.If you attach the dropdown options to the outer container (1), they don't scroll with the rest of their component (D).
----
Non-native dropdowns in general are handicapped.
For Windows/MacOS/Linux, native dropdowns can exceed the bounds of even the OS window. There is no CSS equivalent of this behavior.
I've been interested in re-implementing smooth scrolling in JS to mimic native but.. Even if you hit solid native refresh at all time, I feel you'd have to switch your entire site to JS controlled scrolling for consistency and even then the scroll feel might not match native scroll on other pages per system settings.
It's a bit of a rabbit hole.
Embrace what the web exposes. Allowing document scrolling underneath is distinctly the lesser of two evils. With a bit of effort, you can block scrolling underneath in almost all cases, and I reckon that’s good enough and much better than what there is currently. (Also try asking browser makers, “y’know this ‘top layer’ thing you made for <dialog> modal presentation and fullscreen? Wouldn’t it be nice if we could prevent scrolling underneath a ::backdrop?”)
I do like the idea that you can use these components as a starting point and drop down to first principles with react-aria hooks if it suits the use case.
I'm excited to try these out and possibly switch over and fill in the gaps. Lots of headless systems are locked into a particular styling system but this looks super flexible and will work perfect with Tailwind. Love the callback for classname that supplies states.
Hope they keep the slot props concept from spectrum.
Edit: Slot props are in! Can't explain how much I like the Spectrum projects.
I haven't really grasped what slot are used for tho, can you share a tldr?
https://react-spectrum.adobe.com/react-spectrum/layout.html#...
Too bad React doesn't have Vue slots built-in, but this is nice for streamlining component APIs. Works well with Tailwind classes.
Sure, sure, but if your link doesn’t have an href, it’s because you don’t want a link, you want an unstyled button!
It’s really nice to be able to get aria gains without giving up creative control of the visuals.
I wonder if anyone here has used react-aria and also headless-ui and have any notes about their comparison.
It's a set of unstyled components components for common UI patterns, with accessibility, internationalization, interactions, and behavior built in, allowing you to focus on your unique design and styling rather than re-building these challenging aspects.
If you don't like doing UI/styling, this probably isn't for you as these components require you to style them yourself.
I've never used this particular library, but if it's does what it says on the box it should make it easier to have accessible components out of the box so the developer is only focused on making it look good.
There are so many UX pitfalls that I prefer to delegate the task of fixing them to a library, while still retaining control over how I want to style them.