diff --git a/README.md b/README.md index 0f33dd0..1c2f5cc 100644 --- a/README.md +++ b/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. +## 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 ( +
{link.name}
+ + ); +} +``` + +Next.js prefetches the content of the pages linked via `` 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.name}
+ + ); + })} + > + ); +} +``` + ## Ressources - [Next.js Installation](https://nextjs.org/docs/app/getting-started/installation) - [Next.js React Foundations Course](https://nextjs.org/learn/react-foundations) diff --git a/dashboard-app-course/app/dashboard/customers/page.tsx b/dashboard-app-course/app/dashboard/customers/page.tsx new file mode 100644 index 0000000..b790196 --- /dev/null +++ b/dashboard-app-course/app/dashboard/customers/page.tsx @@ -0,0 +1,3 @@ +export default function Page(){ + returnCustomers Page
+} \ No newline at end of file diff --git a/dashboard-app-course/app/dashboard/invoices/page.tsx b/dashboard-app-course/app/dashboard/invoices/page.tsx new file mode 100644 index 0000000..8d059e7 --- /dev/null +++ b/dashboard-app-course/app/dashboard/invoices/page.tsx @@ -0,0 +1,3 @@ +export default function Page(){ + returnInvoices Page
; +} \ No newline at end of file diff --git a/dashboard-app-course/app/dashboard/layout.tsx b/dashboard-app-course/app/dashboard/layout.tsx new file mode 100644 index 0000000..be4b664 --- /dev/null +++ b/dashboard-app-course/app/dashboard/layout.tsx @@ -0,0 +1,14 @@ +import SideNav from "@/app/ui/dashboard/sidenav"; + +export default function Layout({children}: {children: React.ReactNode}){ + return ( +Dashboard page
; +} \ No newline at end of file diff --git a/dashboard-app-course/app/ui/dashboard/nav-links.tsx b/dashboard-app-course/app/ui/dashboard/nav-links.tsx index 72fa462..328dfd8 100644 --- a/dashboard-app-course/app/ui/dashboard/nav-links.tsx +++ b/dashboard-app-course/app/ui/dashboard/nav-links.tsx @@ -1,8 +1,13 @@ +'use client'; + import { UserGroupIcon, HomeIcon, DocumentDuplicateIcon, } 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. // Depending on the size of the application, this would be stored in a database. @@ -17,19 +22,26 @@ const links = [ ]; export default function NavLinks() { + const pathname = usePathname(); + return ( <> {links.map((link) => { const LinkIcon = link.icon; return ( -{link.name}
- + ); })} >