Skip to content

ConfigProvider 全局配置 ​

在应用外层包一层,子组件共用主题、语言、尺寸和禁用。用法对齐 Ant Design Vue:provide / inject,没写的属性沿用外层。

CkButton、CkInput、CkSelect、CkSwitch、CkSlider、CkStepper、CkTable、CkTabs、CkPagination 会读取 componentSize。CkButton、CkInput、CkSelect、CkSwitch、CkSlider、CkStepper、CkPagination 还会读取 componentDisabled。组件自己的 size / disabled 优先。

主题、语言、密度 ​

vue
<CkConfigProvider :theme="theme" :locale="locale" :density="density">
  <CkThemeSwitch v-model="theme" />
  <CkLangSwitch v-model="locale" />
  <CkClock />
  <CkButton type="primary">主要按钮</CkButton>
</CkConfigProvider>

theme 也可以写成 antd 的对象。algorithm 取 'light' | 'dark' | 'brand',token 覆盖对应的 CSS 变量。

vue
<CkConfigProvider
  :theme="{
    algorithm: 'dark',
    token: { colorPrimary: '#1677ff', borderRadius: 6 },
  }"
>
  <CkButton type="primary">主要按钮</CkButton>
</CkConfigProvider>

组件尺寸 ​

small / middle / large。middle 就是组件上的 default。内层 Provider 可以改掉外层,单个组件再传 size 时以组件为准。

vue
<CkConfigProvider :component-size="componentSize">
  <CkButton type="primary">按钮</CkButton>
  <CkInput placeholder="输入框" />
  <CkConfigProvider component-size="small">
    <CkButton>内层 Small</CkButton>
    <CkButton size="large">自己指定 Large</CkButton>
  </CkConfigProvider>
</CkConfigProvider>

禁用 ​

vue
<CkConfigProvider :component-disabled="componentDisabled">
  <CkButton type="primary">按钮</CkButton>
  <CkButton :disabled="false">强制可用</CkButton>
</CkConfigProvider>

方向 ​

vue
<CkConfigProvider :direction="direction">
  <CkButton type="primary" icon="search">查询状态</CkButton>
  <CkInput model-value="从右向左" />
</CkConfigProvider>

两个汉字间距 ​

对齐 antd 的 autoInsertSpaceInButton。打开后,没有图标、文案正好两个汉字的按钮会拉开字距。默认关闭。

vue
<CkConfigProvider :auto-insert-space-in-button="true">
  <CkButton type="primary">确定</CkButton>
</CkConfigProvider>

静态配置 ​

CkConfigProvider.config() 对应 antd 的 ConfigProvider.config(),作为最外层都没写时的默认值。

ts
import { CkConfigProvider } from "cockpit-ui";

CkConfigProvider.config({
  prefixCls: "ck",
  componentSize: "middle",
  direction: "ltr",
});

弹出层挂载节点用 getPopupContainer。CkSelect 的菜单走浏览器 popover,不使用这项。自定义弹出层通过 useConfig() 读取。

ts
import { useConfig } from "cockpit-ui";

const config = useConfig();
config?.getPopupContainer.value();

API ​

属性说明类型默认
theme主题名,或 { algorithm, token, inherit }'light' | 'dark' | 'brand' | ThemeConfig继承外层,最外层 'dark'
locale语言'zh-cn' | 'en'继承外层,最外层 'zh-cn'
density密度,影响顶栏高度与间距'compact' | 'default' | 'comfortable''default'
componentSize子组件尺寸。middle 等于组件的 default'small' | 'middle' | 'large'-
componentDisabled子组件禁用booleanfalse
direction文本方向'ltr' | 'rtl''ltr'
prefixCls容器类名前缀。内置组件类名仍是 ck-string'ck'
autoInsertSpaceInButton两个汉字的按钮拉开字距booleanfalse
inputInput 通用属性{ autocomplete?: string, allowClear?: boolean }-
getPopupContainer弹出层挂载节点(triggerNode?: HTMLElement) => HTMLElement() => document.body
getTargetContainer滚动监听容器() => HTMLElement | Window() => window

ThemeConfig ​

属性说明类型默认
algorithm主题名'light' | 'dark' | 'brand'继承外层
token覆盖 CSS 变量,见下表ThemeToken-
inherit是否继承外层 tokenbooleantrue

ThemeToken ​

键写入的变量
colorPrimary--cockpit-color-brand、--cockpit-color-blue
colorBgBase--cockpit-color-bg、--cockpit-color-canvas
colorBgContainer--cockpit-color-panel-strong
colorText--cockpit-color-fg
colorTextSecondary--cockpit-color-muted
colorSuccess / colorWarning / colorError / colorInfo成功、预警、危险、提示
colorBorder--cockpit-color-border
borderRadius--cockpit-radius-xs 到 --cockpit-radius-lg。数字按 px
fontFamily--cockpit-font-family
fontSize--cockpit-font-size-body。数字按 px