Skip to content

Commit 3ad9653

Browse files
committed
navbar, sidebar, markdownの読み込み
1 parent ab2bfd9 commit 3ad9653

File tree

8 files changed

+1354
-70
lines changed

8 files changed

+1354
-70
lines changed

app/[docs_id]/page.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { notFound } from "next/navigation";
2+
import { readFile } from "node:fs/promises";
3+
import { join } from "node:path";
4+
import Markdown, { Components } from "react-markdown";
5+
6+
export default async function Page({
7+
params,
8+
}: {
9+
params: Promise<{ docs_id: string }>;
10+
}) {
11+
const { docs_id } = await params;
12+
13+
let mdContent: string;
14+
try {
15+
mdContent = await readFile(
16+
join(process.cwd(), "docs", `${docs_id}.md`),
17+
"utf-8",
18+
);
19+
} catch (error) {
20+
console.error(error);
21+
notFound();
22+
}
23+
24+
return (
25+
<div className="p-4">
26+
<Markdown components={components}>{mdContent}</Markdown>
27+
</div>
28+
);
29+
}
30+
31+
// TailwindCSSがh1などのタグのスタイルを消してしまうので、手動でスタイルを指定する必要がある
32+
const components: Components = {
33+
h1: ({ node, ...props }) => (
34+
<h1 className="text-2xl font-bold my-4" {...props} />
35+
),
36+
h2: ({ node, ...props }) => (
37+
<h2 className="text-xl font-semibold my-3" {...props} />
38+
),
39+
h3: ({ node, ...props }) => (
40+
<h3 className="text-lg font-medium my-2" {...props} />
41+
),
42+
p: ({ node, ...props }) => <p className="my-2" {...props} />,
43+
};

app/layout.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Metadata } from "next";
22
import "./globals.css";
3+
import { Navbar } from "./navbar";
4+
import { Sidebar } from "./sidebar";
5+
import { ReactNode } from "react";
36

47
export const metadata: Metadata = {
58
title: "Create Next App",
@@ -8,12 +11,26 @@ export const metadata: Metadata = {
811

912
export default function RootLayout({
1013
children,
11-
}: Readonly<{
12-
children: React.ReactNode;
13-
}>) {
14+
}: Readonly<{ children: ReactNode }>) {
1415
return (
1516
<html lang="ja">
16-
<body>{children}</body>
17+
<body className="w-screen h-screen">
18+
<div className="drawer lg:drawer-open">
19+
<input id="drawer-toggle" type="checkbox" className="drawer-toggle" />
20+
<div className="drawer-content flex flex-col">
21+
<Navbar />
22+
{children}
23+
</div>
24+
<div className="drawer-side shadow-md">
25+
<label
26+
htmlFor="drawer-toggle"
27+
aria-label="close sidebar"
28+
className="drawer-overlay"
29+
/>
30+
<Sidebar />
31+
</div>
32+
</div>
33+
</body>
1734
</html>
1835
);
1936
}

app/navbar.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function Navbar() {
2+
return (
3+
<div className="navbar bg-base-200 w-full">
4+
<div className="flex-none lg:hidden">
5+
{/* サイドバーを開閉するボタン */}
6+
<label
7+
htmlFor="drawer-toggle"
8+
aria-label="open sidebar"
9+
className="btn btn-square btn-ghost"
10+
>
11+
<svg
12+
xmlns="http://www.w3.org/2000/svg"
13+
fill="none"
14+
viewBox="0 0 24 24"
15+
className="inline-block h-6 w-6 stroke-current"
16+
>
17+
<path
18+
strokeLinecap="round"
19+
strokeLinejoin="round"
20+
strokeWidth="2"
21+
d="M4 6h16M4 12h16M4 18h16"
22+
/>
23+
</svg>
24+
</label>
25+
</div>
26+
<div className="mx-2 flex-1 px-2 font-bold text-xl lg:hidden">
27+
{/* タイトル(サイドバー非表示の場合のみ) */}
28+
Navbar Title
29+
</div>
30+
</div>
31+
);
32+
}

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function Home() {
2-
return <div className="">Hello, world</div>;
2+
return <div className="p-4">This is root page</div>;
33
}

app/sidebar.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Link from "next/link";
2+
3+
export function Sidebar() {
4+
return (
5+
<div className="bg-base-200 min-h-full w-80 p-4">
6+
{/* todo: 背景色ほんとにこれでいい? */}
7+
<h2 className="hidden lg:block text-xl font-bold mb-4">
8+
{/* サイドバーが常時表示されている場合のみ */}
9+
Navbar Title
10+
</h2>
11+
<ul className="menu w-full">
12+
<li>
13+
<Link href="/1">1</Link>
14+
</li>
15+
</ul>
16+
</div>
17+
);
18+
}

docs/1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## sample markdown file
2+
3+
hello, world!

0 commit comments

Comments
 (0)