Browse Source

fe-21 使用context实现暗黑模式功能

master
Chunxian Zhang 2 years ago
parent
commit
3244b5dd63
1 changed files with 40 additions and 0 deletions
  1. +40
    -0
      lazy-timer-fe/src/context/ContextProvider.js

+ 40
- 0
lazy-timer-fe/src/context/ContextProvider.js View File

@ -0,0 +1,40 @@
import React, { createContext, useContext, useState } from 'react';
const StateContext = createContext();
const initialState = {
chat: false,
cart: false,
userProfile: false,
notification: false,
};
export const ContextProvider = ({ children }) => {
const [screenSize, setScreenSize] = useState(undefined);
const [currentColor, setCurrentColor] = useState('#03C9D7');
const [currentMode, setCurrentMode] = useState('Light');
const [themeSettings, setThemeSettings] = useState(false);
const [activeMenu, setActiveMenu] = useState(true);
const [isClicked, setIsClicked] = useState(initialState);
const setMode = (e) => {
setCurrentMode(e.target.value);
localStorage.setItem('themeMode', e.target.value);
};
const setColor = (color) => {
setCurrentColor(color);
localStorage.setItem('colorMode', color);
};
const handleClick = (clicked) => setIsClicked({ ...initialState, [clicked]: true });
return (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<StateContext.Provider value={{ currentColor, currentMode, activeMenu, screenSize, setScreenSize, handleClick, isClicked, initialState, setIsClicked, setActiveMenu, setCurrentColor, setCurrentMode, setMode, setColor, themeSettings, setThemeSettings }}>
{children}
</StateContext.Provider>
);
};
export const useStateContext = () => useContext(StateContext);

Loading…
Cancel
Save