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 Apr 13, 2023
1 parent ac71839 commit ad18674
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 5 deletions.
10 changes: 8 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 AnnounceBanner, { AnnouncementEvent } from './components/AnnounceBanner'
Expand Down Expand Up @@ -199,6 +200,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 Down Expand Up @@ -242,7 +248,7 @@ function Document({ children }: { children?: React.ReactNode }) {
<GovBanner />
<DevBanner />
<Header />
<section className="usa-section main-content">
<section className="usa-section main-content" ref={ref}>
<GridContainer>{children}</GridContainer>
</section>
<Footer />
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": "^6.1.0",
"spin-delay": "^1.1.0",
"ts-dedent": "^2.2.0"
},
Expand Down

0 comments on commit ad18674

Please sign in to comment.