|
|
|
import { scan } from 'react-scan'; |
|
|
|
import React, { useCallback, useEffect } from 'react'; |
|
import { |
|
createCache, |
|
extractStyle, |
|
legacyNotSelectorLinter, |
|
NaNLinter, |
|
parentSelectorLinter, |
|
StyleProvider, |
|
} from '@ant-design/cssinjs'; |
|
import { HappyProvider } from '@ant-design/happy-work-theme'; |
|
import { getSandpackCssText } from '@codesandbox/sandpack-react'; |
|
import { theme as antdTheme, App } from 'antd'; |
|
import type { MappingAlgorithm } from 'antd'; |
|
import type { DirectionType, ThemeConfig } from 'antd/es/config-provider'; |
|
import { createSearchParams, useOutlet, useSearchParams, useServerInsertedHTML } from 'dumi'; |
|
|
|
import { DarkContext } from '../../hooks/useDark'; |
|
import useLayoutState from '../../hooks/useLayoutState'; |
|
import type { ThemeName } from '../common/ThemeSwitch'; |
|
import SiteThemeProvider from '../SiteThemeProvider'; |
|
import type { SiteContextProps } from '../slots/SiteContext'; |
|
import SiteContext from '../slots/SiteContext'; |
|
import { isLocalStorageNameSupported } from '../utils'; |
|
|
|
import '@ant-design/v5-patch-for-react-19'; |
|
|
|
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][]; |
|
type SiteState = Partial<Omit<SiteContextProps, 'updateSiteConfig'>>; |
|
|
|
const RESPONSIVE_MOBILE = 768; |
|
export const ANT_DESIGN_NOT_SHOW_BANNER = 'ANT_DESIGN_NOT_SHOW_BANNER'; |
|
|
|
|
|
const ANT_DESIGN_SITE_THEME = 'ant-design-site-theme'; |
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
const hashId = location.hash.slice(1); |
|
if (hashId.startsWith('components-')) { |
|
if (!document.querySelector(`#${hashId}`)) { |
|
location.hash = `#${hashId.replace(/^components-/, '')}`; |
|
} |
|
} |
|
|
|
if (process.env.NODE_ENV !== 'production') { |
|
scan({ |
|
enabled: false, |
|
showToolbar: true, |
|
}); |
|
} |
|
} |
|
|
|
const getAlgorithm = (themes: ThemeName[] = []) => |
|
themes |
|
.map((theme) => { |
|
if (theme === 'dark') { |
|
return antdTheme.darkAlgorithm; |
|
} |
|
if (theme === 'compact') { |
|
return antdTheme.compactAlgorithm; |
|
} |
|
return null as unknown as MappingAlgorithm; |
|
}) |
|
.filter(Boolean); |
|
|
|
|
|
const getFinalTheme = (urlTheme: ThemeName[]): ThemeName[] => { |
|
|
|
const baseTheme = urlTheme.filter((t) => !['light', 'dark', 'auto'].includes(t)); |
|
const urlColor = urlTheme.find((t) => t === 'light' || t === 'dark'); |
|
if (urlColor) return [...baseTheme, urlColor]; |
|
|
|
if (isLocalStorageNameSupported()) { |
|
const stored = localStorage.getItem(ANT_DESIGN_SITE_THEME) as ThemeName; |
|
if (stored && ['light', 'dark', 'auto'].includes(stored)) { |
|
return [...baseTheme, stored]; |
|
} |
|
} |
|
|
|
return [...baseTheme, 'auto']; |
|
}; |
|
|
|
const GlobalLayout: React.FC = () => { |
|
const outlet = useOutlet(); |
|
const [searchParams, setSearchParams] = useSearchParams(); |
|
const [{ theme = [], direction, isMobile, bannerVisible = false }, setSiteState] = |
|
useLayoutState<SiteState>({ |
|
isMobile: false, |
|
direction: 'ltr', |
|
theme: [], |
|
bannerVisible: false, |
|
}); |
|
|
|
|
|
const useCssVar = searchParams.get('cssVar') !== 'false'; |
|
|
|
const updateSiteConfig = useCallback( |
|
(props: SiteState) => { |
|
setSiteState((prev) => ({ ...prev, ...props })); |
|
|
|
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') { |
|
const arr = Array.isArray(value) ? value : [value]; |
|
const base = arr.filter((t) => !['light', 'dark', 'auto'].includes(t)); |
|
const color = arr.find((t) => t === 'light' || t === 'dark'); |
|
if (color) { |
|
nextSearchParams = createSearchParams({ ...nextSearchParams, theme: [...base, color] }); |
|
} else { |
|
nextSearchParams.delete('theme'); |
|
} |
|
|
|
if (color) { |
|
document.querySelector('html')?.setAttribute('data-prefers-color', color); |
|
} |
|
} |
|
}); |
|
|
|
if (nextSearchParams.toString() !== oldSearchStr) { |
|
setSearchParams(nextSearchParams); |
|
} |
|
}, |
|
[searchParams, setSearchParams], |
|
); |
|
|
|
const updateMobileMode = () => { |
|
updateSiteConfig({ isMobile: window.innerWidth < RESPONSIVE_MOBILE }); |
|
}; |
|
|
|
|
|
useEffect(() => { |
|
if (typeof window === 'undefined') { |
|
return; |
|
} |
|
|
|
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); |
|
|
|
const handleSystemThemeChange = () => {}; |
|
|
|
mediaQuery.addEventListener('change', handleSystemThemeChange); |
|
|
|
return () => { |
|
mediaQuery.removeEventListener('change', handleSystemThemeChange); |
|
}; |
|
}, []); |
|
|
|
|
|
useEffect(() => { |
|
const urlTheme = searchParams.getAll('theme') as ThemeName[]; |
|
const finalTheme = getFinalTheme(urlTheme); |
|
const _direction = searchParams.get('direction') as DirectionType; |
|
|
|
setSiteState({ |
|
theme: finalTheme, |
|
direction: _direction === 'rtl' ? 'rtl' : 'ltr', |
|
}); |
|
|
|
|
|
const colorTheme = finalTheme.find((t) => ['light', 'dark'].includes(t)); |
|
if (colorTheme) { |
|
document.documentElement.setAttribute('data-prefers-color', colorTheme); |
|
} |
|
|
|
|
|
updateMobileMode(); |
|
|
|
|
|
const retrieveMirrorNotification = (window as any)[Symbol.for('antd.mirror-notify')]; |
|
if (typeof retrieveMirrorNotification === 'function') { |
|
retrieveMirrorNotification(); |
|
} |
|
|
|
window.addEventListener('resize', updateMobileMode); |
|
return () => { |
|
window.removeEventListener('resize', updateMobileMode); |
|
}; |
|
}, [searchParams]); |
|
|
|
const siteContextValue = React.useMemo<SiteContextProps>( |
|
() => ({ |
|
direction, |
|
updateSiteConfig, |
|
theme: theme!, |
|
isMobile: isMobile!, |
|
bannerVisible, |
|
}), |
|
[isMobile, direction, updateSiteConfig, theme, bannerVisible], |
|
); |
|
|
|
const themeConfig = React.useMemo<ThemeConfig>(() => { |
|
|
|
const themeForAlgorithm = theme; |
|
return { |
|
algorithm: getAlgorithm(themeForAlgorithm), |
|
token: { motion: !theme.includes('motion-off') }, |
|
cssVar: useCssVar, |
|
hashed: !useCssVar, |
|
}; |
|
}, [theme]); |
|
|
|
const [styleCache] = React.useState(() => createCache()); |
|
|
|
useServerInsertedHTML(() => { |
|
const styleText = extractStyle(styleCache, { |
|
plain: true, |
|
types: 'style', |
|
}); |
|
|
|
return <style data-type="antd-cssinjs" dangerouslySetInnerHTML={{ __html: styleText }} />; |
|
}); |
|
|
|
useServerInsertedHTML(() => { |
|
const styleText = extractStyle(styleCache, { |
|
plain: true, |
|
types: ['cssVar', 'token'], |
|
}); |
|
return ( |
|
<style |
|
data-type="antd-css-var" |
|
data-rc-order="prepend" |
|
data-rc-priority="-9999" |
|
// biome-ignore lint/security/noDangerouslySetInnerHtml: only used in .dumi |
|
dangerouslySetInnerHTML={{ __html: styleText }} |
|
/> |
|
); |
|
}); |
|
|
|
useServerInsertedHTML(() => ( |
|
<style |
|
data-sandpack="true" |
|
id="sandpack" |
|
// biome-ignore lint/security/noDangerouslySetInnerHtml: only used in .dumi |
|
dangerouslySetInnerHTML={{ __html: getSandpackCssText() }} |
|
/> |
|
)); |
|
|
|
return ( |
|
<DarkContext value={theme.includes('dark')}> |
|
<StyleProvider |
|
cache={styleCache} |
|
linters={[legacyNotSelectorLinter, parentSelectorLinter, NaNLinter]} |
|
> |
|
<SiteContext value={siteContextValue}> |
|
<SiteThemeProvider theme={themeConfig}> |
|
<HappyProvider disabled={!theme.includes('happy-work')}> |
|
<App>{outlet}</App> |
|
</HappyProvider> |
|
</SiteThemeProvider> |
|
</SiteContext> |
|
</StyleProvider> |
|
</DarkContext> |
|
); |
|
}; |
|
|
|
export default GlobalLayout; |
|
|