Skip to content

Commit b73dabe

Browse files
committed
ユーザ指定のテーマ・画面との連動
1 parent 48ad6bb commit b73dabe

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

app/[docs_id]/themeToggle.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import { useState, useEffect } from "react";
2+
import { useState, useEffect} from "react";
33

44
export function useChangeTheme(){
55
const [theme, setTheme] = useState("tomorrow");
@@ -23,16 +23,52 @@ export function useChangeTheme(){
2323

2424
};
2525
export function ThemeToggle() {
26+
const [isChecked, setIsChecked] = useState(false);
27+
useEffect(() => {
28+
const checkIsDarkSchemePreferred = () =>
29+
window?.matchMedia?.('(prefers-color-scheme:dark)')?.matches ?? false;
30+
setIsChecked(checkIsDarkSchemePreferred());
31+
}, []);
32+
2633
return (
34+
<label className="flex cursor-pointer gap-2" style={{ marginLeft: "1em" }}>
35+
<svg
36+
xmlns="http://www.w3.org/2000/svg"
37+
width="20"
38+
height="20"
39+
viewBox="0 0 24 24"
40+
fill="none"
41+
stroke="currentColor"
42+
strokeWidth="2"
43+
strokeLinecap="round"
44+
strokeLinejoin="round">
45+
<circle cx="12" cy="12" r="5" />
46+
<path
47+
d="M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4" />
48+
</svg>
2749
<input
2850
type="checkbox"
51+
checked={isChecked}
2952
className="toggle theme-controller"
30-
style={{ marginLeft: "1em" }}
3153
onChange={(e) => {
32-
const isDark = e.target.checked;
33-
const theme = isDark ? "dark" : "light";
54+
const isdark = e.target.checked;
55+
setIsChecked(isdark);
56+
const theme = isdark ? "dark" : "light";
3457
document.documentElement.setAttribute("data-theme", theme);
3558
}}
3659
/>
60+
<svg
61+
xmlns="http://www.w3.org/2000/svg"
62+
width="20"
63+
height="20"
64+
viewBox="0 0 24 24"
65+
fill="none"
66+
stroke="currentColor"
67+
strokeWidth="2"
68+
strokeLinecap="round"
69+
strokeLinejoin="round">
70+
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
71+
</svg>
72+
</label>
3773
);
3874
}

app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function RootLayout({
1818
children,
1919
}: Readonly<{ children: ReactNode }>) {
2020
return (
21-
<html lang="ja" data-theme="light">
21+
<html lang="ja">
2222
<body className="w-screen h-screen">
2323
<div className="drawer lg:drawer-open">
2424
<input id="drawer-toggle" type="checkbox" className="drawer-toggle" />

app/navbar.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ThemeToggle } from "./[docs_id]/themeToggle";
12
export function Navbar() {
23
return (
34
<div className="navbar bg-base-200 w-full">
@@ -23,9 +24,10 @@ export function Navbar() {
2324
</svg>
2425
</label>
2526
</div>
26-
<div className="mx-2 flex-1 px-2 font-bold text-xl lg:hidden">
27-
{/* タイトル(サイドバー非表示の場合のみ) */}
28-
Navbar Title
27+
<div className="mx-2 flex flex-row items-center px-2 font-bold text-xl lg:hidden">
28+
{/* サイドバーが常時表示されている場合のみ */}
29+
<span className="flex-1">Navbar Title</span>
30+
<ThemeToggle />
2931
</div>
3032
</div>
3133
);

app/sidebar.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ export function Sidebar() {
2020
return (
2121
<div className="bg-base-200 h-full w-80 overflow-y-auto">
2222
{/* todo: 背景色ほんとにこれでいい? */}
23-
<h2 className="hidden lg:block text-xl font-bold p-4">
23+
<h2 className="hidden lg:flex flex-row items-center text-xl font-bold p-4">
2424
{/* サイドバーが常時表示されている場合のみ */}
25-
Navbar Title
25+
<span className="flex-1">Navbar Title</span>
2626
<ThemeToggle />
2727
</h2>
2828

29+
30+
2931
<ul className="menu w-full">
3032
{pagesList.map((group) => (
3133
<li key={group.id}>

app/terminal/terminal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use client";
22

3-
import { use, useEffect, useRef, useState } from "react";
3+
import { useEffect, useRef, useState } from "react";
44
import { Terminal } from "@xterm/xterm";
55
import { FitAddon } from "@xterm/addon-fit";
66
import "@xterm/xterm/css/xterm.css";
77
import chalk from "chalk";
88
import { useChangeTheme } from "../[docs_id]/themeToggle";
9-
import { th } from "zod/locales";
9+
1010
/**
1111
* 文字列の幅を計算する。
1212
* 厳密にやるなら @xterm/xterm/src/common/input/UnicodeV6.ts を使うとよさそう

0 commit comments

Comments
 (0)