-
Notifications
You must be signed in to change notification settings - Fork 358
Expand Navigation docs #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand Navigation docs #1061
Changes from 3 commits
b151712
a16ba88
e053840
1cf36b1
c059740
ecce078
95c4fed
66269be
ac6cf91
b2ab9ae
7a7127d
b616e7a
209b5c2
46b600c
ce9542f
9a118ed
ffc069e
82911a8
3183c0a
445314c
aeb29e5
e654bab
7bcf0e4
c49accf
93f3aef
f513dcd
25b3341
dcfc501
d28cf79
053da8b
3e492a9
dcfa2ea
eb4c92b
cd379a8
e0b2d76
c0f374e
f8c554c
148d9f7
df2b9e0
070f0d7
ca4f7a7
e5855f4
08550b5
a97979e
47d983e
195025c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ```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. | ||||||||||||||||||||
|
LadyBluenotes marked this conversation as resolved.
Outdated
amirhhashemi marked this conversation as resolved.
Outdated
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| ## `<a>` element | ||||||||||||||||||||
|
|
||||||||||||||||||||
| The standard HTML `<a>` element is the most basic way to create links. | ||||||||||||||||||||
|
amirhhashemi marked this conversation as resolved.
Outdated
|
||||||||||||||||||||
| In SolidStart, clicking an `<a>` element triggers [soft navigation](/solid-router/reference/components/a#soft-navigation). | ||||||||||||||||||||
|
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. | ||||||||||||||||||||
|
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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||
| ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### 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. | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||
|
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`. | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not clear that where |
||||||||||||||||||||
|
|
||||||||||||||||||||
| 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. | ||||||||||||||||||||
|
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> | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
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. | ||||||||||||||||||||
|
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> | ||||||||||||||||||||
|
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). | ||||||||||||||||||||
|
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. | ||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.