File tree Expand file tree Collapse file tree 2 files changed +73
-1
lines changed Expand file tree Collapse file tree 2 files changed +73
-1
lines changed Original file line number Diff line number Diff line change
1
+ 'use client' ;
2
+
3
+ import Link from 'next/link' ;
4
+ import { useSelectedLayoutSegments } from 'next/navigation' ;
5
+ import { useMemo } from 'react' ;
6
+
7
+ export default function BreadCrumbs ( ) {
8
+ const segments = useSelectedLayoutSegments ( ) ;
9
+
10
+ const crumbs = useMemo ( ( ) => {
11
+ let curPath = '/' ;
12
+
13
+ return [
14
+ {
15
+ name : 'Home' ,
16
+ href : curPath ,
17
+ current : segments . length === 0 ,
18
+ } ,
19
+ ...segments
20
+ . filter (
21
+ ( segment ) => ! segment . startsWith ( '(' ) && ! segment . startsWith ( '[' )
22
+ )
23
+ . map ( ( segment , index ) => {
24
+ curPath += segment ;
25
+
26
+ return {
27
+ name : segment ,
28
+ href : curPath ,
29
+ current : index === segments . length - 1 ,
30
+ } ;
31
+ } ) ,
32
+ ] ;
33
+ } , [ segments ] ) ;
34
+
35
+ console . log ( segments ) ;
36
+
37
+ return (
38
+ < nav className = "flex mb-8 justify-end" aria-label = "Breadcrumb" >
39
+ < ol role = "list" className = "flex items-center space-x-4" >
40
+ { crumbs . map ( ( page , index ) => (
41
+ < li key = { page . name } >
42
+ < div className = "flex items-center" >
43
+ { index !== 0 && (
44
+ < svg
45
+ className = "h-5 w-5 flex-shrink-0 text-gray-300"
46
+ fill = "currentColor"
47
+ viewBox = "0 0 20 20"
48
+ aria-hidden = "true"
49
+ >
50
+ < path d = "M5.555 17.776l8-16 .894.448-8 16-.894-.448z" />
51
+ </ svg >
52
+ ) }
53
+ < Link
54
+ href = { page . href }
55
+ className = { `ml-4 text-sm font-medium ${
56
+ page . current
57
+ ? 'text-sky-700 hover:text-sky-800 drop-shadow-sm'
58
+ : 'text-gray-500 hover:text-gray-700'
59
+ } `}
60
+ aria-current = { page . current ? 'page' : undefined }
61
+ >
62
+ { page . name }
63
+ </ Link >
64
+ </ div >
65
+ </ li >
66
+ ) ) }
67
+ </ ol >
68
+ </ nav >
69
+ ) ;
70
+ }
Original file line number Diff line number Diff line change 1
1
import Link from 'next/link' ;
2
2
import './globals.css' ;
3
3
import { Inter } from 'next/font/google' ;
4
+ import BreadCrumbs from './breadcrumbs' ;
4
5
5
6
const inter = Inter ( { subsets : [ 'latin' ] } ) ;
6
7
@@ -21,7 +22,7 @@ export default function RootLayout({
21
22
< html lang = "en" >
22
23
< body className = { inter . className } >
23
24
< div className = "container p-4 mx-auto" >
24
- < header className = "md:flex md:items-center md:justify-between mb-8 " >
25
+ < header className = "md:flex md:items-center md:justify-between mb-2 " >
25
26
< div className = "min-w-0 flex-1" >
26
27
< h1 className = "text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-3xl sm:tracking-tight" >
27
28
Next.js App Router Example
@@ -31,6 +32,7 @@ export default function RootLayout({
31
32
{ new Date ( ) . toLocaleTimeString ( ) }
32
33
</ div >
33
34
</ header >
35
+ < BreadCrumbs />
34
36
{ children }
35
37
< footer className = "mt-8 border-t border-t-sky-500 pt-8" >
36
38
< nav className = "prose" >
You can’t perform that action at this time.
0 commit comments