ConfigProvider Global configuration
Provide theme, locale, sizes and defaults to descendants.
Import
Use <hui-config-provider> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
In uni-app, load the theme in the global styles of App.vue, without scoped:
@use '@nscode/handy-uni/styles/theme.scss';
For ordinary Vue 3, import the compiled component styles in the application entry. This file includes the mobile theme variables:
import '@nscode/handy-uni/web/style.css'
To compile the Sass theme separately, use @use '@nscode/handy-uni/web/theme.scss';. The uni-app theme defines defaults on page, while the Web theme uses :root. Both support .hui-theme-light, .hui-theme-dark and .hui-palette-*. Mobile control heights are 48px for large, 40px for default and 32px for small. PC and mobile styles must be isolated when displayed on the same page.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-config-provider theme="light" :theme-vars="{ colorPrimary: accent }" @theme-change="event0"
><hui-button type="primary" @click="accent = accent === '#2563eb' ? '#059669' : '#2563eb'"
>切换主色 / Toggle color</hui-button
></hui-config-provider
>
<hui-config-provider theme="dark"
><hui-text>暗色区域 / Dark area</hui-text></hui-config-provider
>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const accent = ref('#2563eb')
const event0 = (arg0: unknown) => {
lastEvent.value = 'theme-change' + ': ' + [describe(arg0)].join(', ')
}
</script>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
theme | UniTheme | — | Theme mode; auto follows the system. |
palette | string | — | Theme palette name. |
themeVars | Record<string, string> | — | Theme variable overrides. |
locale | UniLocaleInput | — | Locale pack merged by key. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
zIndex | number | — | Layer z-index; allocated from the current page stack when omitted. |
defaults | UniComponentDefaults | — | Only Boolean button.block and button.round are supported. Other components, properties and state keys are ignored with development warnings. |
tag | "view" | "page" | 'view' | Root layout mode: view or page; page supplies background and minimum height. |
teleportTarget | string | HTMLElement | — | Web popup mount target; defaults to body. |
scrollLockTarget | string | HTMLElement | — | Web background scroll-lock container; defaults to body. |
pageScrollTarget | string | HTMLElement | — | Web page-scroll container for BackTop, IndexBar, PagedList, PullRefresh and form validation; defaults to the window and is ignored in uni. Pass an element inside Shadow DOM. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
theme-change | (theme: 'light' | 'dark') | System theme changed while using auto mode. |
Slots
| Slot | Parameters | Description |
|---|---|---|
default | — | Main content. |
Methods
None.
Configuration functions
setUniConfig(config) sets application defaults and getUniConfig() reads them. Set global defaults before creating pages. Nested providers merge locale, component defaults and theme variables. Currently, defaults only accepts { button: { block?: boolean, round?: boolean } }. Explicit component props take precedence, and false overrides an inherited value.
Keys beginning with default followed by an uppercase letter, and the state keys modelValue, value, visible, active, month, checkedKeys, expandedKeys, currentKey, opened, expanded and loading, cannot be defaults. They are ignored at runtime with development warnings. Other unsupported components and properties are also ignored.
import { getUniConfig, setUniConfig } from '@nscode/handy-uni'
setUniConfig({ size: 'small', defaults: { button: { round: true } } })
const config = getUniConfig()
Call useUniConfig() in setup to read the nearest configuration as a computed ref. useConfigProvider({ theme, palette, themeVars }) provides reactive theme options and returns config, themeClass and themeStyle. Bind the latter two to the custom root element so descendants receive the CSS variables and theme classes.
Types
ConfigProviderProps、UniTheme、UniGlobalConfig、ResolvedUniConfig、UniComponentDefaults、UniLocaleInput、ConfigProviderEmits are available from @nscode/handy-uni.
Platform notes
pageScrollTarget redirects page scrolling only in the ordinary Vue Web build. Without it, scrolling still targets the browser window. Pass an element inside Shadow DOM because document selectors cannot cross its boundary. This setting controls page scrolling; scrollLockTarget controls overlay scroll locking.
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.