diff --git a/.dumi/hooks/useLayoutState.ts b/.dumi/hooks/useLayoutState.ts new file mode 100644 index 0000000000..e69486fcc6 --- /dev/null +++ b/.dumi/hooks/useLayoutState.ts @@ -0,0 +1,17 @@ +import { startTransition, useState } from 'react'; + +const useLayoutState = ( + ...args: Parameters> +): ReturnType> => { + const [state, setState] = useState(...args); + + const setLayoutState: typeof setState = (...setStateArgs) => { + startTransition(() => { + setState(...setStateArgs); + }); + }; + + return [state, setLayoutState]; +}; + +export default useLayoutState; diff --git a/.dumi/theme/layouts/GlobalLayout.tsx b/.dumi/theme/layouts/GlobalLayout.tsx index 3f0950daaf..23c96a4fb3 100644 --- a/.dumi/theme/layouts/GlobalLayout.tsx +++ b/.dumi/theme/layouts/GlobalLayout.tsx @@ -5,15 +5,16 @@ import { parentSelectorLinter, StyleProvider, } from '@ant-design/cssinjs'; -import { ConfigProvider, theme as antdTheme, App } from 'antd'; +import { App, ConfigProvider, theme as antdTheme } from 'antd'; import type { DirectionType } from 'antd/es/config-provider'; import { createSearchParams, useOutlet, useSearchParams } from 'dumi'; -import React, { startTransition, useCallback, useEffect, useMemo } from 'react'; +import React, { useCallback, useEffect, useMemo } from 'react'; 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'; +import useLayoutState from '../../hooks/useLayoutState'; type Entries = { [K in keyof T]: [K, T[K]] }[keyof T][]; type SiteState = Partial>; @@ -40,7 +41,7 @@ const GlobalLayout: React.FC = () => { const outlet = useOutlet(); const { pathname } = useLocation(); const [searchParams, setSearchParams] = useSearchParams(); - const [{ theme, direction, isMobile }, setSiteState] = React.useState({ + const [{ theme, direction, isMobile }, setSiteState] = useLayoutState({ isMobile: false, direction: 'ltr', theme: ['light'], @@ -85,11 +86,9 @@ const GlobalLayout: React.FC = () => { const _theme = searchParams.getAll('theme') as ThemeName[]; const _direction = searchParams.get('direction') as DirectionType; - startTransition(() => { - setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' }); - // Handle isMobile - updateMobileMode(); - }); + setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' }); + // Handle isMobile + updateMobileMode(); window.addEventListener('resize', updateMobileMode); return () => { diff --git a/package.json b/package.json index 6d49c88972..1b0f61f81c 100644 --- a/package.json +++ b/package.json @@ -203,7 +203,7 @@ "cheerio": "1.0.0-rc.12", "cross-env": "^7.0.0", "dekko": "^0.2.1", - "dumi": "^2.1.13", + "dumi": "^2.1.17", "duplicate-package-checker-webpack-plugin": "^3.0.0", "esbuild-loader": "^3.0.0", "eslint": "^8.0.0",