ConfigProvider
Configure the locale, default size, button defaults, and overlay stacking level for descendant components. It can also switch the page theme.
Import
import { HuiConfigProvider } from '@nscode/handy-ui'
Basic Usage
Wrap your application or a section of it. Descendant components use the ConfigProvider settings when the corresponding props are omitted.
<template>
<HuiConfigProvider size="large" :button="{ type: 'primary' }">
<div style="margin-bottom: 12px">
<HuiSpace wrap>
<HuiButton>Large primary button</HuiButton>
<HuiButton size="small" type="default">Explicit props take precedence</HuiButton>
</HuiSpace>
</div>
<HuiInput placeholder="Large input" />
</HuiConfigProvider>
</template>
Default Size
size sets the default for descendant components that expose a size prop. It accepts large, default, or small. An explicit size on a component takes precedence. Change the size below to update several controls together:
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiButton,
HuiConfigProvider,
HuiInput,
HuiInputNumber,
HuiSegmented,
HuiSelect,
HuiSpace,
HuiSwitch,
HuiTag
} from '@nscode/handy-ui'
import type { SelectOption } from '@nscode/handy-ui'
const size = ref<'large' | 'default' | 'small'>('large')
const city = ref('beijing')
const count = ref(1)
const enabled = ref(true)
const options: SelectOption[] = [
{ label: 'Beijing', value: 'beijing' },
{ label: 'Shanghai', value: 'shanghai' },
{ label: 'Guangzhou', value: 'guangzhou' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="size" :options="['large', 'default', 'small']" />
<HuiConfigProvider :size="size">
<HuiSpace wrap>
<HuiButton type="primary">Button</HuiButton>
<HuiInput placeholder="Input" style="width: 160px" />
<HuiSelect v-model="city" :options="options" style="width: 160px" />
<HuiInputNumber v-model="count" :min="0" />
<HuiSwitch v-model="enabled" />
<HuiTag type="success">Tag</HuiTag>
<HuiButton size="small">Explicit small size</HuiButton>
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Inside a button group, avatar group, chip group, or form, an explicit size on the group or form takes precedence over ConfigProvider:
<script setup lang="ts">
import {
HuiConfigProvider,
HuiForm,
HuiFormItem,
HuiInput,
HuiSelect,
HuiSpace
} from '@nscode/handy-ui'
import type { SelectOption } from '@nscode/handy-ui'
const options: SelectOption[] = [
{ label: 'Beijing', value: 'beijing' },
{ label: 'Shanghai', value: 'shanghai' },
{ label: 'Guangzhou', value: 'guangzhou' }
]
</script>
<template>
<HuiConfigProvider size="small">
<HuiSpace direction="vertical" fill>
<HuiSelect :options="options" />
<!-- An explicit form size is used by controls inside the form -->
<HuiForm size="large">
<HuiFormItem label="Name">
<HuiInput />
</HuiFormItem>
</HuiForm>
</HuiSpace>
</HuiConfigProvider>
</template>
Button Defaults
button sets the default type and size for descendant buttons. Values on the button itself or its button group take precedence. button.size takes precedence over ConfigProvider's size and affects buttons only. Switch the type below to compare:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiConfigProvider, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { ButtonType } from '@nscode/handy-ui'
const type = ref<ButtonType>('primary')
const types = ['default', 'primary', 'success', 'warning', 'danger', 'info', 'help']
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="type" :options="types" />
<HuiConfigProvider :button="{ type }">
<HuiSpace wrap>
<HuiButton>Solid</HuiButton>
<HuiButton variant="outline">Outline</HuiButton>
<HuiButton plain>Plain</HuiButton>
<HuiButton variant="text">Text</HuiButton>
<HuiButton type="default">Explicit default</HuiButton>
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
When both button.size and size are provided, buttons use button.size while other controls use size:
<template>
<HuiConfigProvider size="large" :button="{ size: 'small', type: 'primary' }">
<HuiSpace wrap>
<HuiButton>Button: small</HuiButton>
<HuiInput placeholder="Input: large" style="width: 160px" />
<HuiButton size="default">Explicit default</HuiButton>
</HuiSpace>
</HuiConfigProvider>
</template>
Switching Languages
There are 79 built-in locales. See the Internationalization guide for the full list and on-demand imports. Changing locale immediately updates descendant component text. Explicit text props take precedence; application data and date formats are not translated.
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
HuiConfigProvider,
HuiDatePicker,
HuiEmpty,
HuiPagination,
HuiSegmented,
HuiSpace,
localeEn,
localeZhCn,
localeZhTw
} from '@nscode/handy-ui'
const locales = { 'zh-cn': localeZhCn, 'zh-tw': localeZhTw, en: localeEn }
const language = ref<keyof typeof locales>('zh-cn')
const locale = computed(() => locales[language.value])
const options = [
{ label: 'Simplified Chinese', value: 'zh-cn' },
{ label: 'Traditional Chinese', value: 'zh-tw' },
{ label: 'English', value: 'en' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="language" :options="options" />
<HuiConfigProvider :locale="locale">
<HuiSpace direction="vertical" fill>
<HuiDatePicker />
<HuiEmpty />
<HuiPagination :total="100" />
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Nested Locales
A nested locale affects only its descendants. Below, the outer section uses English while the nested date picker uses Traditional Chinese. The outermost ConfigProvider determines the locale for components invoked through functions; nested providers do not change it.
<script setup lang="ts">
import {
HuiConfigProvider,
HuiDatePicker,
HuiSpace,
localeEn,
localeZhTw
} from '@nscode/handy-ui'
</script>
<template>
<HuiConfigProvider :locale="localeEn">
<HuiSpace wrap>
<HuiDatePicker />
<HuiConfigProvider :locale="localeZhTw">
<HuiDatePicker />
</HuiConfigProvider>
</HuiSpace>
</HuiConfigProvider>
</template>
Overriding Individual Messages
locale accepts a partial locale. Missing keys are resolved from outer ConfigProviders, the locale configured through app.use, and finally the default Simplified Chinese locale, in that order. Placeholders such as {total} are replaced with their values.
<script setup lang="ts">
import { HuiConfigProvider, HuiPagination, localeEn } from '@nscode/handy-ui'
import type { HuiLocaleInput } from '@nscode/handy-ui'
const customLocale: HuiLocaleInput = { pagination: { total: 'Found {total} results' } }
</script>
<template>
<HuiConfigProvider :locale="localeEn">
<HuiConfigProvider :locale="customLocale">
<HuiPagination :total="100" />
</HuiConfigProvider>
</HuiConfigProvider>
</template>
Overlay Stacking
zIndex is the base stacking level for internal overlays, applied as an inline style on their root elements. The first overlay uses this value, and subsequently opened automatic levels appear above currently visible layers. When a layer is hidden, automatic levels are rearranged by opening order; explicit zIndex values are preserved. You can also configure zIndex when installing the application, with a local ConfigProvider taking precedence.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiConfigProvider, HuiModal, HuiSelect } from '@nscode/handy-ui'
import type { SelectOption } from '@nscode/handy-ui'
const visible = ref(false)
const options: SelectOption[] = [
{ label: 'Beijing', value: 'beijing' },
{ label: 'Shanghai', value: 'shanghai' },
{ label: 'Guangzhou', value: 'guangzhou' }
]
</script>
<template>
<HuiConfigProvider :z-index="3000">
<div style="margin-bottom: 12px">
<HuiButton @click="visible = true">Open modal</HuiButton>
</div>
<HuiSelect :options="options" />
<HuiModal v-model:visible="visible" title="Notice">Content</HuiModal>
</HuiConfigProvider>
</template>
Reactivity and Nesting
Prop updates affect descendant components. ConfigProviders can be nested: omitted settings inherit from the outer provider, and explicit settings use the inner provider's value. Change the outer size below:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiConfigProvider, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
const size = ref<'large' | 'default' | 'small'>('large')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="size" :options="['large', 'default', 'small']" />
<HuiConfigProvider :size="size">
<HuiSpace wrap>
<HuiButton>Outer</HuiButton>
<HuiConfigProvider :z-index="3000" :button="{ type: 'primary' }">
<!-- size follows the outer provider; button type and zIndex use the inner provider -->
<HuiButton>Inner: inherited size</HuiButton>
</HuiConfigProvider>
<HuiConfigProvider size="small">
<!-- The explicit inner size no longer follows the outer provider -->
<HuiButton>Inner: fixed small</HuiButton>
</HuiConfigProvider>
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Theme
theme sets data-theme on <html>, equivalent to calling document.documentElement.setAttribute('data-theme', theme). It affects the whole page, not only the provider's descendants. Omitting theme leaves the page theme unchanged. Unmounting the provider or removing the prop restores the value from before it took control. It is usually placed at the application root, for example <HuiConfigProvider theme="dark"><App /></HuiConfigProvider>.
Selecting Dark changes every component on this page to the dark theme. Select Not provided or leave the page to restore the previous theme:
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
HuiButton,
HuiCard,
HuiConfigProvider,
HuiInput,
HuiSegmented,
HuiSpace,
HuiSwitch,
HuiTag
} from '@nscode/handy-ui'
import type { SegmentedOption, ConfigProviderThemeMode } from '@nscode/handy-ui'
const mode = ref<string | number>('none')
const options: SegmentedOption[] = [
{ value: 'none', label: 'Not provided' },
{ value: 'light', label: 'Light (light)' },
{ value: 'dark', label: 'Dark (dark)' }
]
const theme = computed(() =>
mode.value === 'none' ? undefined : (mode.value as ConfigProviderThemeMode)
)
const enabled = ref(true)
</script>
<template>
<HuiSegmented v-model="mode" :options="options" />
<HuiConfigProvider :theme="theme">
<HuiCard title="Card" style="margin-top: 16px">
<HuiSpace wrap>
<HuiButton type="primary">Primary button</HuiButton>
<HuiButton>Default button</HuiButton>
<HuiInput placeholder="Input" style="width: 160px" />
<HuiSwitch v-model="enabled" />
<HuiTag type="success">Tag</HuiTag>
</HuiSpace>
</HuiCard>
</HuiConfigProvider>
</template>
Reading Configuration in Custom Components
import { useGlobalConfig } from '@nscode/handy-ui'
// Call at the top level of setup; returns undefined outside ConfigProvider
const config = useGlobalConfig()
const size = computed(() => config?.value.size ?? 'default')
ConfigViewer below is a custom component that displays the configuration it reads. Inside ConfigProvider, it reads the surrounding settings and follows their updates. Outside a provider, it reads undefined:
<script setup lang="ts">
import { defineComponent, h, ref } from 'vue'
import { HuiConfigProvider, HuiSegmented, HuiSpace, useGlobalConfig } from '@nscode/handy-ui'
const ConfigViewer = defineComponent({
setup() {
const config = useGlobalConfig()
return () =>
h(
'code',
config
? `size: ${config.value.size}, zIndex: ${config.value.zIndex}, button.type: ${config.value.button.type}`
: 'Outside ConfigProvider: undefined'
)
}
})
const size = ref<'large' | 'default' | 'small'>('default')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="size" :options="['large', 'default', 'small']" />
<HuiConfigProvider :size="size" :z-index="3000" :button="{ type: 'primary' }">
<ConfigViewer />
</HuiConfigProvider>
<ConfigViewer />
</HuiSpace>
</template>
Component Defaults
defaults uses registered component names without the Hui prefix and camelCase prop names. The 25 supported components are Button, Input, Select, SelectInput, CascaderPanel, AudioPlayer, VideoPlayer, Schedule, DatePicker, Pagination, Form, FormItem, Modal, Drawer, Tooltip, Popconfirm, Upload, Card, Descriptions, Empty, Table, RichContent, RichEditor, ImageCropper, and ImageCropperDialog. The type map covers every registered component. Names that have not been integrated produce a development warning; their settings do not take effect ahead of implementation.
Ordinary props resolve in this order: explicit prop → nearest merged component default → built-in component value. Explicit undefined skips that component default and retains the existing prop parsing and inheritance behavior. An empty string, false, and 0 are also explicit values. Replacing configuration or adding and deleting its keys in place updates the components. Function values remain props and are not called as factories.
Nested providers merge shallowly by component and prop. An inner value replaces the same outer prop; an object-valued prop is replaced as a whole. Each layer first normalizes the legacy button setting into defaults.Button, then merges its new defaults, so the new setting wins within that layer. An explicitly supplied inner size, including undefined, blocks inherited common component sizes; a component size configured by that same inner layer still applies. Drawer.size is a panel length, so this common-size reset does not remove it.
Inherited props keep their existing chain: Button.type first reads ButtonGroup. FormItem first reads actual Form settings for label position, label width, error placement, and message visibility. When Form has not configured a field, its built-in fallback does not hide the FormItem default.
Controlled state, disabled/readonly, business data, and initial state cannot be configured through defaults. Both types and runtime reject them, including modelValue, visible, selectedKeys, fileList, model, data, options, columns, rules, dependencies, prop, schema, and defaultValue. A component's own width, colors, appearance, and policy props remain configurable; global defaults do not create implicit form state.
The merged components retain their own data boundaries. RichContent.html cannot be supplied through defaults. RichEditor rejects modelValue, uploadImage, extensions, and starterKit. ImageCropper rejects src, zoom, and rotate; ImageCropperDialog also rejects visible. Supply those values, image sources, upload adapters, and editor initialization extensions explicitly on the relevant instance. Disabling and read-only state continue to use the shared restrictions above.
Buttons, inputs, selectors, tooltips, and other controls used internally by the library retain their own baseline props. Public Button defaults such as href, loading, and debounce, or an Input password default, therefore do not change internal operations. Explicit application configuration still overrides that baseline, and components supplied through user slots still read their provider. Function-based Modal.open, messages, notifications, and message boxes do not read these defaults; their explicit call options retain precedence. Locale, overlay stacking, and application-context behavior keep their existing contracts.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiConfigProvider,
HuiEmpty,
HuiForm,
HuiFormItem,
HuiInput,
HuiPopconfirm,
HuiSpace
} from '@nscode/handy-ui'
import type { HuiComponentDefaults } from '@nscode/handy-ui'
type DemoDefaults = Pick<HuiComponentDefaults, 'Button' | 'Input' | 'FormItem' | 'Empty'>
const defaults = reactive<DemoDefaults>({
Button: { type: 'primary', round: true },
Input: { size: 'large', clearable: true, placeholder: 'From component defaults' },
FormItem: { labelPosition: 'top' },
Empty: { description: 'Default description for the public empty state' }
})
const query = ref('You can clear this text')
const local = ref('The form explicitly uses small')
const status = ref('Click the public button or confirm to observe their existing behavior')
const toggle = () => {
if (!defaults.Button || !defaults.Input) return
defaults.Button.type = defaults.Button.type === 'primary' ? 'success' : 'primary'
defaults.Button.round = !defaults.Button.round
defaults.Input.clearable = !defaults.Input.clearable
}
</script>
<template>
<HuiSpace class="config-defaults-demo" direction="vertical" fill>
<HuiButton class="defaults-toggle" @click="toggle">Toggle component defaults</HuiButton>
<HuiConfigProvider :defaults="defaults" size="default">
<HuiSpace wrap>
<HuiButton
class="defaults-public-button"
@click="status = 'The public button received a click'"
>Public button</HuiButton
>
<HuiButton class="defaults-explicit-button" type="default" :round="false"
>Explicit props take precedence</HuiButton
>
<HuiButton class="defaults-undefined-button" :type="undefined" :round="undefined"
>Explicit undefined uses the built-in value</HuiButton
>
</HuiSpace>
<HuiInput
v-model="query"
class="defaults-public-input"
style="max-width: 300px"
aria-label="Input using component defaults"
/>
<HuiForm size="small" :model="{ local }">
<HuiFormItem label="The form size takes precedence"
><HuiInput v-model="local" class="defaults-form-input"
/></HuiFormItem>
</HuiForm>
<HuiConfigProvider
size="small"
:defaults="{ Input: { placeholder: 'The inner provider merges individual props' } }"
>
<HuiInput
class="defaults-nested-input"
style="max-width: 300px"
aria-label="Input using nested configuration"
/>
</HuiConfigProvider>
<HuiPopconfirm
title="Confirm this demo action?"
@confirm="status = 'Confirmed; internal buttons retain their own type and shape'"
>
<HuiButton class="defaults-confirm-trigger"
>Inspect the internal confirmation buttons</HuiButton
>
</HuiPopconfirm>
<HuiEmpty class="defaults-public-empty" />
</HuiConfigProvider>
<p class="defaults-status" role="status">{{ status }}</p>
</HuiSpace>
</template>
Application installation can also provide the outermost defaults. Providers inside the component tree continue to override them layer by layer:
import { createApp } from 'vue'
import HandyUI from '@nscode/handy-ui'
import App from './App.vue'
createApp(App)
.use(HandyUI, {
defaults: {
Button: { type: 'primary' },
Input: { clearable: true },
Table: { stripe: true }
}
})
.mount('#app')
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| theme | 'light' | 'dark' | — | Page theme; leaves the page theme unchanged when omitted |
| size | 'large' | 'default' | 'small' | 'default' | Default component size; an explicit component size takes precedence |
| zIndex | number | 2000 | Overlay z-index; see the overlay stacking description |
| button | ConfigProviderButtonConfig | {} | Legacy button size and type, merged per prop into defaults.Button |
| defaults | HuiComponentDefaults | {} | Default props by component name; see Component Defaults for the supported set and inheritance rules |
| locale | HuiLocaleInput | — | Full or partial locale; the outermost provider also determines the locale for components invoked through functions |
Omitted settings inherit from the outer ConfigProvider, then fall back to the defaults in the table.
Slots
| Slot | Description |
|---|---|
| default | Content to configure. The component renders no additional DOM element |
Types
Public types use the component prefix. Compatibility mappings are ThemeMode → ConfigProviderThemeMode, Size → ComponentSize, and ButtonConfig → ConfigProviderButtonConfig. The old names remain importable from @nscode/handy-ui, are marked @deprecated, and will be removed in 1.0.
import type {
ConfigProviderProps,
ConfigProviderContext,
ConfigProviderButtonConfig,
ConfigProviderThemeMode,
HuiLocale,
HuiLocaleInput,
HuiComponentDefaults,
HuiComponentPropsMap,
HuiComponentDefaultName,
HandyUIInstallOptions
} from '@nscode/handy-ui'
interface ConfigProviderButtonConfig {
size?: 'large' | 'default' | 'small'
type?: 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help'
}
// Values returned by useGlobalConfig()
interface ConfigProviderContext {
locales: HuiLocaleInput[] // Locales ordered from innermost to outermost
theme: 'light' | 'dark' | undefined // undefined when no provider specifies theme
size: 'large' | 'default' | 'small'
zIndex: number
button: ConfigProviderButtonConfig
defaults?: HuiComponentDefaults
}