Skip to content

Commit

Permalink
feat(config): add loading config
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangPF committed Jul 16, 2023
1 parent 0cf11d9 commit fb44ecd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions example/.dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export default defineConfig({
}
]
},
loading: {
skeleton: ['/guide', '/config']
},
docVersions: {
[pkgJSON.version]: ''
},
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"files": [
"dist"
],
"exports": {
"./loading": "./dist/common/Loading/index.js"
},
"scripts": {
"build": "father build",
"build:docs": "cross-env APP_ROOT=example dumi build",
Expand Down
43 changes: 43 additions & 0 deletions src/common/Loading/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useCallback } from 'react';
import { Skeleton, Space, Spin } from 'antd';
import { useLocation } from 'dumi';
import type { IAllThemeConfig } from '../../types';
import useAdditionalThemeConfig from '../../hooks/useAdditionalThemeConfig';

const defaultLoadingConfig: IAllThemeConfig['loading'] = {
skeleton: []
};

const Loading: React.FC = () => {
const { pathname } = useLocation();
const { loading = defaultLoadingConfig } = useAdditionalThemeConfig();

const skeletonLoadingRenderRule = useCallback(() => {
const pathnameReg = loading.skeleton;
if (pathnameReg) {
return (
pathnameReg.filter((rule) => {
return pathname.startsWith(rule);
}).length > 0
);
}
return false;
}, [loading, pathname]);

if (skeletonLoadingRenderRule()) {
return (
<Space direction="vertical" style={{ width: '100%', marginTop: 24 }} size={40}>
<Skeleton title={false} active paragraph={{ rows: 3 }} />
<Skeleton active paragraph={{ rows: 3 }} />
</Space>
);
}

return (
<Space style={{ width: '100%', margin: '120px 0', justifyContent: 'center' }} align="center">
<Spin size="large" />
</Space>
);
};

export default Loading;
4 changes: 1 addition & 3 deletions src/hooks/useMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] =>
sidebarGroupModePath === true
? true
: (sidebarGroupModePath ?? []).filter((rule: ISidebarGroupModePathItem) => {
return typeof rule === 'string'
? pathname.startsWith(rule)
: rule.test(pathname);
return pathname.startsWith(rule);
}).length > 0;

if (isSideBarGroupMode) {
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SerializedStyles } from '@emotion/react';
import { type IThemeConfig } from 'dumi/dist/client/theme-api/types';
import { type ThemeConfig } from 'antd';

export type ISidebarGroupModePathItem = string | RegExp;
export type ISidebarGroupModePathItem = string;

interface ILocaleEnhance {
/** 同 themeConfig 中 locales 项中的 id */
Expand Down Expand Up @@ -36,6 +36,9 @@ interface IFeature {
itemCss?: SerializedStyles;
}

interface ILoading {
skeleton?: Array<string>;
}
// 分组类型,将 children 换位字符串数组

export type SidebarEnhanceItemType = {
Expand Down Expand Up @@ -105,6 +108,8 @@ interface IAdditionalThemeConfig {
sidebarEnhance?: Record<string, SidebarEnhanceItems>;
/** antd 主题定制,同 `ConfigProvider` 中 `theme` */
theme?: Omit<ThemeConfig, 'algorithm'>;
/** 是否展示页面加载状态 */
loading?: ILoading;
}

export interface IAllThemeConfig extends Omit<IThemeConfig, 'socialLinks'>, IAdditionalThemeConfig {
Expand Down

0 comments on commit fb44ecd

Please sign in to comment.