Skip to content

Commit 2c1c15e

Browse files
committed
コンテクスト周りを整理
1 parent 5055043 commit 2c1c15e

File tree

5 files changed

+33
-92
lines changed

5 files changed

+33
-92
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
- Bun
1414
- SolidJS
1515
- Tailwind CSS
16-
[Preflight](https://tailwindcss.com/docs/preflight)が無効化されています。
17-
開発時にはこれを留意してください。
16+
[Preflight](https://tailwindcss.com/docs/preflight)が無効化されています。
17+
開発時にはこれを留意してください。
1818
- GitHub Pages
1919

2020
## 開発

src/App.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import Layout from "@/components/Layout";
22
import "./App.css";
3-
import { LibraryProvider } from "@/components/Context";
43
import MainContent from "@/components/MainContent";
54

65
function App() {
76
return (
8-
<LibraryProvider>
9-
<Layout>
10-
<MainContent />
11-
</Layout>
12-
</LibraryProvider>
7+
<Layout>
8+
<MainContent />
9+
</Layout>
1310
);
1411
}
1512

src/components/Context.tsx

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,17 @@
11
import { Paper } from "@/lib/paper";
2-
import {
3-
type Theme,
4-
applyThemeToDOM,
5-
readTheme,
6-
writeTheme,
7-
} from "@/lib/theme";
82
import { TTS } from "@/lib/voice";
93
import {
104
type ParentProps,
115
createContext,
12-
createEffect,
136
createSignal,
147
useContext,
158
} from "solid-js";
169

17-
// テーマのコンテクスト
18-
const ThemeContext = createContext<[() => Theme, (theme: Theme) => void]>();
19-
20-
function ThemeProvider(props: ParentProps) {
21-
const [theme, setTheme] = createSignal<Theme>(readTheme());
22-
23-
createEffect(() => {
24-
const current = theme();
25-
writeTheme(current);
26-
applyThemeToDOM(current);
27-
});
28-
29-
return (
30-
<ThemeContext.Provider value={[theme, setTheme]}>
31-
{props.children}
32-
</ThemeContext.Provider>
33-
);
34-
}
35-
36-
export const useTheme = () => {
37-
const value = useContext(ThemeContext);
38-
39-
if (!value)
40-
throw new Error(
41-
"テーマのコンテキストプロバイダーが使われていないようです。",
42-
);
43-
44-
return value;
45-
};
46-
4710
// VoiceControllerのコンテクスト
4811
const tts = new TTS();
4912
const TTSContext = createContext(tts);
5013

51-
function VoiceProvider(props: ParentProps) {
14+
export function TTSProvider(props: ParentProps) {
5215
return (
5316
<TTSContext.Provider value={tts}>{props.children}</TTSContext.Provider>
5417
);
@@ -62,7 +25,7 @@ export function useTTS(): TTS {
6225
const PaperContext =
6326
createContext<[() => Paper | undefined, (paper: Paper) => void]>();
6427

65-
function PaperProvider(props: ParentProps) {
28+
export function PaperProvider(props: ParentProps) {
6629
const [paper, setPaper] = createSignal<Paper>();
6730

6831
return (
@@ -86,34 +49,3 @@ export function usePaper(): [() => Paper, (raw: HTMLElement) => void] {
8649
(raw) => setPaper(new Paper(raw)),
8750
];
8851
}
89-
90-
// 選択した文章に関するコンテクスト
91-
const SelectedContentsContext =
92-
createContext<[() => string | undefined, (contents: string) => void]>();
93-
94-
function SelectedContentsProvider(props: ParentProps) {
95-
const [contents, setContents] = createSignal<string>();
96-
97-
return (
98-
<SelectedContentsContext.Provider value={[contents, setContents]}>
99-
{props.children}
100-
</SelectedContentsContext.Provider>
101-
);
102-
}
103-
104-
export function useSelectedContents() {
105-
return useContext(SelectedContentsContext);
106-
}
107-
108-
// 全てのコンテクストプロバイダーを適用するコンポーネント
109-
export function LibraryProvider(props: ParentProps) {
110-
return (
111-
<SelectedContentsProvider>
112-
<PaperProvider>
113-
<ThemeProvider>
114-
<VoiceProvider>{props.children}</VoiceProvider>
115-
</ThemeProvider>
116-
</PaperProvider>
117-
</SelectedContentsProvider>
118-
);
119-
}

src/components/MainContent.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MainController from "@/components/Controller/MainController";
22
import QuickController from "@/components/Controller/QuickController";
33
import Paper from "@/components/Paper";
44
import { type ParentProps, Show, createSignal } from "solid-js";
5+
import { PaperProvider, TTSProvider } from "./Context";
56
import Footer from "./Footer";
67

78
/** 大きいモニターの場合しか表示しないコンポーネント */
@@ -18,20 +19,22 @@ function OnlyBigMonitor(props: ParentProps) {
1819

1920
function MainContent() {
2021
return (
21-
<>
22-
<main class="flex-1 flex flex-col justify-between">
23-
<div class="space-y-4">
24-
<MainController />
25-
<Paper />
26-
</div>
22+
<PaperProvider>
23+
<TTSProvider>
24+
<main class="flex-1 flex flex-col justify-between">
25+
<div class="space-y-4">
26+
<MainController />
27+
<Paper />
28+
</div>
2729

28-
<Footer />
29-
</main>
30+
<Footer />
31+
</main>
3032

31-
<OnlyBigMonitor>
32-
<QuickController />
33-
</OnlyBigMonitor>
34-
</>
33+
<OnlyBigMonitor>
34+
<QuickController />
35+
</OnlyBigMonitor>
36+
</TTSProvider>
37+
</PaperProvider>
3538
);
3639
}
3740

src/components/ThemeToggleButton.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { useTheme } from "./Context";
1+
import { applyThemeToDOM, writeTheme } from "@/lib/theme";
2+
import { readTheme } from "@/lib/theme";
3+
import { createEffect, createSignal } from "solid-js";
24

35
const THEME_TITLES = {
46
dark: "ダーク",
@@ -7,7 +9,14 @@ const THEME_TITLES = {
79
};
810

911
function ThemeToggleButton(props: { class: string }) {
10-
const [theme, setTheme] = useTheme();
12+
const [theme, setTheme] = createSignal(readTheme());
13+
14+
createEffect(() => {
15+
const current = theme();
16+
17+
writeTheme(current);
18+
applyThemeToDOM(current);
19+
});
1120

1221
const toggleTheme = () => {
1322
const current = theme();

0 commit comments

Comments
 (0)