Skip to content

Commit

Permalink
✨ feat: 支持自定义主题 token 的初始值配置
Browse files Browse the repository at this point in the history
close #9
  • Loading branch information
arvinxx committed Mar 14, 2023
1 parent 6c71f48 commit 5324555
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/docs/config/apiHeader.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nav: 配置

```tsx | pure
import { SmileOutlined } from '@ant-design/icons';
import { ApiHeader as Header, siteSelectors, useSiteStore } from 'dumi-theme-antdstyle';
import { ApiHeader as Header, siteSelectors, useSiteStore } from 'dumi-theme-antd-style';
import { FC, memo } from 'react';

const ApiHeader: FC = memo(() => {
Expand Down
29 changes: 29 additions & 0 deletions example/docs/config/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,32 @@ export default defineConfig({
},
});
```

### siteToken

- 类型:`SiteConfigToken`
- 默认值:`undefined`

网站主题 Token 配置,该配置会传递给 [DumiSiteProvider](/components/dumi-site-provider) 组件

```ts
export default defineConfig({
themeConfig: {
siteToken: {
headerHeight: 64,
footerHeight: 300,
sidebarWidth: 240,
tocWidth: 176,
contentMaxWidth: 1152,
},
},
});
```

| 属性名 | 类型 | 默认值 | 描述 |
| --------------- | -------- | ------ | ------------ |
| headerHeight | `number` | `64` | 网站头部高度 |
| footerHeight | `number` | `300` | 网站底部高度 |
| sidebarWidth | `number` | `240` | 侧边栏宽度 |
| tocWidth | `number` | `176` | 目录宽度 |
| contentMaxWidth | `number` | `1152` | 内容最大宽度 |
27 changes: 23 additions & 4 deletions src/components/DumiSiteProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import { StyleProvider, ThemeProvider as Provider } from 'antd-style';
import { PropsWithChildren } from 'react';
import { CustomTokenParams, StyleProvider, ThemeProvider as Provider } from 'antd-style';
import { ReactNode, useCallback } from 'react';

import { useThemeStore } from '../../store/useThemeStore';
import { getAntdTheme, getCustomStylish, getCustomToken } from '../../styles';
import { createCustomToken, getAntdTheme, getCustomStylish } from '../../styles';
import { SiteConfigToken } from '../../types';

export const ThemeProvider = ({ children }: PropsWithChildren) => {
export interface ThemeProviderProps {
token?: Partial<SiteConfigToken>;
children?: ReactNode;
}

export const ThemeProvider = ({ children, token }: ThemeProviderProps) => {
const themeMode = useThemeStore((s) => s.themeMode);

const getCustomToken = useCallback(
(params: CustomTokenParams) => {
const base = createCustomToken(params);

if (token) {
return { ...base, ...token };
} else {
return base;
}
},
[token],
);

return (
<StyleProvider speedy={process.env.NODE_ENV === 'production'} prefix={'site'}>
<Provider
Expand Down
7 changes: 3 additions & 4 deletions src/components/DumiSiteProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { App } from 'antd';
import { StyleProvider } from 'antd-style';
import { PropsWithChildren } from 'react';

import { ThemeProvider } from './ThemeProvider';
import { ThemeProvider, ThemeProviderProps } from './ThemeProvider';

export default ({ children }: PropsWithChildren) => {
export default ({ children, token }: ThemeProviderProps) => {
return (
<StyleProvider speedy={process.env.NODE_ENV === 'production'} prefix={'site'}>
<ThemeProvider>
<ThemeProvider token={token}>
<App>{children}</App>
</ThemeProvider>
</StyleProvider>
Expand Down
15 changes: 10 additions & 5 deletions src/layouts/DocLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import animateScrollTo from 'animated-scroll-to';
import { Helmet, useIntl, useLocation } from 'dumi';
import isEqual from 'fast-deep-equal';
import { memo, StrictMode, useEffect, type FC } from 'react';
import { memo, PropsWithChildren, StrictMode, useEffect, type FC } from 'react';

import SiteProvider from '../../components/DumiSiteProvider';
import DumiSiteProvider from '../../components/DumiSiteProvider';
import { StoreUpdater } from '../../components/StoreUpdater';

import Docs from '../../pages/Docs';
Expand Down Expand Up @@ -53,12 +53,17 @@ const DocLayout: FC = memo(() => {
);
});

const THemeProvider = ({ children }: PropsWithChildren) => {
const siteToken = useSiteStore((s) => s.siteData.themeConfig.siteToken, isEqual);
return <DumiSiteProvider token={siteToken}>{children}</DumiSiteProvider>;
};

export default () => (
<StrictMode>
<SiteProvider>
<StoreUpdater />
<StoreUpdater />
<THemeProvider>
<GlobalStyle />
<DocLayout />
</SiteProvider>
</THemeProvider>
</StrictMode>
);
6 changes: 3 additions & 3 deletions src/styles/customToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ declare module 'antd-style' {
}

/**
* @title 站点令牌
* @title 站点主题 Token
* @description 站点的一些基础配置信息
*/
interface SiteToken {
export interface SiteToken {
/**
* @title 头部高度
*/
Expand Down Expand Up @@ -62,7 +62,7 @@ interface SiteToken {
fontFamilyHighlighter: string;
}

export const getCustomToken: GetCustomToken<SiteToken> = ({ isDarkMode, token }) => {
export const createCustomToken: GetCustomToken<SiteToken> = ({ isDarkMode, token }) => {
const gradientColor1 = token.blue;
const gradientColor2 = isDarkMode ? token.pink : token.cyan;
const gradientColor3 = token.purple;
Expand Down
9 changes: 9 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SiteToken } from 'dumi-theme-antd-style/styles';
import { FooterColumn } from 'rc-footer/es/column';
import { ApiHeaderConfig, HighlighterSyntaxTheme, IHero } from '../types';

Expand Down Expand Up @@ -62,4 +63,12 @@ export interface SiteThemeConfig {
* @type HighlighterSyntaxTheme
*/
syntaxTheme?: HighlighterSyntaxTheme;
/**
* 网站主题 Token 配置
*/
siteToken?: SiteConfigToken;
}

export type SiteConfigToken = Partial<
Pick<SiteToken, 'headerHeight' | 'footerHeight' | 'sidebarWidth' | 'tocWidth' | 'contentMaxWidth'>
>;

1 comment on commit 5324555

@vercel
Copy link

@vercel vercel bot commented on 5324555 Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.