Using Tailwind's Catalyst UI with Tanstack Router
Hello! Catalyst requires a bit of setup with the client-side router (https://catalyst.tailwindui.com/docs#client-side-router-integration) and attempting to strictly type the
Link
component.
I pulled heavily from @Manuel Schiller's example from another thread (https://discord.com/channels/719702312431386674/1192472012585705504/1193343346215755828) and the below technically "works" but TS is stating that the type is wrong. Feel like I'm really close but overlooking something as I'm definitely out of my league here. Appreciate any help here and will create an issue in Tailwind's issues repo with the solution to add it to their docs!
Getting started - Catalyst UI Kit
Modern application UI components to kickstart your design system.
10 Replies
absent-sapphire•2y ago
can you provide a minimal example on e.g. codesandbox?
passive-yellowOP•2y ago
absent-sapphire•2y ago
i would just remove the
& Omit<LinkProps<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>, "to">,
https://codesandbox.io/p/devbox/catalyst-tanstack-router-forked-qv78hr?file=%2Fsrc%2Flink.tsx%3A34%2C27passive-yellowOP•2y ago
Tried that but breaks any component that utilizes the underlying Link component, updated the codesandbox with a sample component mirroring the TextLink component to give an example of it in use
absent-sapphire•2y ago
you need to forward the children: https://codesandbox.io/p/devbox/catalyst-tanstack-router-forked-4xfpxg?file=%2Fsrc%2Fmain.tsx%3A29%2C33
passive-yellowOP•2y ago
Thanks for the help! I'll work on updating the other components but this was extremely helpful!
absent-sapphire•2y ago
@B0NECH0P I were coming across the same thing a few days ago, this was my solution. Looks similar to yours, although I didn't passthrough all the generics.
Edit: Don't use this.. you don't get type safety for link props anymore...
I was struggeling with this once more today. My solution without forwarding the generics doesn't provide type safety in the catalyst components. But I also didn't got the solution from codesandbox above to work.
One additional problem occurred: Typesafety for 'props' does only work if there is a matching 'to'. Rematching 'to' to 'href' because Catalyst checks for the existence of 'href' in multiple places does make the 'props' not typesafe anymore.
But finally I found something which works for me and is - as far as I can tell now - fully typesafe:
The only downside of this is that I have to change all other Catalyst components mentioning 'Link' and replace 'href' by 'to'.
One more update: Changing all other Catalyst components would be quite complicated, so I - for now - ended with this:
The prop is called 'href' but it accepts an object 'ToOptions'. Downside now is that typescript does not warn for missing keys in params, but I am accepting that for now...
wise-white•13mo ago
I get this error on RouterLink while using this. 🤔
passive-yellowOP•13mo ago
Saw your DM @Baqir, here's what I ended up with:
In Catalyst's Link component:
There are a few Catalyst components that you need to update the href properties from strings to
LinkProps
from @tanstack/react-router
but works well and still maintaining some of the type safety from TanStack Router.wise-white•13mo ago
Thanks that works. I did not have to change anything in the other components.