---
title: ConfigProvider - 全局化配置
order: 8
path: /ConfigProvider
nav:
  order: 1
  title: 桌面组件
  path: /design
group:
  order: 10
  title: 其他
  path: /others
---

## ConfigProvider 全局化配置

### 代码演示

#### 主题切换

<code src="./demos/base.tsx" background="var(--bg-1)" height="400px"/>

## API

| 参数                     | 说明                                                                                                                                                                                                           | 类型                                                                                                                                | 默认值              | 版本                               |
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ---------------------------------- |
| autoInsertSpaceInButton  | 设置为 `false` 时，移除按钮中 2 个汉字之间的空格                                                                                                                                                               | boolean                                                                                                                             | true                |                                    |
| componentSize            | 设置 antd 组件大小                                                                                                                                                                                             | `small` \| `middle` \| `large`                                                                                                      | -                   |                                    |
| csp                      | 设置 [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) 配置                                                                                                                     | { nonce: string }                                                                                                                   | -                   |                                    |
| direction                | 设置文本展示方向。 [示例](#components-config-provider-demo-direction)                                                                                                                                          | `ltr` \| `rtl`                                                                                                                      | `ltr`               |                                    |
| dropdownMatchSelectWidth | 下拉菜单和选择器同宽。默认将设置 `min-width`，当值小于选择框宽度时会被忽略。`false` 时会关闭虚拟滚动                                                                                                           | boolean \| number                                                                                                                   | -                   | 4.3.0                              |
| form                     | 设置 Form 组件的通用属性                                                                                                                                                                                       | { validateMessages?: [ValidateMessages](/components/form/#validateMessages), requiredMark?: boolean \| `optional`, colon?: boolean} | -                   | requiredMark: 4.8.0; colon: 4.18.0 |
| getPopupContainer        | 弹出框（Select, Tooltip, Menu 等等）渲染父节点，默认渲染到 body 上。                                                                                                                                           | function(triggerNode)                                                                                                               | () => document.body |                                    |
| getTargetContainer       | 配置 Affix、Anchor 滚动监听容器。                                                                                                                                                                              | () => HTMLElement                                                                                                                   | () => window        | 4.2.0                              |
| iconPrefixCls            | 设置图标统一样式前缀。注意：需要配合 `less` 变量 [@iconfont-css-prefix](https://github.com/ant-design/ant-design/blob/d943b85a523bdf181dabc12c928226f3b4b893de/components/style/themes/default.less#L106) 使用 | string                                                                                                                              | `anticon`           | 4.11.0                             |
| input                    | 设置 Input 组件的通用属性                                                                                                                                                                                      | { autoComplete?: string }                                                                                                           | -                   | 4.2.0                              |
| locale                   | 语言包配置，语言包可到 [antd/lib/locale](http://unpkg.com/antd/lib/locale/) 目录下寻找                                                                                                                         | object                                                                                                                              | -                   |                                    |
| pageHeader               | 统一设置 PageHeader 的 ghost，参考 [PageHeader](/components/page-header)                                                                                                                                       | { ghost: boolean }                                                                                                                  | true                |                                    |
| prefixCls                | 设置统一样式前缀。注意：需要配合 `less` 变量 [@ant-prefix](https://github.com/ant-design/ant-design/blob/2c6c789e3a9356f96c47aea0083f5a15538315cf/components/style/themes/default.less#L7) 使用                | string                                                                                                                              | `ant`               |                                    |
| renderEmpty              | 自定义组件空状态。参考 [空状态](/components/empty/)                                                                                                                                                            | function(componentName: string): ReactNode                                                                                          | -                   |                                    |
| space                    | 设置 Space 的 `size`，参考 [Space](/components/space)                                                                                                                                                          | { size: `small` \| `middle` \| `large` \| `number` }                                                                                | -                   | 4.1.0                              |
| virtual                  | 设置 `false` 时关闭虚拟滚动                                                                                                                                                                                    | boolean                                                                                                                             | -                   | 4.3.0                              |

### ConfigProvider.config()

设置 `Modal`、`Message`、`Notification` rootPrefixCls。

### ConfigProvider.switchTheme（ option:SwitchConfig ）

`ConfigProvider.switchTheme(option:SwitchConfig)`方法可用来进行主题切换

#### SwitchConfig

| 参数       | 说明                 | 类型            | 默认值 |
| ---------- | -------------------- | --------------- | ------ |
| theme      | 主题名称             | `dark`\|`light` | light  |
| useStorage | 本地存储配置         | `boolean`       | true   |
| success    | 主题切换成功回调函数 | `()=>void`      | —      |

### ConfigProvider.onThemeChange（ (theme:themeObj)=>void ）:MutationObserver

`ConfigProvider.onThemeChange（ (theme:themeObj,themeName:'light'|'dark')=>void ）:MutationObserver`方法可以监听主题切换事件
