2023-09-19 21:01:07 +08:00
|
|
|
import React, { useCallback, useEffect, useMemo } from 'react';
|
2023-09-16 23:42:12 +08:00
|
|
|
import dayjs from 'dayjs';
|
2023-01-17 17:51:59 +08:00
|
|
|
import {
|
|
|
|
createCache,
|
2023-08-25 19:08:52 +08:00
|
|
|
extractStyle,
|
2023-01-17 17:51:59 +08:00
|
|
|
legacyNotSelectorLinter,
|
|
|
|
logicalPropertiesLinter,
|
2023-01-20 10:31:27 +08:00
|
|
|
parentSelectorLinter,
|
2023-01-17 17:51:59 +08:00
|
|
|
StyleProvider,
|
|
|
|
} from '@ant-design/cssinjs';
|
2023-08-07 10:44:04 +08:00
|
|
|
import { HappyProvider } from '@ant-design/happy-work-theme';
|
2023-08-07 14:24:05 +08:00
|
|
|
import { getSandpackCssText } from '@codesandbox/sandpack-react';
|
2023-09-19 21:01:07 +08:00
|
|
|
import { App, theme as antdTheme } from 'antd';
|
2023-08-01 10:53:55 +08:00
|
|
|
import type { DirectionType } from 'antd/es/config-provider';
|
2023-09-19 21:01:07 +08:00
|
|
|
import { createSearchParams, useOutlet, useSearchParams, useServerInsertedHTML } from 'dumi';
|
2023-08-25 19:08:52 +08:00
|
|
|
|
|
|
|
import { DarkContext } from '../../hooks/useDark';
|
2023-04-23 15:42:36 +08:00
|
|
|
import useLayoutState from '../../hooks/useLayoutState';
|
2022-11-22 17:00:28 +08:00
|
|
|
import useLocation from '../../hooks/useLocation';
|
2023-01-17 17:51:59 +08:00
|
|
|
import type { ThemeName } from '../common/ThemeSwitch';
|
|
|
|
import ThemeSwitch from '../common/ThemeSwitch';
|
2023-08-25 19:08:52 +08:00
|
|
|
import SiteThemeProvider from '../SiteThemeProvider';
|
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;
|
2023-09-16 23:42:12 +08:00
|
|
|
export const ANT_DESIGN_NOT_SHOW_BANNER = 'ANT_DESIGN_NOT_SHOW_BANNER';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-07-27 16:22:53 +08:00
|
|
|
// const styleCache = createCache();
|
|
|
|
// if (typeof global !== 'undefined') {
|
|
|
|
// (global as any).styleCache = styleCache;
|
|
|
|
// }
|
2022-12-01 16:10:53 +08:00
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
const getAlgorithm = (themes: ThemeName[] = []) =>
|
2023-09-11 11:17:11 +08:00
|
|
|
themes
|
|
|
|
.map((theme) => {
|
|
|
|
if (theme === 'dark') {
|
|
|
|
return antdTheme.darkAlgorithm;
|
|
|
|
}
|
|
|
|
if (theme === 'compact') {
|
|
|
|
return antdTheme.compactAlgorithm;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter((item) => item);
|
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();
|
2023-09-16 23:42:12 +08:00
|
|
|
const [{ theme = [], direction, isMobile, bannerVisible = false }, setSiteState] =
|
2023-09-15 22:03:53 +08:00
|
|
|
useLayoutState<SiteState>({
|
|
|
|
isMobile: false,
|
|
|
|
direction: 'ltr',
|
|
|
|
theme: [],
|
2023-09-16 23:42:12 +08:00
|
|
|
bannerVisible: false,
|
2023-09-15 22:03:53 +08:00
|
|
|
});
|
2022-12-19 11:42:41 +08:00
|
|
|
|
|
|
|
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,
|
2023-09-19 21:01:07 +08:00
|
|
|
theme: value.filter((t) => t !== 'light'),
|
2023-09-13 09:31:22 +08:00
|
|
|
});
|
2023-09-20 16:50:55 +08:00
|
|
|
|
|
|
|
document
|
|
|
|
.querySelector('html')
|
|
|
|
?.setAttribute('data-prefers-color', value.includes('dark') ? 'dark' : 'light');
|
2022-12-19 11:42:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2023-09-16 23:42:12 +08:00
|
|
|
const storedBannerVisibleLastTime =
|
|
|
|
localStorage && localStorage.getItem(ANT_DESIGN_NOT_SHOW_BANNER);
|
|
|
|
const storedBannerVisible =
|
|
|
|
storedBannerVisibleLastTime && dayjs().diff(dayjs(storedBannerVisibleLastTime), 'day') >= 1;
|
|
|
|
|
|
|
|
setSiteState({
|
|
|
|
theme: _theme,
|
|
|
|
direction: _direction === 'rtl' ? 'rtl' : 'ltr',
|
|
|
|
bannerVisible: storedBannerVisibleLastTime ? storedBannerVisible : true,
|
|
|
|
});
|
2023-03-21 13:00:16 +08:00
|
|
|
// Handle isMobile
|
|
|
|
updateMobileMode();
|
2022-12-27 17:09:01 +08:00
|
|
|
|
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!,
|
2023-09-15 22:03:53 +08:00
|
|
|
bannerVisible,
|
2022-12-19 11:42:41 +08:00
|
|
|
}),
|
2023-09-15 22:03:53 +08:00
|
|
|
[isMobile, direction, updateSiteConfig, theme, bannerVisible],
|
2022-12-19 11:42:41 +08:00
|
|
|
);
|
|
|
|
|
2023-07-27 16:22:53 +08:00
|
|
|
const [styleCache] = React.useState(() => createCache());
|
|
|
|
|
|
|
|
useServerInsertedHTML(() => {
|
|
|
|
const styleText = extractStyle(styleCache, true);
|
|
|
|
return <style data-type="antd-cssinjs" dangerouslySetInnerHTML={{ __html: styleText }} />;
|
|
|
|
});
|
|
|
|
|
2023-08-07 14:24:05 +08:00
|
|
|
useServerInsertedHTML(() => (
|
|
|
|
<style
|
|
|
|
data-sandpack="true"
|
|
|
|
id="sandpack"
|
|
|
|
dangerouslySetInnerHTML={{ __html: getSandpackCssText() }}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
2023-07-26 11:03:42 +08:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
2023-08-25 19:08:52 +08:00
|
|
|
<DarkContext.Provider value={theme.includes('dark')}>
|
|
|
|
<StyleProvider
|
|
|
|
cache={styleCache}
|
|
|
|
linters={[logicalPropertiesLinter, legacyNotSelectorLinter, parentSelectorLinter]}
|
|
|
|
>
|
|
|
|
<SiteContext.Provider value={siteContextValue}>
|
|
|
|
<SiteThemeProvider
|
|
|
|
theme={{
|
|
|
|
algorithm: getAlgorithm(theme),
|
|
|
|
token: {
|
|
|
|
motion: !theme.includes('motion-off'),
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<HappyProvider disabled={!theme.includes('happy-work')}>{content}</HappyProvider>
|
|
|
|
</SiteThemeProvider>
|
|
|
|
</SiteContext.Provider>
|
|
|
|
</StyleProvider>
|
|
|
|
</DarkContext.Provider>
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GlobalLayout;
|