-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Allen Zhang (张涛)
committed
Jun 30, 2024
1 parent
4965841
commit 0738b75
Showing
8 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/canyon-platform/src/components/GlobaScreenWidthLimitModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const useWindowSize = () => { | ||
const [windowSize, setWindowSize] = useState({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
setWindowSize({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
}; | ||
|
||
window.addEventListener("resize", handleResize); | ||
|
||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
}; | ||
}, []); | ||
|
||
return windowSize; | ||
}; | ||
const GlobaScreenWidthLimitModal = () => { | ||
const { width } = useWindowSize(); | ||
|
||
useEffect(() => { | ||
if (width < 1424) { | ||
document.body.style.overflow = "hidden"; | ||
} | ||
// 在组件卸载时恢复滚动 | ||
return () => { | ||
document.body.style.overflow = "auto"; | ||
}; | ||
}, [width]); | ||
|
||
return ( | ||
<> | ||
{width < 1424 ? ( | ||
<div className={"fullscreen-modal"}> | ||
<Result | ||
className={"bg-white dark:bg-black rounded-lg"} | ||
title="你的窗口太小了,请调大一点" | ||
/> | ||
</div> | ||
) : null} | ||
</> | ||
); | ||
}; | ||
|
||
export default GlobaScreenWidthLimitModal; |
107 changes: 107 additions & 0 deletions
107
packages/canyon-platform/src/components/GlobalContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// src/context/GlobalContext.js | ||
|
||
import { ConfigProvider } from "antd"; | ||
import enUS from "antd/es/locale/en_US"; | ||
import zhCN from "antd/es/locale/zh_CN"; | ||
import { createContext, useReducer, useEffect } from "react"; | ||
|
||
const getSystemTheme = () => | ||
window.matchMedia?.("(prefers-color-scheme: dark)").matches | ||
? "dark" | ||
: "light"; | ||
const getSystemLocale = () => | ||
navigator.language.startsWith("zh") ? zhCN : enUS; | ||
|
||
const initialState = { | ||
theme: getSystemTheme(), | ||
locale: getSystemLocale(), | ||
}; | ||
|
||
const GlobalContext = createContext(); | ||
|
||
const globalReducer = (state, action) => { | ||
switch (action.type) { | ||
case "TOGGLE_THEME": | ||
return { | ||
...state, | ||
theme: state.theme === "light" ? "dark" : "light", | ||
}; | ||
case "SET_THEME": | ||
return { | ||
...state, | ||
theme: action.payload, | ||
}; | ||
case "SET_LOCALE": | ||
return { | ||
...state, | ||
locale: action.payload, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
const { darkAlgorithm } = theme; | ||
const GlobalProvider = ({ children }) => { | ||
const [state, dispatch] = useReducer(globalReducer, initialState); | ||
|
||
useEffect(() => { | ||
// console.log(initialState) | ||
|
||
// if (initialState.locale ==='l'){ | ||
|
||
// } | ||
|
||
const darkModeMediaQuery = window.matchMedia( | ||
"(prefers-color-scheme: dark)", | ||
); | ||
|
||
const handleSystemThemeChange = (e) => { | ||
console.log("?????"); | ||
|
||
const newTheme = e.matches ? "dark" : "light"; | ||
if (newTheme === "dark") { | ||
document.documentElement.classList.add("dark"); | ||
// document.body.style.backgroundColor = "black"; | ||
} else { | ||
document.documentElement.classList.remove("dark"); | ||
// document.body.style.backgroundColor = "white"; | ||
} | ||
|
||
dispatch({ type: "SET_THEME", payload: newTheme }); | ||
}; | ||
|
||
handleSystemThemeChange({ matches: initialState.theme === "dark" }); | ||
|
||
darkModeMediaQuery.addEventListener("change", handleSystemThemeChange); | ||
|
||
const handleSystemLocaleChange = () => { | ||
const newLocale = navigator.language.startsWith("zh") ? zhCN : enUS; | ||
console.log(newLocale); | ||
dispatch({ type: "SET_LOCALE", payload: newLocale }); | ||
}; | ||
window.addEventListener("languagechange", handleSystemLocaleChange); | ||
|
||
return () => { | ||
darkModeMediaQuery.removeEventListener("change", handleSystemThemeChange); | ||
window.removeEventListener("languagechange", handleSystemLocaleChange); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<GlobalContext.Provider value={{ state, dispatch }}> | ||
<ConfigProvider | ||
locale={state.locale} | ||
theme={{ | ||
token: { | ||
colorPrimary: "#0071c2", | ||
}, | ||
algorithm: state.theme === "dark" ? [darkAlgorithm] : [], | ||
}} | ||
> | ||
{children} | ||
</ConfigProvider> | ||
</GlobalContext.Provider> | ||
); | ||
}; | ||
|
||
export { GlobalContext, GlobalProvider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters