2022-12-27 17:09:01 +08:00
|
|
|
import React, { startTransition, useCallback, useEffect, useMemo } from 'react';
|
2022-12-19 11:42:41 +08:00
|
|
|
import { createSearchParams, useOutlet, useSearchParams } from 'dumi';
|
2022-11-09 12:28:04 +08:00
|
|
|
import { ConfigProvider, theme as antdTheme } from 'antd';
|
2022-12-01 16:10:53 +08:00
|
|
|
import { createCache, StyleProvider } from '@ant-design/cssinjs';
|
2022-12-19 11:42:41 +08:00
|
|
|
import type { DirectionType } from 'antd/es/config-provider';
|
2022-11-22 17:00:28 +08:00
|
|
|
import ThemeSwitch from '../common/ThemeSwitch';
|
2022-12-08 17:44:49 +08:00
|
|
|
import type { ThemeName } from '../common/ThemeSwitch';
|
2022-11-22 17:00:28 +08:00
|
|
|
import useLocation from '../../hooks/useLocation';
|
2022-12-19 11:42:41 +08:00
|
|
|
import type { SiteContextProps } from '../slots/SiteContext';
|
|
|
|
import SiteContext from '../slots/SiteContext';
|
|
|
|
|
|
|
|
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][];
|
|
|
|
type SiteState = Partial<Omit<SiteContextProps, 'updateSiteContext'>>;
|
|
|
|
|
|
|
|
const RESPONSIVE_MOBILE = 768;
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-01 16:10:53 +08:00
|
|
|
const styleCache = createCache();
|
|
|
|
if (typeof global !== 'undefined') {
|
|
|
|
(global as any).styleCache = styleCache;
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
const getAlgorithm = (themes: ThemeName[] = []) =>
|
|
|
|
themes.map((theme) => {
|
2022-12-08 17:44:49 +08:00
|
|
|
if (theme === 'dark') {
|
|
|
|
return antdTheme.darkAlgorithm;
|
|
|
|
}
|
|
|
|
if (theme === 'compact') {
|
|
|
|
return antdTheme.compactAlgorithm;
|
|
|
|
}
|
|
|
|
return antdTheme.defaultAlgorithm;
|
|
|
|
});
|
2022-11-22 17:00:28 +08:00
|
|
|
|
2022-11-24 20:11:50 +08:00
|
|
|
const GlobalLayout: React.FC = () => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const outlet = useOutlet();
|
2022-11-22 17:00:28 +08:00
|
|
|
const { pathname } = useLocation();
|
2022-12-08 17:44:49 +08:00
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
2022-12-19 11:42:41 +08:00
|
|
|
const [{ theme, direction, isMobile }, setSiteState] = React.useState<SiteState>({
|
|
|
|
isMobile: false,
|
|
|
|
direction: 'ltr',
|
|
|
|
theme: ['light'],
|
|
|
|
});
|
|
|
|
|
|
|
|
const updateSiteConfig = useCallback(
|
|
|
|
(props: SiteState) => {
|
|
|
|
setSiteState((prev) => ({ ...prev, ...props }));
|
|
|
|
|
|
|
|
// updating `searchParams` will clear the hash
|
|
|
|
const oldSearchStr = searchParams.toString();
|
|
|
|
|
|
|
|
let nextSearchParams: URLSearchParams = searchParams;
|
|
|
|
(Object.entries(props) as Entries<SiteContextProps>).forEach(([key, value]) => {
|
|
|
|
if (key === 'direction') {
|
|
|
|
if (value === 'rtl') {
|
|
|
|
nextSearchParams.set('direction', 'rtl');
|
|
|
|
} else {
|
|
|
|
nextSearchParams.delete('direction');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (key === 'theme') {
|
|
|
|
nextSearchParams = createSearchParams({
|
|
|
|
...nextSearchParams,
|
|
|
|
theme: value.filter((t) => t !== 'light'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (nextSearchParams.toString() !== oldSearchStr) {
|
|
|
|
setSearchParams(nextSearchParams);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[searchParams, setSearchParams],
|
|
|
|
);
|
|
|
|
|
|
|
|
const updateMobileMode = () => {
|
|
|
|
updateSiteConfig({ isMobile: window.innerWidth < RESPONSIVE_MOBILE });
|
2022-11-22 17:00:28 +08:00
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const _theme = searchParams.getAll('theme') as ThemeName[];
|
|
|
|
const _direction = searchParams.get('direction') as DirectionType;
|
|
|
|
|
2022-12-27 17:09:01 +08:00
|
|
|
startTransition(() => {
|
|
|
|
setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' });
|
|
|
|
// Handle isMobile
|
|
|
|
updateMobileMode();
|
|
|
|
});
|
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
window.addEventListener('resize', updateMobileMode);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', updateMobileMode);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const siteContextValue = useMemo(
|
|
|
|
() => ({
|
|
|
|
direction,
|
|
|
|
updateSiteConfig,
|
|
|
|
theme: theme!,
|
|
|
|
isMobile: isMobile!,
|
|
|
|
}),
|
|
|
|
[isMobile, direction, updateSiteConfig, theme],
|
|
|
|
);
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
2022-12-01 16:10:53 +08:00
|
|
|
<StyleProvider cache={styleCache}>
|
2022-12-19 11:42:41 +08:00
|
|
|
<SiteContext.Provider value={siteContextValue}>
|
|
|
|
<ConfigProvider
|
|
|
|
theme={{
|
|
|
|
algorithm: getAlgorithm(theme),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{outlet}
|
|
|
|
{!pathname.startsWith('/~demos') && (
|
|
|
|
<ThemeSwitch
|
|
|
|
value={theme}
|
|
|
|
onChange={(nextTheme) => updateSiteConfig({ theme: nextTheme })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ConfigProvider>
|
|
|
|
</SiteContext.Provider>
|
2022-12-01 16:10:53 +08:00
|
|
|
</StyleProvider>
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GlobalLayout;
|