Skip to content

Commit

Permalink
コンテクスト周りを整理
Browse files Browse the repository at this point in the history
  • Loading branch information
tasuren committed Jan 12, 2025
1 parent 5055043 commit 2c1c15e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
- Bun
- SolidJS
- Tailwind CSS
[Preflight](https://tailwindcss.com/docs/preflight)が無効化されています。
開発時にはこれを留意してください。
[Preflight](https://tailwindcss.com/docs/preflight)が無効化されています。
開発時にはこれを留意してください。
- GitHub Pages

## 開発
Expand Down
9 changes: 3 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import Layout from "@/components/Layout";
import "./App.css";
import { LibraryProvider } from "@/components/Context";
import MainContent from "@/components/MainContent";

function App() {
return (
<LibraryProvider>
<Layout>
<MainContent />
</Layout>
</LibraryProvider>
<Layout>
<MainContent />
</Layout>
);
}

Expand Down
72 changes: 2 additions & 70 deletions src/components/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
import { Paper } from "@/lib/paper";
import {
type Theme,
applyThemeToDOM,
readTheme,
writeTheme,
} from "@/lib/theme";
import { TTS } from "@/lib/voice";
import {
type ParentProps,
createContext,
createEffect,
createSignal,
useContext,
} from "solid-js";

// テーマのコンテクスト
const ThemeContext = createContext<[() => Theme, (theme: Theme) => void]>();

function ThemeProvider(props: ParentProps) {
const [theme, setTheme] = createSignal<Theme>(readTheme());

createEffect(() => {
const current = theme();
writeTheme(current);
applyThemeToDOM(current);
});

return (
<ThemeContext.Provider value={[theme, setTheme]}>
{props.children}
</ThemeContext.Provider>
);
}

export const useTheme = () => {
const value = useContext(ThemeContext);

if (!value)
throw new Error(
"テーマのコンテキストプロバイダーが使われていないようです。",
);

return value;
};

// VoiceControllerのコンテクスト
const tts = new TTS();
const TTSContext = createContext(tts);

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

function PaperProvider(props: ParentProps) {
export function PaperProvider(props: ParentProps) {
const [paper, setPaper] = createSignal<Paper>();

return (
Expand All @@ -86,34 +49,3 @@ export function usePaper(): [() => Paper, (raw: HTMLElement) => void] {
(raw) => setPaper(new Paper(raw)),
];
}

// 選択した文章に関するコンテクスト
const SelectedContentsContext =
createContext<[() => string | undefined, (contents: string) => void]>();

function SelectedContentsProvider(props: ParentProps) {
const [contents, setContents] = createSignal<string>();

return (
<SelectedContentsContext.Provider value={[contents, setContents]}>
{props.children}
</SelectedContentsContext.Provider>
);
}

export function useSelectedContents() {
return useContext(SelectedContentsContext);
}

// 全てのコンテクストプロバイダーを適用するコンポーネント
export function LibraryProvider(props: ParentProps) {
return (
<SelectedContentsProvider>
<PaperProvider>
<ThemeProvider>
<VoiceProvider>{props.children}</VoiceProvider>
</ThemeProvider>
</PaperProvider>
</SelectedContentsProvider>
);
}
27 changes: 15 additions & 12 deletions src/components/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MainController from "@/components/Controller/MainController";
import QuickController from "@/components/Controller/QuickController";
import Paper from "@/components/Paper";
import { type ParentProps, Show, createSignal } from "solid-js";
import { PaperProvider, TTSProvider } from "./Context";
import Footer from "./Footer";

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

function MainContent() {
return (
<>
<main class="flex-1 flex flex-col justify-between">
<div class="space-y-4">
<MainController />
<Paper />
</div>
<PaperProvider>
<TTSProvider>
<main class="flex-1 flex flex-col justify-between">
<div class="space-y-4">
<MainController />
<Paper />
</div>

<Footer />
</main>
<Footer />
</main>

<OnlyBigMonitor>
<QuickController />
</OnlyBigMonitor>
</>
<OnlyBigMonitor>
<QuickController />
</OnlyBigMonitor>
</TTSProvider>
</PaperProvider>
);
}

Expand Down
13 changes: 11 additions & 2 deletions src/components/ThemeToggleButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useTheme } from "./Context";
import { applyThemeToDOM, writeTheme } from "@/lib/theme";
import { readTheme } from "@/lib/theme";
import { createEffect, createSignal } from "solid-js";

const THEME_TITLES = {
dark: "ダーク",
Expand All @@ -7,7 +9,14 @@ const THEME_TITLES = {
};

function ThemeToggleButton(props: { class: string }) {
const [theme, setTheme] = useTheme();
const [theme, setTheme] = createSignal(readTheme());

createEffect(() => {
const current = theme();

writeTheme(current);
applyThemeToDOM(current);
});

const toggleTheme = () => {
const current = theme();
Expand Down

0 comments on commit 2c1c15e

Please sign in to comment.