API design of high-level library on top of React-Aria #2569
Replies: 1 comment 3 replies
-
I think my first thought is that it's pretty clever. I think the thing I'd worry about most is memo'ing all the Components you pass out so that React doesn't unmount/mount things repeatedly. I've included some bits to track mounting and unmounting for the button and label. I'd expect them to only mount once and never unmount in this particular app unless I closed the page, but that's not the case https://codesandbox.io/s/unruffled-cherry-3mtv6 I see mounting and unmounting just in response to hovering elements. I'm not sure I have any suggestions for improving it unfortunately. Even if you tracked prop changes and only updated the memo'd component if there were prop changes, it'd still result in a new cycle of mounting. You may find this discussion to be a useful read as well: #2450 |
Beta Was this translation helpful? Give feedback.
-
Hey! I've been working on creating a new high-level interface on top of React-Aria. Think something kind of like Radix UI or Headless UI but the main difference being it's easy to "drop down" into lower levels to get the behavior and accessibility props. Those libraries kinda lock you into using their components, and instead I am creating a library here that gives you control on how high or low of a level you need the behavior of your components.
One thing I have been thinking about is a proper API design of this library. Here are some notes on a potential API:
Suppose you are trying to use a Select component. You can do so at a high-level with this:
But suppose you decide you want to manually style or position the label and the popover. You can do:
But suppose you decide to use custom label elements for your select. Then maybe:
The idea is that you can "drop down" the levels as you need, with the high levels and the low levels available as you need them from the same hook. The high level components get their accessibility props fully injected in, so you always stay fully accessible no matter what level you decide to use.
The
useSelect
hook could be implemented like this:Here is a sandbox that kinda shows how this can be built.
https://codesandbox.io/s/bold-kepler-w01w2?file=/src/App.tsx
I know the code is ugly, etc. If everything were fleshed out, the
useButton
would return the Button component (instead of it being built inside the Select), and there would beusePopover
anduseListbox
hooks being used. I also know the API doesn't match the one above perfectly: its kind of a mess to assign properties to functions in Typescript.Is this a confusing API? Is it okay? What could be fixed, and how would you approach it?
I'd really like to get some feedback on this approach/idea, @devongovett @snowystinger
Beta Was this translation helpful? Give feedback.
All reactions