Skip to content

Commit

Permalink
feat: Migrate router docs to v2 layout
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins committed Jan 25, 2024
1 parent 33ef71e commit e7bfd46
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 176 deletions.
71 changes: 70 additions & 1 deletion app/projects/router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@
import { Link } from '@remix-run/react'
import reactLogo from '~/images/react-logo.svg'
import { FaDiscord, FaGithub } from 'react-icons/fa/index'
import { useDocsConfig } from '~/utils/config'
import type { ConfigSchema, MenuItem } from '~/utils/config'

export const repo = 'tanstack/router'

export const v1branch = 'main'
export const latestBranch = 'main'
export const latestVersion = 'v1'
export const availableVersions = ['v1']

export const gradientText =
'inline-block text-transparent bg-clip-text bg-gradient-to-r from-lime-500 to-emerald-500'

const frameworks = {
react: { label: 'React', logo: reactLogo, value: 'react' },
} as const

export type Framework = keyof typeof frameworks

export const createLogo = (version?: string) => (
<>
<Link to="/" className="font-light">
TanStack
</Link>
<Link to=".." className={`font-bold`}>
<span className={`${gradientText}`}>Router</span>
<span className="text-sm align-super">
{version === 'latest' ? latestVersion : version}
</span>
</Link>
</>
)

export const localMenu: MenuItem = {
label: 'Menu',
children: [
{
label: 'Home',
to: '..',
},
{
label: (
<div className="flex items-center gap-2">
GitHub <FaGithub className="text-lg opacity-20" />
</div>
),
to: 'https://github.com/tanstack/router',
},
{
label: (
<div className="flex items-center gap-2">
Discord <FaDiscord className="text-lg opacity-20" />
</div>
),
to: 'https://tlinz.com/discord',
},
],
}

export function getBranch(argVersion?: string) {
const version = argVersion || latestVersion

return ['latest', latestVersion].includes(version) ? latestBranch : version
}

export const useRouterDocsConfig = (config: ConfigSchema) => {
return useDocsConfig({
config,
frameworks,
localMenu,
availableVersions,
})
}
5 changes: 2 additions & 3 deletions app/routes/router.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { redirect } from '@remix-run/node'
export const loader: LoaderFunction = (context) => {
handleRedirects(context)

return redirect('/router/v1')
return redirect('/router/latest')
}

