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):classNameandstyleprops will work in the RAC way, but in fact they only work in the TSR way because TSR'suseLinkPropsgobbles the input.onHover,onPress, etc) will be properly used or merged with the resulting component.disabledvs 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
hrefprop using the type declaration is fine, but not perfect, though this can be fixed by wrapping input in thelinkOptionsfunction. What's missing?data-status="active"Both of the above are handled by inline code inside TSR's
useLinkPropshook.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 inputToOptionswith the fallbackfromprop filled by the leaf match fromuseMatchesiffromwasn't in the inputuseIsLinkActive: (toOptions: ToOptions, activeOptions?: ActiveOptions) => booleanAnd
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
usePreloadwith any supplied in props into RAC's Link.usePreloadalso requiresuseIntersectionObserverobviously, which I think for now is just an internal function not exported for consumers.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions