Skip to content

Commit

Permalink
Enable client-side routing for all Markdown links
Browse files Browse the repository at this point in the history
This results in much faster navigation when following links that
are defined in .md or .mdx documents using `[markdown](syntax)`.

See remix-run/remix#6048
  • Loading branch information
lpsinger committed May 16, 2023
1 parent 14108ea commit 9d07010
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
12 changes: 10 additions & 2 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ import {
useRouteError,
} from '@remix-run/react'
import { ButtonGroup, GovBanner, GridContainer } from '@trussworks/react-uswds'
import type { ReactNode } from 'react'
import { type ReactNode, useRef } from 'react'
import TopBarProgress from 'react-topbar-progress-indicator'
import { useDelegatedAnchors } from 'remix-utils'
import { useSpinDelay } from 'spin-delay'

import { DevBanner } from './components/DevBanner'
Expand Down Expand Up @@ -207,6 +208,11 @@ function Progress() {
}

function Document({ children }: { children?: React.ReactNode }) {
// Use client-side routing for anchors in Markdown content.
// See https://github.com/remix-run/remix/discussions/6048.
const ref = useRef<HTMLElement | null>(null)
useDelegatedAnchors(ref)

return (
<html lang="en-US">
<head>
Expand All @@ -223,7 +229,9 @@ function Document({ children }: { children?: React.ReactNode }) {
<DevBanner />
<Header />
<NewsBanner message="GCN Circulars are now part of the new GCN!" />
<main id="main-content">{children}</main>
<main id="main-content" ref={ref}>
{children}
</main>
<Footer />
<ScrollRestoration />
<Scripts />
Expand Down
130 changes: 127 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"react-router": "^6.10.0",
"react-tag-autocomplete": "^7.0.0-rc.1",
"react-topbar-progress-indicator": "^4.1.1",
"remix-utils": "github:lpsinger/remix-utils#route-data-moved-npm-prepare",
"spin-delay": "^1.1.0",
"ts-dedent": "^2.2.0"
},
Expand Down

0 comments on commit 9d07010

Please sign in to comment.