mirror of
https://github.com/michivonah/nextjs.git
synced 2025-12-22 22:16:28 +01:00
dashboard layout + implement page navigation + docs
This commit is contained in:
parent
aa734325b4
commit
aea677e6f7
6 changed files with 116 additions and 3 deletions
78
README.md
78
README.md
|
|
@ -238,6 +238,84 @@ export default function Page() {
|
||||||
|
|
||||||
The `width` and `height` should be specified to prevent layout shift - the values set are just used for defining the right aspect ratio - Next.js decides by itself which size has to be rendered.
|
The `width` and `height` should be specified to prevent layout shift - the values set are just used for defining the right aspect ratio - Next.js decides by itself which size has to be rendered.
|
||||||
|
|
||||||
|
## Layouts
|
||||||
|
In a `layout.tsx` file the layout of a page with all their subpages is defined. The files has to return a Layout component on the default function. The children should be imported and shown on the layout, else they won't be visible anywhere.
|
||||||
|
|
||||||
|
Example `layout.tsx`
|
||||||
|
```tsx
|
||||||
|
import SideNav from "@/app/ui/dashboard/sidenav";
|
||||||
|
|
||||||
|
export default function Layout({children}: {children: React.ReactNode}){
|
||||||
|
return (
|
||||||
|
<div className="flex h-screen flex-col md:flex-row md:overflow-hidden">
|
||||||
|
<div className="w-full flex-none md:w-64">
|
||||||
|
<SideNav />
|
||||||
|
</div>
|
||||||
|
<div className="flex-grow p-6 md:overflow-y-auto md:p-12">
|
||||||
|
{ children }
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
By using the `<Link />` component instead of the HTML tag `<a>` you can to client-side-navigation without a full page reload.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
export default function NavLinks() {
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
key={link.name}
|
||||||
|
href={link.href}
|
||||||
|
className="flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3"
|
||||||
|
>
|
||||||
|
<LinkIcon className="w-6" />
|
||||||
|
<p className="hidden md:block">{link.name}</p>
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Next.js prefetches the content of the pages linked via `<Link />` so that it can a switch between the pages is faster respectly nearly instant.
|
||||||
|
|
||||||
|
To show the currently active link the React hook `usePathname` can be used. To use conditons for changing the css classes you can use `clsx`.
|
||||||
|
Example:
|
||||||
|
```tsx
|
||||||
|
import Link from 'next/link';
|
||||||
|
import {usePathname} from 'next/navigation';
|
||||||
|
import clsx from 'clsx';
|
||||||
|
|
||||||
|
export default function NavLinks() {
|
||||||
|
const pathname = usePathname();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{links.map((link) => {
|
||||||
|
const LinkIcon = link.icon;
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
key={link.name}
|
||||||
|
href={link.href}
|
||||||
|
className={clsx(
|
||||||
|
"flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3",
|
||||||
|
{
|
||||||
|
'bg-sky-100 text-blue-600': pathname === link.href,
|
||||||
|
},
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<LinkIcon className="w-6" />
|
||||||
|
<p className="hidden md:block">{link.name}</p>
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Ressources
|
## Ressources
|
||||||
- [Next.js Installation](https://nextjs.org/docs/app/getting-started/installation)
|
- [Next.js Installation](https://nextjs.org/docs/app/getting-started/installation)
|
||||||
- [Next.js React Foundations Course](https://nextjs.org/learn/react-foundations)
|
- [Next.js React Foundations Course](https://nextjs.org/learn/react-foundations)
|
||||||
|
|
|
||||||
3
dashboard-app-course/app/dashboard/customers/page.tsx
Normal file
3
dashboard-app-course/app/dashboard/customers/page.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export default function Page(){
|
||||||
|
return <p>Customers Page</p>
|
||||||
|
}
|
||||||
3
dashboard-app-course/app/dashboard/invoices/page.tsx
Normal file
3
dashboard-app-course/app/dashboard/invoices/page.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export default function Page(){
|
||||||
|
return <p>Invoices Page</p>;
|
||||||
|
}
|
||||||
14
dashboard-app-course/app/dashboard/layout.tsx
Normal file
14
dashboard-app-course/app/dashboard/layout.tsx
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import SideNav from "@/app/ui/dashboard/sidenav";
|
||||||
|
|
||||||
|
export default function Layout({children}: {children: React.ReactNode}){
|
||||||
|
return (
|
||||||
|
<div className="flex h-screen flex-col md:flex-row md:overflow-hidden">
|
||||||
|
<div className="w-full flex-none md:w-64">
|
||||||
|
<SideNav />
|
||||||
|
</div>
|
||||||
|
<div className="flex-grow p-6 md:overflow-y-auto md:p-12">
|
||||||
|
{ children }
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
3
dashboard-app-course/app/dashboard/page.tsx
Normal file
3
dashboard-app-course/app/dashboard/page.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export default function Page(){
|
||||||
|
return <p>Dashboard page</p>;
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
UserGroupIcon,
|
UserGroupIcon,
|
||||||
HomeIcon,
|
HomeIcon,
|
||||||
DocumentDuplicateIcon,
|
DocumentDuplicateIcon,
|
||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import {usePathname} from 'next/navigation';
|
||||||
|
import clsx from 'clsx';
|
||||||
|
|
||||||
// Map of links to display in the side navigation.
|
// Map of links to display in the side navigation.
|
||||||
// Depending on the size of the application, this would be stored in a database.
|
// Depending on the size of the application, this would be stored in a database.
|
||||||
|
|
@ -17,19 +22,26 @@ const links = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export default function NavLinks() {
|
export default function NavLinks() {
|
||||||
|
const pathname = usePathname();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{links.map((link) => {
|
{links.map((link) => {
|
||||||
const LinkIcon = link.icon;
|
const LinkIcon = link.icon;
|
||||||
return (
|
return (
|
||||||
<a
|
<Link
|
||||||
key={link.name}
|
key={link.name}
|
||||||
href={link.href}
|
href={link.href}
|
||||||
className="flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3"
|
className={clsx(
|
||||||
|
"flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3",
|
||||||
|
{
|
||||||
|
'bg-sky-100 text-blue-600': pathname === link.href,
|
||||||
|
},
|
||||||
|
)}
|
||||||
>
|
>
|
||||||
<LinkIcon className="w-6" />
|
<LinkIcon className="w-6" />
|
||||||
<p className="hidden md:block">{link.name}</p>
|
<p className="hidden md:block">{link.name}</p>
|
||||||
</a>
|
</Link>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</>
|
</>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue