主题定制
组件的颜色、圆角、间距、字号、阴影都来自 CSS 变量(--hui- 开头),定义在 @nscode/theme 里。定制主题就是改这些变量的值,不需要重新编译组件库。
暗色主题
在 <html> 上加 data-theme="dark" 就切到暗色,去掉或改成 light 就回到亮色:
// 切到暗色
document.documentElement.setAttribute('data-theme', 'dark')
// 切回亮色
document.documentElement.setAttribute('data-theme', 'light')
也可以给 <html> 加 class="dark",效果一样。
主题选择规则:
- 写了
data-theme="dark"(或class="dark"):暗色; - 写了
data-theme="light":亮色,即使系统是深色模式; - 写了
data-theme="auto":跟随系统的深色模式设置(prefers-color-scheme); - 都没写:默认亮色。
需要跟随系统时,在 <html> 上显式写 data-theme="auto"。未配置主题的项目始终保持亮色。
HuiConfigProvider 的 theme 等同于在 <html> 上写 data-theme,作用于整个页面;不传 theme 时它不修改页面主题。
data-theme 也可以写在页面里的某个容器上,只让这一块用暗色:
<div data-theme="dark">
<!-- 这里面的组件是暗色 -->
</div>
弹出层(Select 的下拉、Tooltip、Modal 等)会挂到 body 下,不在这个容器里,所以不受局部容器的 data-theme 影响,仍然跟随根元素的主题。
配色方案
内置极简商务、翡翠绿意、中国红韵三套配色,默认使用极简商务。在 <html>(或某个容器)上加 data-palette 切换,可以和暗色主题叠加。官网顶栏右侧的调色板按钮可以直接切换。
data-palette | 风格 | 主色 |
|---|---|---|
不写或 classic | 极简商务 | #409eff |
jade | 翡翠绿意 | #18a058 |
crimson | 中国红韵 | #d9262c |
jade(翡翠绿意)以绿色为主,成功色和主色相同;暗色下主色是薄荷绿 #63e2b7。官网保存的旧 nature 偏好自动迁移至 jade,其他已移除配色回退至 classic。业务页面使用已移除的配色时也应更新 data-palette。
crimson(中国红韵)的主色是正红 #d9262c,页面底色是带一点粉的白 #fffafa,正文是偏暖的深灰;暗色下主色为 #e5484d。主色和危险色都是红色系,删除等危险操作的按钮要有明确的文字或图标,不能只靠颜色区分。
document.documentElement.setAttribute('data-palette', 'jade')
// 恢复默认配色
document.documentElement.removeAttribute('data-palette')
自定义变量
在自己的样式里覆盖变量即可,放在 @nscode/handy-ui/style.css 之后引入:
:root {
--hui-color-primary: #7c3aed;
--hui-color-primary-light: #8b5cf6; /* 悬停 */
--hui-color-primary-dark: #6d28d9; /* 按下 */
--hui-color-primary-bg: #f5f3ff; /* 亮色下的浅底色 */
--hui-border-radius: 4px;
}
/* 暗色下的浅底色:三种开启暗色的方式都要覆盖到 */
:root.dark,
[data-theme='dark'] {
--hui-color-primary-bg: rgba(124, 58, 237, 0.2);
}
@media (prefers-color-scheme: dark) {
:root[data-theme='auto'] {
--hui-color-primary-bg: rgba(124, 58, 237, 0.2);
}
}
:root 上设的 -bg 和暗色主题的选择器优先级相同,又写在后面,切到暗色时仍然生效,亮色的浅底色放在深色背景上会很刺眼,所以要像上面这样给暗色再设一次。暗色下没有单独设置的 -bg、-bg-hover 会按当前的功能色自动算出(功能色加 20%、30% 的不透明度),只改了功能色、没改 -bg 时不用写后面两段。
只想改某个页面或某个区域,把选择器换成对应的容器,并给容器加上 hui-theme-scope 类:
<div class="report-page hui-theme-scope">…</div>
.report-page {
--hui-color-primary: #0f766e;
--hui-color-primary-light: #3f918b;
--hui-color-primary-dark: #0c5e58;
--hui-color-primary-bg: #e7f1f0;
}
浅色阶(--hui-color-primary-light-3 … -light-9)是在声明它的元素上按当时的主色算好的,子元素继承的是算好的结果。容器带上 hui-theme-scope 类,浅色阶才会在容器上按新主色重新计算;-light、-dark、-bg 这几个固定值的变体要像上面这样自己写。
暗色下要单独设置时,选择器要覆盖三种开启方式::root.dark、[data-theme='dark'],以及 @media (prefers-color-scheme: dark) 里的 :root[data-theme='auto'],写法见上面第一个例子。
组件级变量的覆盖
组件级公开变量只读取并带兜底,例如color: var(--hui-chip-color, var(--_chip-color))。组件根、尺寸和状态类不会重定义公开值;类型、尺寸、状态改用--_开头的私有默认。公开配置可写在组件自身或任意祖先,嵌套同类组件也会继承;私有默认仍按各实例的类型和尺寸重置。旧组件按迁移清单逐项处理,具体公开项以组件文档为准。
Chip 的普通态与选中态使用各自的公开配色。普通态的 --hui-chip-color、--hui-chip-bg、--hui-chip-border-color 不覆盖选中态;选中态用对应的 -selected 变量,未配置则采用该实例的选中默认。两组都可以在任意祖先上设置:
.chip-colors {
--hui-chip-color: var(--hui-color-danger);
--hui-chip-bg: var(--hui-color-danger-light-9);
--hui-chip-border-color: var(--hui-color-danger);
--hui-chip-color-selected: var(--hui-color-white);
--hui-chip-bg-selected: var(--hui-color-success);
--hui-chip-border-color-selected: var(--hui-color-success);
}
命名为--hui-<组件>[-<部位>]-<属性>[-<状态>],例如--hui-tag-border-color、--hui-scrollbar-thumb-color-hover;属性用bg、color、border-color、radius、font-size、padding、height、width、gap等,状态放末尾。更名后不保留旧名兜底。
业务组件的scoped样式可以直接在祖先类上设置变量,不需要:deep(),CSS变量仍会继承到子组件:
.profile-panel {
--hui-form-section-title-font-size: 20px;
--hui-form-section-gap: 24px;
}
只有直接覆盖内部BEM选择器时才需要:deep(.hui-xxx__part)。此时按实际组件选择器的特异性与引入顺序处理;优先用公开变量,不把!important作为默认写法。scoped不会阻断变量继承,需要局部不同取值时在内层容器显式重设公开值。
Splitter 的分隔条尺寸可在祖先上配置;未传 gutterSize 时读取 --hui-splitter-gutter-size,默认 6px。显式属性优先,例如 :gutter-size="6" 会覆盖祖先的 12px。嵌套实例继承公开变量,各自的属性覆盖只作用于自身。
.wide-divider {
--hui-splitter-gutter-size: 12px;
}
变量一览
下面是常用变量。完整定义见 packages/theme/src/variables.scss,暗色的取值见 dark.scss。
功能色
每种功能色都有同一套后缀。primary 可以换成 success、warning、danger、info、help。
| 变量 | 用途 |
|---|---|
--hui-color-primary | 主色 |
--hui-color-primary-light、-lighter | 比主色浅一档、两档,常用于悬停 |
--hui-color-primary-dark、-darker | 比主色深一档、两档,常用于按下 |
--hui-color-primary-bg、-bg-hover | 浅底色及其悬停色,用于提示框、标签的底色 |
--hui-color-primary-light-3、-5、-7、-8、-9 | 浅色阶,数字越大越浅。由主色和背景色混合得到,改了主色、换配色、切暗色都会自动跟着变 |
另有 --hui-color-white、--hui-color-black、--hui-color-transparent。
文字、背景、边框
| 变量 | 用途 |
|---|---|
--hui-text-color | 正文 |
--hui-text-color-secondary | 次要文字 |
--hui-text-color-tertiary | 辅助说明 |
--hui-text-color-disabled | 禁用文字 |
--hui-text-color-placeholder | 占位文字 |
--hui-bg-color | 组件和页面的底色 |
--hui-bg-color-secondary、-tertiary | 比底色深一档、两档的背景,用于表头、斑马纹等 |
--hui-bg-color-page | 页面底色(内容区外面那一层) |
--hui-bg-color-hover、-active | 悬停、按下时叠加的半透明背景 |
--hui-bg-color-disabled | 禁用背景 |
--hui-fill-color、-light、-dark | 填充色:滑块和进度条的轨道、骨架屏等 |
--hui-mask-color | 弹窗、抽屉的遮罩 |
--hui-border-color | 边框 |
--hui-border-color-light、-lighter | 较浅的边框、分割线 |
--hui-border-color-hover | 输入类组件悬停时的边框 |
圆角、间距、尺寸
| 变量 | 取值 |
|---|---|
--hui-border-radius-sm / --hui-border-radius / -lg / -xl | 2px / 6px / 8px / 12px |
--hui-border-radius-round / -circle | 9999px / 50% |
--hui-spacing-1 … --hui-spacing-12 | 4px 起,每档加 4px(--hui-spacing-4 是 16px) |
--hui-size-xs / -sm / -md / -lg / -xl | 24px / 32px / 40px / 48px / 56px |
--hui-component-size-large / --hui-component-size / --hui-component-size-small | 40px / 32px / 24px(控件高度,含边框) |
字体
--hui-size-* 是通用尺寸刻度,组件不读取;控件高度使用 --hui-component-size-*。 字号 sm 和 base 都是 14px,小号控件使用 xs(12px)。
| 变量 | 取值 |
|---|---|
--hui-font-family、--hui-font-family-mono | 正文字体、等宽字体 |
--hui-font-size-xs / -sm / -base / -lg / -xl | 12px / 14px / 14px / 16px / 20px |
--hui-font-size-2xl … -5xl | 24px / 30px / 38px / 48px |
--hui-line-height-tight / -normal / -relaxed / -loose | 1.25 / 1.5 / 1.75 / 2 |
--hui-font-weight-normal / -medium / -semibold / -bold | 400 / 500 / 600 / 700 |
阴影、过渡
| 变量 | 用途 |
|---|---|
--hui-shadow-sm、--hui-shadow、-md、-lg、-xl、-2xl | 由浅到深的阴影 |
--hui-transition-duration-fast / -normal / -slow | 150ms / 200ms / 300ms |
--hui-transition-timing-function | 过渡曲线 |
组件弹层(对话框、抽屉、下拉、提示等)的层级不由 CSS 变量控制,从 2000 起,写在各弹层的内联样式里。主题里的 --hui-z-index-*(1000~1080)只给 hui-z-dropdown、hui-z-modal 这类工具类用,改它们不会影响组件弹层。
工具类
主题里带了一批 hui- 开头的工具类,可以直接写在业务页面的元素上:
| 类别 | 例子 |
|---|---|
| 文字 | hui-text-left、hui-text-center、hui-text-primary、hui-text-secondary、hui-ellipsis、hui-ellipsis-2 |
| 背景 | hui-bg-primary、hui-bg-primary-light、hui-bg-secondary |
| 布局 | hui-flex、hui-flex-col、hui-items-center、hui-justify-between、hui-gap-2 |
| 圆角、阴影、边框 | hui-rounded、hui-rounded-lg、hui-shadow、hui-shadow-md、hui-border |
| 其他 | hui-hidden、hui-cursor-pointer、hui-overflow-auto、hui-w-full |
完整列表见 packages/theme/src/base.scss。
在组件库的 SCSS 之外使用 mixin
@nscode/theme 还提供了一组 SCSS mixin(断点、文本截断、滚动条、弹性布局等),业务项目用 SCSS 时可以引入:
@use '@nscode/theme/scss/mixins.scss' as ns;
.title {
@include ns.text-ellipsis(2);
}
.sidebar {
// 断点名:sm 640px、md 768px、lg 1024px、xl 1280px、2xl 1536px
@include ns.breakpoint-down('md') {
display: none;
}
}
注意只引 mixins,不要在组件的样式里 @use '@nscode/theme' 整个主题,那样会把全部变量和重置样式再输出一份。