function handleRedirects(context: Parameters<LoaderFunction>[0]) {
Expand All @@ -27,8 +27,7 @@ function handleRedirects(context: Parameters<LoaderFunction>[0]) {
reactLocationv2List.forEach((item) => {
if (url.pathname.startsWith(`/router/react-location/${item.from}`)) {
throw redirect(
`/router/v1/${item.to}?from=reactLocationV2&original=https://react-location-v2.tanstack.com/${item.from}`,
301
`/router/v1/${item.to}?from=reactLocationV2&original=https://react-location-v2.tanstack.com/${item.from}`
)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import {
FaGithub,
FaTshirt,
} from 'react-icons/fa'
import { Link, useLoaderData } from '@remix-run/react'
import { Link, useLoaderData, useParams } from '@remix-run/react'
import { json } from '@remix-run/node'
import { TbHeartHandshake, TbZoomQuestion } from 'react-icons/tb'
import { VscPreview } from 'react-icons/vsc'
import { RiLightbulbFlashLine } from 'react-icons/ri'
import { gradientText, v1branch } from '~/projects/router'
import { gradientText, getBranch } from '~/projects/router'
import { Carbon } from '~/components/Carbon'
import { Footer } from '~/components/Footer'
import SponsorPack from '~/components/SponsorPack'
import { Logo } from '~/components/Logo'
import { getSponsorsForSponsorPack } from '~/server/sponsors'
import type { LoaderFunction } from '@remix-run/node'
import type { Framework } from '~/projects/router'

const menu = [
{
Expand Down Expand Up @@ -81,10 +82,9 @@ export const loader: LoaderFunction = async () => {

export default function TanStackRouterRoute() {
const { sponsors } = useLoaderData<typeof loader>()
const [framework] = React.useState<
'react' | 'preact' | 'svelte' | 'vue' | 'solid'
>('react')

const { version } = useParams()
const branch = getBranch(version)
const [framework] = React.useState<Framework>('react')
const [isDark, setIsDark] = React.useState(true)

React.useEffect(() => {
Expand Down Expand Up @@ -338,7 +338,7 @@ export default function TanStackRouterRoute() {
<div className="bg-white dark:bg-black">
<iframe
key={framework}
src={`https://stackblitz.com/github/tanstack/router/tree/${v1branch}/examples/${framework}/kitchen-sink-file-based?file=src%2Fmain.tsx&embed=1&theme=${
src={`https://stackblitz.com/github/tanstack/router/tree/${branch}/examples/${framework}/kitchen-sink-file-based?file=src%2Fmain.tsx&embed=1&theme=${
isDark ? 'dark' : 'light'
}`}
title="tannerlinsley/router: kitchen-sink"
Expand Down
44 changes: 44 additions & 0 deletions app/routes/router.$version.docs.$.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useLoaderData, useParams } from '@remix-run/react'
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node'
import { repo, getBranch } from '~/projects/router'
import { DefaultErrorBoundary } from '~/components/DefaultErrorBoundary'
import { seo } from '~/utils/seo'
import { Doc } from '~/components/Doc'
import { loadDocs } from '~/utils/docs'

export const loader = async (context: LoaderFunctionArgs) => {
const { '*': docsPath, version } = context.params
const { url } = context.request

return loadDocs({
repo,
branch: getBranch(version),
docPath: `docs/${docsPath}`,
currentPath: url,
redirectPath: url.replace(/\/docs.*/, '/docs/react/overview'),
})
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return seo({
title: `${data?.title ?? 'Docs'} | TanStack Router Docs`,
description: data?.description,
})
}

export const ErrorBoundary = DefaultErrorBoundary

export default function RouteReactTableDocs() {
const { title, content, filePath } = useLoaderData<typeof loader>()
const { version } = useParams()
const branch = getBranch(version)
return (
<Doc
title={title}
content={content}
repo={repo}
branch={branch}
filePath={filePath}
/>
)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { redirect } from '@remix-run/node'
import type { LoaderFunction } from '@remix-run/node'
import { redirect } from '@remix-run/node'

export const loader: LoaderFunction = (context) => {
throw redirect(
context.request.url.replace(/\/examples.*/, '/examples/react/basic')
context.request.url.replace(/\/docs.*/, '/docs/react/overview')
)
}
45 changes: 45 additions & 0 deletions app/routes/router.$version.docs.framework.$framework.$.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node'
import { repo, getBranch } from '~/projects/router'
import { DefaultErrorBoundary } from '~/components/DefaultErrorBoundary'
import { seo } from '~/utils/seo'
import { useLoaderData, useParams } from '@remix-run/react'
import { Doc } from '~/components/Doc'
import { loadDocs } from '~/utils/docs'

export const loader = async (context: LoaderFunctionArgs) => {
const { '*': docsPath, framework, version } = context.params
const { url } = context.request

return loadDocs({
repo,
branch: getBranch(version),
docPath: `docs/framework/${framework}/${docsPath}`,
currentPath: url,
redirectPath: url.replace(/\/docs.*/, '/docs/react/overview'),
})
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return seo({
title: `${data?.title} | TanStack Router Docs`,
description: data?.description,
})
}

export const ErrorBoundary = DefaultErrorBoundary

export default function RouteDocs() {
const { title, content, filePath } = useLoaderData<typeof loader>()
const { version } = useParams()
const branch = getBranch(version)

return (
<Doc
title={title}
content={content}
repo={repo}
branch={branch}
filePath={filePath}
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { redirect } from '@remix-run/node'
import type { LoaderFunctionArgs } from '@remix-run/node'

export const loader = async (context: LoaderFunctionArgs) => {
throw redirect(
context.request.url.replace(/\/docs.*/, '/docs/react/overview')
)
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import * as React from 'react'
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node'
import { json } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
import { useLoaderData, useParams } from '@remix-run/react'
import { DocTitle } from '~/components/DocTitle'
import { repo, v1branch } from '~/projects/router'
import { repo, getBranch } from '~/projects/router'
import { seo } from '~/utils/seo'
import { capitalize, slugToTitle } from '~/utils/utils'
import { FaExternalLinkAlt } from 'react-icons/fa'

export const loader = async (context: LoaderFunctionArgs) => {
const { '*': examplePath } = context.params
const [kind, _name] = (examplePath ?? '').split('/')
const [name, search] = _name.split('?')
const { framework, '*': name } = context.params

return json({ kind, name, search: search ?? '' })
return json({ framework, name })
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return seo({
title: `${capitalize(data.kind)} Router ${slugToTitle(
title: `${capitalize(data.framework)} Router ${slugToTitle(
data.name
)} Example | TanStack Router Docs`,
description: `An example showing how to implement ${slugToTitle(
data.name
)} in ${capitalize(data.kind)} Router`,
)} in ${capitalize(data.framework)} Router`,
})
}

export default function RouteReactTableDocs() {
const { kind, name, search } = useLoaderData<typeof loader>()
const { framework, name } = useLoaderData<typeof loader>()
const { version } = useParams()
const branch = getBranch(version)

const examplePath = [kind, name].join('/')
const examplePath = [framework, name].join('/')

const [isDark, setIsDark] = React.useState(true)

React.useEffect(() => {
setIsDark(window.matchMedia?.(`(prefers-color-scheme: dark)`).matches)
}, [])

const githubUrl = `https://github.com/${repo}/tree/${v1branch}/examples/${examplePath}`
const stackBlitzUrl = `https://stackblitz.com/github/${repo}/tree/${v1branch}/examples/${examplePath}?${search}embed=1&theme=${
const githubUrl = `https://github.com/${repo}/tree/${branch}/examples/${examplePath}`
const stackBlitzUrl = `https://stackblitz.com/github/${repo}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}`
const codesandboxUrl = `https://codesandbox.io/s/github/${repo}/tree/${v1branch}/examples/${examplePath}?${search}embed=1&theme=${
const codesandboxUrl = `https://codesandbox.io/s/github/${repo}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}`

Expand All @@ -51,7 +51,7 @@ export default function RouteReactTableDocs() {
<div className="p-4 lg:p-6">
<DocTitle>
<span>
{capitalize(kind)} Example: {slugToTitle(name)}
{capitalize(framework)} Example: {slugToTitle(name)}
</span>
<div className="flex items-center gap-4 flex-wrap font-normal text-xs">
<a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LoaderFunction } from '@remix-run/node'
import { redirect } from '@remix-run/node'
import type { LoaderFunction } from '@remix-run/node'

export const loader: LoaderFunction = (context) => {
throw redirect(context.request.url.replace(/\/docs.*/, '/docs/overview'))
throw redirect(context.request.url.replace(/\/examples.*/, '/examples/basic'))
}
49 changes: 49 additions & 0 deletions app/routes/router.$version.docs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Outlet, json, useLoaderData } from '@remix-run/react'
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node'
import {
repo,
getBranch,
useRouterDocsConfig,
createLogo,
} from '~/projects/router'
import { seo } from '~/utils/seo'
import { DocsLayout } from '~/components/DocsLayout'
import { getTanstackDocsConfig } from '~/utils/config'

export const loader = async (context: LoaderFunctionArgs) => {
const { version } = context.params
const branch = getBranch(version)
const tanstackDocsConfig = await getTanstackDocsConfig(repo, branch)

return json({
tanstackDocsConfig,
version,
})
}

export const meta: MetaFunction = () => {
return seo({
title: 'TanStack Router Docs | React Router',
description: 'Modern and scalable routing for React applications',
})
}

export default function DocsRoute() {
const { tanstackDocsConfig, version } = useLoaderData<typeof loader>()
let config = useRouterDocsConfig(tanstackDocsConfig)

return (
<DocsLayout
v2={true}
logo={createLogo(version)}
colorFrom={'from-lime-500'}
colorTo={'to-emerald-500'}
textColor={'text-emerald-500'}
config={config}
framework={config.frameworkConfig}
version={config.versionConfig}
>
<Outlet />
</DocsLayout>
)
}
5 changes: 5 additions & 0 deletions app/routes/router.v1.tsx → app/routes/router.$version.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Link, Outlet, useLocation, useSearchParams } from '@remix-run/react'
import { DefaultErrorBoundary } from '~/components/DefaultErrorBoundary'
import { useClientOnlyRender } from '~/utils/useClientOnlyRender'

export const ErrorBoundary = DefaultErrorBoundary

Expand All @@ -10,6 +11,10 @@ export default function RouteReactRouter() {
const show = params.get('from') === 'reactLocationV2'
const original = params.get('original')

if (!useClientOnlyRender()) {
return null
}

return (
<>
{show ? (
Expand Down
Loading

0 comments on commit e7bfd46

Please sign in to comment.