Skip to content
Merged
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b151712
Expand Navigation docs
amirhhashemi Feb 6, 2025
a16ba88
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 6, 2025
e053840
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 6, 2025
1cf36b1
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
c059740
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
ecce078
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
95c4fed
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
66269be
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
ac6cf91
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
b2ab9ae
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
7a7127d
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 7, 2025
b616e7a
update
amirhhashemi Feb 7, 2025
209b5c2
update
amirhhashemi Feb 7, 2025
46b600c
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 7, 2025
ce9542f
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 7, 2025
9a118ed
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 7, 2025
ffc069e
update
amirhhashemi Feb 7, 2025
82911a8
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 7, 2025
3183c0a
Update src/routes/solid-router/concepts/navigation.mdx
amirhhashemi Feb 8, 2025
445314c
add callout
amirhhashemi Feb 8, 2025
aeb29e5
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 10, 2025
e654bab
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 13, 2025
7bcf0e4
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 13, 2025
c49accf
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 13, 2025
93f3aef
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 13, 2025
f513dcd
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 16, 2025
25b3341
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 16, 2025
dcfc501
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 16, 2025
d28cf79
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
053da8b
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
3e492a9
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
dcfa2ea
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
eb4c92b
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
cd379a8
update
amirhhashemi Feb 18, 2025
e0b2d76
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
c0f374e
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
f8c554c
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 18, 2025
148d9f7
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 25, 2025
df2b9e0
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 25, 2025
070f0d7
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 26, 2025
ca4f7a7
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Feb 26, 2025
e5855f4
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Mar 4, 2025
08550b5
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Mar 4, 2025
a97979e
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Mar 6, 2025
47d983e
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Mar 7, 2025
195025c
Merge branch 'main' into expand-navigation-docs
kodiakhq[bot] Mar 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 113 additions & 45 deletions src/routes/solid-router/concepts/navigation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,136 @@
title: "Navigation"
---

Once your routes have been created within an application, [anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a) can be used to help users navigate between different views or pages.
SolidStart provides several ways to manage navigation between routes:
Comment thread
LadyBluenotes marked this conversation as resolved.
Outdated

```jsx {4-5}
const App = (props) => (
<>
- Using the native [`<a>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a).
- Using the [`<A>` component](/solid-router/reference/components/a).
- Using the [`useNavigate` primitive](/solid-router/reference/primitives/use-navigate).
- Using the [`redirect` function](/solid-router/reference/response-helpers/redirect).

This page provides an overview of these options.
Comment thread
LadyBluenotes marked this conversation as resolved.
Outdated
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

## `<a>` element

