Add ThemeProvider.
This commit is contained in:
parent
38e9f96732
commit
22336e1f31
@ -1,17 +1,16 @@
|
|||||||
import React, {useState, useEffect} from 'react';
|
import React, {useState, useEffect} from 'react';
|
||||||
import parse from 'html-react-parser'
|
import parse from 'html-react-parser'
|
||||||
|
import { useTheme } from '../ThemeContext';
|
||||||
|
|
||||||
// import ScrollSpy from '../UI/ScrollSpy';
|
// import ScrollSpy from '../UI/ScrollSpy';
|
||||||
|
|
||||||
const BasicPage = (props) => {
|
const BasicPage = (props) => {
|
||||||
const [theme, setTheme] = useState(
|
|
||||||
localStorage.getItem('theme') || 'light'
|
|
||||||
);
|
|
||||||
|
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
const [isLoaded, setIsLoaded] = useState(false);
|
const [isLoaded, setIsLoaded] = useState(false);
|
||||||
const [items, setItems] = useState([]);
|
const [items, setItems] = useState([]);
|
||||||
|
|
||||||
|
const { theme } = useTheme();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("http://localhost:5218/page/" + props.id)
|
fetch("http://localhost:5218/page/" + props.id)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
|
|||||||
20
src/ThemeContext.js
Normal file
20
src/ThemeContext.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import React, { createContext, useContext, useState } from 'react';
|
||||||
|
|
||||||
|
const ThemeContext = createContext();
|
||||||
|
|
||||||
|
export function ThemeProvider({ children }) {
|
||||||
|
const [theme, setTheme] = useState(
|
||||||
|
localStorage.getItem('theme') || 'dark'
|
||||||
|
// document.body.className = theme;
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ThemeContext.Provider value={{ theme, setTheme }}>
|
||||||
|
{children}
|
||||||
|
</ThemeContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTheme() {
|
||||||
|
return useContext(ThemeContext);
|
||||||
|
}
|
||||||
@ -1,17 +1,10 @@
|
|||||||
import React, {useEffect, useState} from 'react';
|
import React from 'react';
|
||||||
|
import { useTheme } from '../ThemeContext';
|
||||||
import './Footer.css';
|
import './Footer.css';
|
||||||
|
|
||||||
|
|
||||||
const Footer = (props) => {
|
const Footer = () => {
|
||||||
const [theme, setTheme] = useState(
|
const { theme } = useTheme();
|
||||||
localStorage.getItem('theme') || 'light'
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem('theme', theme);
|
|
||||||
document.body.className = theme;
|
|
||||||
}, [theme]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<footer className={`bg-${theme} text-center text-lg-start text-${theme}`}>
|
<footer className={`bg-${theme} text-center text-lg-start text-${theme}`}>
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import React, {useEffect, useState} from 'react';
|
import React from 'react';
|
||||||
import {Link} from 'react-router-dom';
|
import {Link} from 'react-router-dom';
|
||||||
|
import { useTheme } from '../ThemeContext';
|
||||||
|
|
||||||
import './Navigation.css';
|
import './Navigation.css';
|
||||||
|
|
||||||
@ -8,16 +9,7 @@ let section = window.location.pathname.split('/')[2];
|
|||||||
let chapter = window.location.pathname.split('/')[3];
|
let chapter = window.location.pathname.split('/')[3];
|
||||||
|
|
||||||
const Navigation = () => {
|
const Navigation = () => {
|
||||||
const [theme, setTheme] = useState(
|
const { theme, setTheme } = useTheme();
|
||||||
localStorage.getItem('theme') || 'dark'
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem('theme', theme);
|
|
||||||
document.body.className = theme;
|
|
||||||
}, [theme]);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const toggleTheme = () => {
|
const toggleTheme = () => {
|
||||||
setTheme((theme === 'light') ? 'dark' : 'light');
|
setTheme((theme === 'light') ? 'dark' : 'light');
|
||||||
@ -60,7 +52,8 @@ const Navigation = () => {
|
|||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
</ul>
|
</ul>
|
||||||
<button style={{display: 'none'}} onClick={toggleTheme} type="button" className={`btn btn-${theme}`}>Toggle Theme</button>
|
{/* style={{display: 'none'}} */}
|
||||||
|
<button onClick={toggleTheme} type="button" className={`btn btn-${theme}`}>Toggle Theme</button>
|
||||||
{/* <form className="d-flex" role="search">
|
{/* <form className="d-flex" role="search">
|
||||||
<input className="form-control me-2" type="search" placeholder="Search" aria-label="Search"/>
|
<input className="form-control me-2" type="search" placeholder="Search" aria-label="Search"/>
|
||||||
<button className="btn btn-outline-success" type="submit">Search</button>
|
<button className="btn btn-outline-success" type="submit">Search</button>
|
||||||
|
|||||||
@ -3,11 +3,14 @@ import ReactDOM from 'react-dom/client';
|
|||||||
import './index.css';
|
import './index.css';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import reportWebVitals from './reportWebVitals';
|
import reportWebVitals from './reportWebVitals';
|
||||||
|
import { ThemeProvider } from './ThemeContext';
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||||
root.render(
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App />
|
<ThemeProvider>
|
||||||
|
<App />
|
||||||
|
</ThemeProvider>
|
||||||
</React.StrictMode>
|
</React.StrictMode>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user