Better support for custom links #4338
jdharrisnz
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I'm using React Aria Components. For creating custom links, there are two ways to do it:
Option 1 is the more full-featured way to do it, but there are several clashes with RAC when doing something like
createLink(AriaLink)
:className
andstyle
props will work in the RAC way, but in fact they only work in the TSR way because TSR'suseLinkProps
gobbles the input.onHover
,onPress
, etc) will be properly used or merged with the resulting component.disabled
vs RAC'sisDisabled
, which both show up in the result.So it's just not a very nice result.
Next, for option 2, setting up the RAC's RouterProvider and type declaration is just good practice in any case, since it integrates with more components than just Link. With this method, you get a Link component that avoids all of the above weird TypeScript issues and has consistent prop types. The autocomplete support in the
href
prop using the type declaration is fine, but not perfect, though this can be fixed by wrapping input in thelinkOptions
function. What's missing?data-status="active"
Both of the above are handled by inline code inside TSR's
useLinkProps
hook.I would like to suggest that this lib extracts the relevant code into exported hooks, so that consumers can augment their own Link components with usePreload and useIsLinkActive.
For the time being, I've done this myself by writing my own hooks from looking at the source code. I needed these three:
useToOptions: (options: ToOptions) => ToOptions
, returns inputToOptions
with the fallbackfrom
prop filled by the leaf match fromuseMatches
iffrom
wasn't in the inputuseIsLinkActive: (toOptions: ToOptions, activeOptions?: ActiveOptions) => boolean
And
usePreload
(types come from my own Link component, but it's just a combination of TSR Link and RAC Link):In my case, I just manually merge the event handlers from
usePreload
with any supplied in props into RAC's Link.usePreload
also requiresuseIntersectionObserver
obviously, which I think for now is just an internal function not exported for consumers.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions