mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 22:48:29 +08:00
2cdf586291
* chore: fix lint * chore: fix lint * test: fix 16 * fix: lint
159 lines
4.6 KiB
TypeScript
159 lines
4.6 KiB
TypeScript
import {
|
|
createCache,
|
|
legacyNotSelectorLinter,
|
|
logicalPropertiesLinter,
|
|
parentSelectorLinter,
|
|
StyleProvider,
|
|
extractStyle,
|
|
} from '@ant-design/cssinjs';
|
|
import { createSearchParams, useOutlet, useSearchParams } from 'dumi';
|
|
import { useServerInsertedHTML } from 'umi';
|
|
import React, { useCallback, useEffect, useMemo } from 'react';
|
|
import type { DirectionType } from 'antd/es/config-provider';
|
|
import { App, theme as antdTheme } from 'antd';
|
|
import useLayoutState from '../../hooks/useLayoutState';
|
|
import SiteThemeProvider from '../SiteThemeProvider';
|
|
import useLocation from '../../hooks/useLocation';
|
|
import type { ThemeName } from '../common/ThemeSwitch';
|
|
import ThemeSwitch from '../common/ThemeSwitch';
|
|
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;
|
|
|
|
// const styleCache = createCache();
|
|
// if (typeof global !== 'undefined') {
|
|
// (global as any).styleCache = styleCache;
|
|
// }
|
|
|
|
const getAlgorithm = (themes: ThemeName[] = []) =>
|
|
themes.map((theme) => {
|
|
if (theme === 'dark') {
|
|
return antdTheme.darkAlgorithm;
|
|
}
|
|
if (theme === 'compact') {
|
|
return antdTheme.compactAlgorithm;
|
|
}
|
|
return antdTheme.defaultAlgorithm;
|
|
});
|
|
|
|
const GlobalLayout: React.FC = () => {
|
|
const outlet = useOutlet();
|
|
const { pathname } = useLocation();
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const [{ theme = [], direction, isMobile }, setSiteState] = useLayoutState<SiteState>({
|
|
isMobile: false,
|
|
direction: 'ltr',
|
|
theme: [],
|
|
});
|
|
|
|
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 });
|
|
};
|
|
|
|
useEffect(() => {
|
|
const _theme = searchParams.getAll('theme') as ThemeName[];
|
|
const _direction = searchParams.get('direction') as DirectionType;
|
|
|
|
setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' });
|
|
// Handle isMobile
|
|
updateMobileMode();
|
|
|
|
window.addEventListener('resize', updateMobileMode);
|
|
return () => {
|
|
window.removeEventListener('resize', updateMobileMode);
|
|
};
|
|
}, []);
|
|
|
|
const siteContextValue = useMemo(
|
|
() => ({
|
|
direction,
|
|
updateSiteConfig,
|
|
theme: theme!,
|
|
isMobile: isMobile!,
|
|
}),
|
|
[isMobile, direction, updateSiteConfig, theme],
|
|
);
|
|
|
|
const [styleCache] = React.useState(() => createCache());
|
|
|
|
useServerInsertedHTML(() => {
|
|
const styleText = extractStyle(styleCache, true);
|
|
return <style data-type="antd-cssinjs" dangerouslySetInnerHTML={{ __html: styleText }} />;
|
|
});
|
|
|
|
const demoPage = pathname.startsWith('/~demos');
|
|
|
|
// ============================ Render ============================
|
|
let content: React.ReactNode = outlet;
|
|
|
|
// Demo page should not contain App component
|
|
if (!demoPage) {
|
|
content = (
|
|
<App>
|
|
{outlet}
|
|
<ThemeSwitch
|
|
value={theme}
|
|
onChange={(nextTheme) => updateSiteConfig({ theme: nextTheme })}
|
|
/>
|
|
</App>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<StyleProvider
|
|
cache={styleCache}
|
|
linters={[logicalPropertiesLinter, legacyNotSelectorLinter, parentSelectorLinter]}
|
|
>
|
|
<SiteContext.Provider value={siteContextValue}>
|
|
<SiteThemeProvider
|
|
theme={{
|
|
algorithm: getAlgorithm(theme),
|
|
token: {
|
|
motion: !theme.includes('motion-off'),
|
|
},
|
|
}}
|
|
>
|
|
{content}
|
|
</SiteThemeProvider>
|
|
</SiteContext.Provider>
|
|
</StyleProvider>
|
|
);
|
|
};
|
|
|
|
export default GlobalLayout;
|