The standard HTML `<a>` element is the most basic way to create links.
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated
In SolidStart, clicking an `<a>` element triggers [soft navigation](/solid-router/reference/components/a#soft-navigation).
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

```tsx
function Navbar() {
return (
<nav>
<a href="/users">Users</a>
<a href="/">Home</a>
<a href="/login">Login</a>
</nav>
<h1>My Site with lots of pages</h1>
{props.children}
</>
);
);
}
```

## `<A>` component

While you can use the native HTML anchor tag, Solid Router does provide a [`<A>`](/solid-router/reference/components/a) component.
While similar to an anchor tag, the `<A>` component supports automatically applying the base URL path and relative paths.

```jsx {4-5}
const App = (props) => (
<>
<nav>
<A href="/">Home</A>
<A href="/users">Users</A>
</nav>
<h1>My Site with lots of pages</h1>
{props.children}
</>
);
The `<A>` component extends the native `<a>` element.
It automatically handles the router's base URL and supports relative paths.
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

```tsx
import { A } from "@solidjs/router";

function DashboardPage() {
return (
<main>
{/* This is a relative path that, from /dashboard, links to /dashboard/users */}
<A href="users">Users</A>
</main>
Comment on lines +40 to +27
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
<main>
{/* This is a relative path that, from /dashboard, links to /dashboard/users */}
<A href="users">Users</A>
</main>
<nav>
<A href="/">Home</A>
<A href="/users">Users</A>
</nav>

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.

I like to keep the relative path example because I didn't find an example anywhere else.

If it's not very clear that the href prop of <A> is exactly like <a>, I can add a regular link too.

);
}
```

### Styling links
<Callout type="info" title="Note">

`<A>` can only be used within a component that is rendered by a [`<Route>`](/solid-router/reference/components/route).

</Callout>

See the [`<A>` API reference](/solid-router/reference/components/a) for more information.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Callout is something that should be included in the router section instead of SolidStart.

Suggested change
<Callout type="info" title="Note">
`<A>` can only be used within a component that is rendered by a [`<Route>`](/solid-router/reference/components/route).
</Callout>
See the [`<A>` API reference](/solid-router/reference/components/a) for more information.
See the [`<A>` API reference](/solid-router/reference/components/a) in the Solid Router section for more information.

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.

Again, my mistake. This page is in the Solid Router docs.


### Styling

The `<A>` component provides `activeClass` and `inactiveClass` props to style links based on their active state.
These props apply corresponding CSS classes to the link.
If these props are omitted, the classes `active` and `inactive` are used by default.
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

A link is considered active if the current route matches the link's `href` or is a descendant of it.
For example, a link with `href="/dashboard"` is active when the current route is `/dashboard`, `/dashboard/users`, or `/dashboard/users/1/profile`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
A link is considered active if the current route matches the link's `href` or is a descendant of it.
For example, a link with `href="/dashboard"` is active when the current route is `/dashboard`, `/dashboard/users`, or `/dashboard/users/1/profile`.
By default, a link is considered active when the current route matches the `href` or any of its descendant routes.
For example, if the route `/users` is specified, it would also match `/users`, `/users/1`, `/users/1/posts`, and any other nested routes.

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.

if the route /users is specified

It's not clear that where /users is specified.


The `end` prop modifies this behavior.
`end={true}` requires an exact match between the link's `href` and the current route for the link to be considered active.
This is particularly useful for root route links (`href="/"`), which would otherwise match all routes.
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

In addition, the `<A>` component also provides an `activeClass` and `inactiveClass` prop.
This prop accepts a string that, when provided, will apply the specified class to the anchor tag based on the current route.
If the current route matches the `href` prop, the `activeClass` class will be applied, otherwise the `inactiveClass` class will be applied.
```tsx
import { A } from "@solidjs/router";

```jsx
<A href="/users" activeClass="underlined" inactiveClass="default">
Users
</A>
function Navbar() {
return (
<nav>
<A href="/" end={true}>
Home
</A>
<A
href="/login"
activeClass="text-blue-900"
inactiveClass="text-blue-500"
>
Login
</A>
</nav>
);
}
```

### Matching routes
## `useNavigate` primitive

The `useNavigate` primitive allows for programmatically navigating to a specified route.

By default, matching using the `<A>` component will include locations that are _descendants_ of the specified route.
This would include `/users`, `/users/1`, `/users/1/posts`, etc. if `/users` is the specified route.
```tsx
import { useNavigate } from "@solidjs/router";

```jsx
// Will match /users and /users/1
<A href="/users">
Users
</A>
function LoginPage() {
const navigate = useNavigate();

return (
<button
onClick={() => {
// Perform login logic here ...
navigate("/dashboard", { replace: true });
}}
>
Login
</button>
);
Comment thread
amirhhashemi marked this conversation as resolved.
}
```
To match the exact route only, you can use the `end` prop:

```jsx
// Will match /users/1 only
<A href="/users/1" end>
User 1
</A>
This example redirects the user to `/dashboard` after login.
The `replace: true` option effectively removes the login page from the browser's history stack by replacing it with the `/dashboard` route, preventing the user from returning to it with the back button.
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

<Callout type="info" title="Note">

`useNavigate` can only be used within a component that is rendered by a `<Route>`.

</Callout>
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

See the [`useNavigate` API reference](/solid-router/reference/primitives/use-navigate) for more information.

## `redirect` function

The `redirect` function returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that allows navigation to a specified route within a [query](/solid-router/reference/data-apis/query) or [action](/solid-router/reference/data-apis/action).
Comment thread
amirhhashemi marked this conversation as resolved.
Outdated

```tsx
import { action, redirect } from "@solidjs/router";

const logout = action(async () => {
localStorage.remove("token");
throw redirect("/");
});
```

See the [`redirect` API reference](/solid-router/reference/response-helpers/redirect) for more information.