Router Breadcrumbs (Displayable Routes) #962
Replies: 4 comments 9 replies
-
kind bump |
Beta Was this translation helpful? Give feedback.
-
I'm imagining this as two simple utilities as an API to build breadcrumbs; a hook and a link component. Something like useBreadcrumbs interface BreadcrumbRoute {
path: string;
name: string; // where does this come from?
// TODO any more?
}
export function useBreadcrumbs(): Array<BreadcrumbRoute> {
// TODO MAGIC reactive
return collectedBreadcrumbs;
} and BreadcrumbLink type Props = PropsWithChildren<{link: BreadcrumbRouter}>;
export function BreadcrumbLink({ link, children }: Props) {
// custom onclick handling that pops history
// parent could pass name? do we just need path then
return <Link to={link.path}>{children}</Link>;
// can we use Link internally or since we are essentially pressing the browser's
// back button n times.
} The "press browser back btn n times" feels correct, but probably has subtle things that are wrong about it (ie can you suspend routing or batch n back btn presses?). |
Beta Was this translation helpful? Give feedback.
-
This works for me //use-tsr-breadcrumbs
import { useLocation } from "@tanstack/react-router";
export function useTSRBreadCrumbs() {
const current = useLocation();
const route_history = current.pathname
.split("/")
.filter((x) => x && x.length > 0);
const breadcrumb_routes = route_history.reduce(
(acc: { name: string; path: string }[], route) => {
const prev_path = acc[acc.length - 1]?.path ?? "";
acc.push({ name: route, path: `${prev_path}/${route}` });
return acc;
},
[],
);
return { breadcrumb_routes };
} //TSRBreadCrumbs.tsx
import { Link } from "@tanstack/react-router";
import { useTSRBreadCrumbs } from "./use-bread-crumbs";
import { ChevronRight } from "lucide-react";
interface TSRBreadCrumbsProps {}
export function TSRBreadCrumbs({}: TSRBreadCrumbsProps) {
const { breadcrumb_routes } = useTSRBreadCrumbs();
if(breadcrumb_routes.length<2)return null
return (
<div className="w-full flex flex-wrap md:justify-end gap-0.1 p-1">
{breadcrumb_routes.map((crumb) => {
if (breadcrumb_routes.length - 1 === breadcrumb_routes?.indexOf(crumb)) {
return (
<span key={crumb.path} className="text-xs bread-crumbs-links">
{crumb.name}
</span>
);
}
return (
<span key={crumb.path} className="flex gap-1 justify-center items-center">
<Link className="text-xs bread-crumbs-links" to={crumb.path}>
{crumb.name}
</Link>
<ChevronRight className="size-4" />
</span>
);
})}
</div>
);
}
|
Beta Was this translation helpful? Give feedback.
-
We've been trying to address breadcrumbs recently with some experimental changes with Firstly We have It would be great to get some feedback |
Beta Was this translation helpful? Give feedback.
-
Hello,
First time suggesting a feature so please let me know if you need any more details.
For my Router Layout Component, I am doing something like this.
And, I am currently generating breadcrumbs like so:
And in my Breadcrumbs component I have to do something like this to replace paramIds with their actual value
It works decently but as you can probably imagine, it's very fragile especially when I attempt to work with static nested paths (ie.
['/', '/users/$userId', '/users/$userId/']
<-- not sure why but '/users' got skipped. Is there currently a way I could easily get breadcrumbs from the router? If not, this is my suggested feature.Ideally, we could be provided a way to provide displayable names for each of the routes, that way we could retrieve that displayable name of the route that could then be used for the breadcrumbs.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions