expose as/ref utilities #800
Replies: 4 comments
-
Hey! Thank you for your suggestion! In Headless UI itself we have some helpers to make rendering components easier, currently it has these options built-in:
Both of these will also ensure that TypeScript starts showing all the props available based on:
We also have the functionality to write your component using a render prop: <Menu.Button>
{({ open }) => (
)}
</Menu.Button> Internally we call this a We also override the We are also able to enable certain features per component:
Long story short we have bunch of features here. Would you imagine some API that allows you to create components yourself that expose all of this functionality? Or would you like a bit more control where you can enable/disable all these features? |
Beta Was this translation helpful? Give feedback.
-
@RobinMalfait interesting! Good question. Makes me think two things…
So not sure if that totally answers your question! Overall though I trust you to figure out these useful abstractions across UI components, so I'm personally game for whatever other built-in features happen to come along with the custom component factory. For me so far the biggest issues have been I definitely think something in this realm would be very helpful though for generic components that are copy-pasted from Tailwind UI. Right now the stateful ones are nicely handled with things like |
Beta Was this translation helpful? Give feedback.
-
I was looking for a way to bring focus to the select component. I guess a ref wouldn't be enough here because it's not a under the hood. |
Beta Was this translation helpful? Give feedback.
-
Hey! I converted the issue to a discussion since this is a cool idea but not really an issue. |
Beta Was this translation helpful? Give feedback.
-
Hello, thank you for this awesome library! I love the direction it's going, and the simplicity of some of these headless components is really well done.
The problem I'm running in to is that there is a decent amount of boilerplate required to build a "proper" UI kit component that makes it slightly more work than just copy-pasting from Tailwind UI currently. (Although the current workflow with Tailwind CSS + Tailwind UI is already amazing!)
The boilerplate I'm talking about is pieces like ref forwarding, prop typing, and
as
handling.I'd like to propose exposing some utilities that Headless UI defines internally, since it's something that is needed when building the JS/TS versions of lots of components. Specifically to expose a "factory" function that handles
as=
andref=
and all the associated complexity of typings for those two props.For a specific example, consider copying an
<input>
component from https://tailwindui.com/components/application-ui/forms/input-groups and wanting to let it be one of three sizes.In React you might write something like:
This is easy to arrive at fairly quickly from copying the initial snippet from Tailwind UI.
But then if you're using TypeScript you need to write the correct types too. (And unfortunately it's non-trivial to figure out the exact right types to use for the given element.) But you end up with:
And then you realize while hooking up some parts of your app that this isn't complete either, because you need to handle ref-forwarding to make it integrate properly with other UI pieces. So you do that:
And a bit later, say you have a specific place in your app where you want to make use of a "currency input" (like
react-currency-input-field
), you realize that it's not simple to do with your component because you don't have theas=
prop like other UI kits do.So you try to add that, but it's extremely non-trivial to figure out... But I see that a lot of the complex logic is already written internally for
@headless-ui
, so it would be amazing to expose it for others to build with too.It would be great to have a solution like:
Which handles three things for you:
ref
forwarding with the proper element type.props
with the proper default props based on the element type.as
prop with support for overriding and proper typings when overridden.I think this is something that pretty much any component you define in your own personal UI kit after copying the snippets from Tailwind UI would need.
Hope that makes sense! Happy to give more details too if needed.
Beta Was this translation helpful? Give feedback.
All reactions