Skip to content
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

docs: add link and navigation page content #211

Merged
merged 6 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { Code } from '@mantine/core'
export default function MdxCode(
props: HTMLAttributes<HTMLPreElement>,
): JSX.Element {
return <Code {...props} style={{ fontSize: 14 }} />
return <Code {...props} style={{ fontSize: 'inherit' }} />
}
10 changes: 5 additions & 5 deletions apps/documentation/src/components/sidebar/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export const sidebarElements: Array<SidebarElement> = [
label: 'Defining routes',
href: '/documentation/routing/defining-routes',
},
{
type: 'element',
label: 'Link and navigation',
href: '/documentation/routing/link-and-navigation',
},
{
type: 'element',
label: 'Pages',
Expand All @@ -131,11 +136,6 @@ export const sidebarElements: Array<SidebarElement> = [
label: 'Layouts',
href: '/documentation/routing/layouts',
},
{
type: 'element',
label: 'Link and navigation',
href: '/documentation/routing/link-and-navigation',
},
{
type: 'element',
label: 'Redirecting',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,65 @@ import Breadcrumbs, { Element } from '@/components/breadcrumbs'

# Link and navigation

Todo
The Tuono router facilitates client-side route transitions between pages, enhancing the user experience by:

- Enabling smooth navigation in single-page applications (SPAs)
- Preventing page reloads during site navigation

How? Using the `Link` component provided from Tuono exports:

```tsx
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
import { Link } from 'tuono'
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

export default function HomePage() {
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
return (
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
<ul>
<li>
<Link href="/">Home</Link>
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
</li>
<li>
<Link href="/about">About Us</Link>
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
</li>
<li>
<Link href="/blog/hello-world">Blog Post</Link>
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
</li>
</ul>
)
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
}
```

The example above uses multiple links. Each one maps a path (`href`) to a known route:
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

- `/` → `src/routes/index.tsx`
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
- `/about` → `src/routes/about.tsx`
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
- `/blog/hello-world` → `src/routes/blog/[slug].tsx`
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

> Additional considerations:
>
> - Any `<Link />` in the viewport is prefetched by default when it appears within the viewport.
> - The corresponding data for server-rendered routes is fetched only when the `<Link />` is clicked.

## The `useRouter` hook
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

The Link component is not suitable for programmatic navigation.
To handle this, the library provides the `useRouter` hook.
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

For example, after a user submits a form and the API request succeeds, they can be redirected to another page.
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

```tsx
import { useRouter } from 'tuono'
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

export default function GoToAboutPage() {
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
const router = useRouter()
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved

return (
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
marcalexiei marked this conversation as resolved.
Show resolved Hide resolved
<ProfileForm
onUpdateSuccess={() => {
router.push('/profile')
}}
/>
)
}
```

> For more information about this hook visit the [dedicated API reference page](/documentation/hooks/use-router).
Loading