Skip to content

Move the editor's URL state onto react-router - #1325

Merged
microbit-matt-hillsdon merged 1 commit into
mainfrom
react-router
Sep 11, 2026
Merged

Move the editor's URL state onto react-router#1325
microbit-matt-hillsdon merged 1 commit into
mainfrom
react-router

Conversation

@microbit-matt-hillsdon

Copy link
Copy Markdown
Collaborator

Replaces the bespoke pushState/popstate router with react-router 7, as ml-trainer uses, ahead of adding pages beyond the editor. URLs are unchanged: the editor stays at the base URL with the documentation tab and anchor as optional path segments, and deeper paths remain the editor with no tab selected.

  • urls.ts holds the basename (derived from the Vite base URL) and the editor route path and link builder, used for both routes and links.
  • router.tsx creates the router. The root route wraps its outlet in the existing ErrorBoundary, so an uncaught render error shows the content-load-error message rather than unmounting the whole app.
  • router-hooks.tsx keeps the useRouterState/useRouterTabSlug API over useParams/useNavigate/useLocation. Focus travels in history state and the state identity is tied to location.key so navigating to the same anchor again re-runs scroll and focus effects, as before.
  • Search results use react-router's Link so the href respects the basename (the old toUrl produced a broken absolute URL).
  • The sidebar only resets the anchor when the clicked tab is the current one. It previously reset whenever an anchor was set and relied on the old router having updated state synchronously between pointer-up and click; with react-router that made clicking another tab from a deep link land on the current tab's top level.

@github-actions

Copy link
Copy Markdown

Preview build will be at
https://review-python-editor-v3.microbit.org/react-router/

Comment thread src/router.tsx
export const createRouter = () =>
createBrowserRouter(
[
{

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might expect 404 handling but this matches current behaviours.

@microbit-matt-hillsdon
microbit-matt-hillsdon force-pushed the react-router branch 2 times, most recently from 86640fc to d7fa324 Compare September 11, 2026 16:30
Replaces the bespoke pushState/popstate router with react-router 7, as
ml-trainer uses, ahead of adding pages beyond the editor. URLs are
unchanged: the editor stays at the base URL with the documentation tab
and anchor as optional path segments, and deeper paths remain the editor
with no tab selected.

- urls.ts holds the basename (derived from the Vite base URL) and the
  editor route path and link builder, used for both routes and links.
- router.tsx creates the router. The root route wraps its outlet in the
  existing ErrorBoundary, so an uncaught render error shows the
  content-load-error message rather than unmounting the whole app.
- router-hooks.tsx keeps the useRouterState/useRouterTabSlug API over
  useParams/useNavigate/useLocation. Focus travels in history state and
  the state identity is tied to location.key so navigating to the same
  anchor again re-runs scroll and focus effects, as before.
- Search results use react-router's Link so the href respects the
  basename (the old toUrl produced a broken absolute URL).
- The sidebar only resets the anchor when the clicked tab is the current
  one. It previously reset whenever an anchor was set and relied on the
  old router having updated state synchronously between pointer-up and
  click; with react-router that made clicking another tab from a deep
  link land on the current tab's top level.
@microbit-matt-hillsdon

Copy link
Copy Markdown
Collaborator Author

I'm going to merge this to help unblock some other work but @microbit-robert please can you review retrospectively.

@microbit-matt-hillsdon
microbit-matt-hillsdon merged commit 226ec54 into main Sep 11, 2026
1 check passed
@microbit-matt-hillsdon
microbit-matt-hillsdon deleted the react-router branch September 11, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant