Skip to content
Merged
Changes from all 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
139 changes: 90 additions & 49 deletions src/routes/solid-router/concepts/navigation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,109 @@
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.
When using Solid Router, you can use the standard standard HTML [`<a>` elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a), which triggers [soft navigation](/solid-router/reference/components/a#soft-navigation).
In addition to using this, Solid Router offers other options for navigating between routes:

```jsx {4-5}
const App = (props) => (
<>
<nav>
<a href="/users">Users</a>
<a href="/">Home</a>
</nav>
<h1>My Site with lots of pages</h1>
{props.children}
</>
);
```
- The [`<A>` component](/solid-router/reference/components/a).
- The [`useNavigate` primitive](/solid-router/reference/primitives/use-navigate).
- The [`redirect` function](/solid-router/reference/response-helpers/redirect).

## `<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 by automatically applying the base URL path and, additionally, supports relative paths.

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

function DashboardPage() {
return (
<main>
<nav>
<A href="/">Home</a>
</nav>
{/* This is a relative path that, from /dashboard, links to /dashboard/users */}
<A href="users">Users</A>
</main>
);
}
```

### Styling links
See the [`<A>` API reference](/solid-router/reference/components/a) for more information.

### Styling

The `<A>` component allows you to style links based on their active state using the `activeClass` and `inactiveClass` props.
When provided, these props apply the corresponding CSS classes to the link.
If omitted, the default classes `active` and `inactive` are used.

By default, a link is considered active when the current route matches the link's `href` or any of its descendant routes.
For example, a link with `href="/dashboard"` is active when the current route is `/dashboard`, `/dashboard/users`, or `/dashboard/users/1/profile`.

To match an exact route, the prop `end` can be used.
When `true`, it ensures that the link is only considered active if the `href` exactly matches the current route.
This is useful for root route links (href="/"), which might otherwise match all routes.

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={() => {
// Login logic
navigate("/dashboard", { replace: true });
}}
>
Login
</button>
);
}
```
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.
By using `replace: true`, the login page is removed from the browser's history stack and replaced with the `/dashboard` route.
This prevents the user from navigating back to the login page using the back button.

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

## `redirect` function

The `redirect` function returns a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), which enables navigation to a specified route within [query](/solid-router/reference/data-apis/query) or [action](/solid-router/reference/data-apis/action).

```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.
Loading