Skip to content

Allow navigate component to accept integers for go back/forward#56

Merged
Archmonger merged 5 commits into
mainfrom
fix-52-navigate-go-back-forward
Jul 21, 2026
Merged

Allow navigate component to accept integers for go back/forward#56
Archmonger merged 5 commits into
mainfrom
fix-52-navigate-go-back-forward

Conversation

@Archmonger

Copy link
Copy Markdown
Contributor

Description

Allow the navigate component to accept integers for the to parameter, enabling relative navigation in the browser's history stack — e.g., navigate(-1) to go back, navigate(1) to go forward.

This works by:

  • Python side: to parameter typed as str | int. When an integer is received, the path-matching comparison is skipped (since an integer has no path to compare), and the JS-side Navigate component is always rendered.
  • JS side: The Navigate component checks typeof to === "number" and calls window.history.go(to). The resulting popstate event is handled by the existing History component, which already updates the server-side location state — so no duplicate callback is needed.
  • TypeScript: NavigateProps.to updated to string | number.

Checklist

  • Tests have been developed for bug fixes or new functionality.
  • The changelog has been updated, if necessary.
  • Documentation has been updated, if necessary.
  • GitHub Issues closed by this PR have been linked.

Closes #52

By submitting this pull request I agree that all contributions comply with this project's open source license(s).

The `navigate()` component now accepts `str | int` for the `to` parameter.
When an integer is provided, the JS-side uses `history.go(to)` to perform
relative navigation in the browser's history stack (e.g. `navigate(-1)` to
go back, `navigate(1)` to go forward).

Key changes:
- Python: `navigate(to: str | int, ...)` — integer paths bypass path-matching
  logic and always render the JS Navigate component
- JS: `Navigate` component checks `typeof to === "number"` and calls
  `window.history.go(to)`; the resulting popstate event is handled by the
  existing History component
- JS: `NavigateProps.to` type updated to `string | number`
- Tests: Added `test_navigate_component_go_back` and
  `test_navigate_component_go_forward` E2E tests

Closes #52
…utton content

When navigate() was rendered as a child of html.button(), the Navigate
component's JS null-render left the button with empty text. Changed tests
to return navigate() at the component level instead.
Python's bool is a subclass of int, so isinstance(True, int) is True.
Add explicit not isinstance(to, bool) guard to prevent booleans from
being misinterpreted as relative history navigation.
Pyright correctly flags that `isinstance(to, bool)` doesn't narrow the
`str | int` union since `bool` is a subclass of `int`. Use `type(to) is int`
for a strict exact-type check that both satisfies the type checker and
correctly excludes booleans.
Pyright cannot narrow to str after isinstance(to, int) since bool is
a subclass of int. Wrapping the string-only code path in an explicit
isinstance(to, str) check resolves the type error.
@Archmonger
Archmonger marked this pull request as ready for review July 21, 2026 12:35
@Archmonger
Archmonger requested a review from a team as a code owner July 21, 2026 12:35
@Archmonger
Archmonger merged commit 4c6c6dc into main Jul 21, 2026
9 checks passed
@Archmonger
Archmonger deleted the fix-52-navigate-go-back-forward branch July 21, 2026 12:46
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.

Allow navigate component to go back or go forward

1 participant