mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-28 20:25:29 +08:00
* feat(scaleable): add scaleable props to each component * chore(scaleable): update the exported type * feat: apply scaleable to components chore: remove with-default test: improve testcase for scaleable chore: resolve test warning ci: upgrade nodejs to latest lts docs: fix type error in document site * docs: update documents to be compatible with scaleable chore: fix build errors * chore: remove all size-related attributes docs: improve guide document * docs: add scaleable documentation test: update snapshots chore: remove unused * feat: add scaleable to grid components * docs: improve docs * test: update snapshots * fix(grid): fix basic component props
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import React, { useMemo, useState } from 'react'
|
|
import { ConfigContext, Configs } from 'lib/config-context'
|
|
import { useRouter } from 'next/router'
|
|
import { DeepPartial } from 'components/utils/types'
|
|
import { GeistUIThemes, Themes } from 'components'
|
|
import { useTheme } from 'components'
|
|
import { CHINESE_LANGUAGE_IDENT, CUSTOM_THEME_TYPE } from './constants'
|
|
|
|
const defaultProps = {}
|
|
|
|
export type ConfigProviderProps = {
|
|
onThemeChange?: (themes: DeepPartial<GeistUIThemes>) => void
|
|
onThemeTypeChange?: (type: string) => void
|
|
}
|
|
|
|
const ConfigProvider: React.FC<React.PropsWithChildren<ConfigProviderProps>> = React.memo(
|
|
({
|
|
onThemeChange,
|
|
onThemeTypeChange,
|
|
children,
|
|
}: React.PropsWithChildren<ConfigProviderProps> & typeof defaultProps) => {
|
|
const theme = useTheme()
|
|
const { pathname } = useRouter()
|
|
const [isChinese, setIsChinese] = useState<boolean>(() =>
|
|
pathname.includes(CHINESE_LANGUAGE_IDENT),
|
|
)
|
|
const [scrollHeight, setScrollHeight] = useState<number>(0)
|
|
const [tabbarFixed, setTabbarFixed] = useState<boolean>(false)
|
|
const [customTheme, setCustomTheme] = useState<GeistUIThemes>(theme)
|
|
|
|
const updateSidebarScrollHeight = (height: number) => setScrollHeight(height)
|
|
const updateChineseState = (state: boolean) => setIsChinese(state)
|
|
const updateTabbarFixed = (state: boolean) => setTabbarFixed(state)
|
|
const updateCustomTheme = (nextTheme: DeepPartial<GeistUIThemes>) => {
|
|
const mergedTheme = Themes.create(theme, { ...nextTheme, type: CUSTOM_THEME_TYPE })
|
|
setCustomTheme(mergedTheme)
|
|
onThemeChange && onThemeChange(mergedTheme)
|
|
}
|
|
const switchTheme = (type: string) => {
|
|
onThemeTypeChange && onThemeTypeChange(type)
|
|
}
|
|
|
|
const initialValue = useMemo<Configs>(
|
|
() => ({
|
|
onThemeChange,
|
|
isChinese,
|
|
tabbarFixed,
|
|
customTheme,
|
|
switchTheme,
|
|
updateCustomTheme,
|
|
updateTabbarFixed,
|
|
updateChineseState,
|
|
sidebarScrollHeight: scrollHeight,
|
|
updateSidebarScrollHeight,
|
|
}),
|
|
[onThemeChange, scrollHeight, tabbarFixed, isChinese],
|
|
)
|
|
|
|
return (
|
|
<ConfigContext.Provider value={initialValue}>{children}</ConfigContext.Provider>
|
|
)
|
|
},
|
|
)
|
|
|
|
ConfigProvider.defaultProps = defaultProps
|
|
ConfigProvider.displayName = 'GeistConfigProvider'
|
|
export default ConfigProvider
|