@adrienlcp/react-router
Des liens react-aria qui naviguent par react-router : un RouterProvider branché sur useNavigate, les URL externes laissées telles quelles, NavigateOptions typé sur chaque lien
Fonctionne avec
- React Router
- React Aria
- React
- TypeScript
Installation
pnpm add @adrienlcp/react-routerExports
Groupés par la section de la documentation qui les explique. Une ligne mène à sa section.
ignoreSupersededNavigation1 export
Documentés dans le code seulement1 export
Documentation
Le README et les documents à côté, tels qu’écrits dans le dépôt.
PrésentationREADME.md
For apps whose links are
react-aria-components and
whose routes are react-router: the wiring every
one of them repeats, kept in one version across them. React 19,
react-aria-components and react-router are peers.
pnpm add @adrienlcp/react-routerLinks that navigate on the clientREADME.md
import { AriaRouterProvider } from '@adrienlcp/react-router'
import { ViewTransition } from 'react'
import { Outlet, useLocation } from 'react-router'
export const RootRoute: React.FC = () => {
const { pathname } = useLocation()
return (
<AriaRouterProvider>
<ViewTransition default="none" enter="auto" exit="auto" key={pathname}>
<Outlet />
</ViewTransition>
</AriaRouterProvider>
)
}react-router wraps its updates in startTransition, so React's
<ViewTransition> animates each navigation; keyed on the path with
default="none", it animates only a change of page, not the loads and
reloads of data in between. Leave react-router's own viewTransition option
off: it starts the transition outside React. @adrienlcp/styles/reduced-motion.css
stills it for a reader who asked for less motion.
Mounted in the root route's element, inside the router, it hands
react-aria's RouterProvider react-router's navigate: an href on any
react-aria Link, MenuItem or ListBoxItem below it becomes a client-side
navigation instead of a page load.
routerOptionsare typed and forwarded. Importing anything from this package augments react-aria'sRouterConfig, sorouterOptionson every react-aria link is react-router'sNavigateOptions:<Link href={paths.settings} routerOptions={{ replace: true }}>. Drop the app's owndeclare module 'react-aria-components'block.navigateDefaultsis read when each navigation happens, not when the provider renders, so a default computed from live state follows a change made mid-session. A link's ownrouterOptionsoverride it.- A superseded navigation is not an error. One navigation cut short by the
next rejects with
AbortError, more often while a view transition runs; that rejection is swallowed, any other one is not. - Give links absolute paths. A relative
hrefis displayed resolved against the link's route, butnavigateruns from the root route.
An external URL left aloneREADME.md
react-router's useHref resolves every href against the current route, an
external URL included: https://example.com would come out as
/current/route/https:/example.com. AriaRouterProvider hands react-aria an
useHref that returns an absolute URL untouched and defers to react-router's
for the rest.
ignoreSupersededNavigationREADME.md
The .catch handler behind the provider, for an app that calls navigate
itself: Promise.resolve(navigate(to)).catch(ignoreSupersededNavigation)
swallows the AbortError of a superseded navigation and throws anything else
again.