fumadocs-core
Version:
The React.js library for building a documentation website
55 lines (54 loc) • 1.62 kB
JavaScript
import { FrameworkProvider } from "./index.js";
import { useMemo } from "react";
import { jsx } from "react/jsx-runtime";
import { Link, useParams, useRouter, useRouterState } from "@tanstack/react-router";
// src/framework/tanstack.tsx
const framework = {
Link({ href = "", prefetch = true, ...props }) {
const router = useRouter();
if (href.startsWith("#")) return /* @__PURE__ */ jsx("a", {
href,
...props
});
const hashIdx = href.indexOf("#");
const path = hashIdx === -1 ? href : href.slice(0, hashIdx);
const searchIdx = path.indexOf("?");
return /* @__PURE__ */ jsx(Link, {
to: searchIdx === -1 ? path : path.slice(0, searchIdx),
search: searchIdx === -1 ? void 0 : router.options.parseSearch(path.slice(searchIdx)),
hash: hashIdx === -1 ? void 0 : href.slice(hashIdx + 1),
preload: prefetch ? "intent" : false,
...props,
children: props.children
});
},
usePathname() {
return useRouterState({ select: (state) => (state.isLoading && state.resolvedLocation || state.location).pathname });
},
useRouter() {
const router = useRouter();
return useMemo(() => ({
push(url) {
router.navigate({ href: url });
},
refresh() {
router.invalidate();
}
}), [router]);
},
useParams() {
return useParams({ strict: false });
}
};
/**
* Fumadocs adapter for Tanstack Router/Start
*/
function TanstackProvider({ children, Link: CustomLink, Image: CustomImage }) {
return /* @__PURE__ */ jsx(FrameworkProvider, {
...framework,
Link: CustomLink ?? framework.Link,
Image: CustomImage ?? framework.Image,
children
});
}
export { TanstackProvider };