70 lines
3.5 KiB
TypeScript
70 lines
3.5 KiB
TypeScript
/**
|
||
* 把 theme/palette.ts + theme/metrics.ts 写成 CSS 自定义属性。
|
||
*
|
||
* 为什么是运行时注入而不是再抄一份 CSS:抄一份就有两份真值,历史上「改一处忘另一处」
|
||
* 已经让 antd 控件和自绘区域漂出过两套尺子。这里由 TS 生成一次,
|
||
* Tailwind 的 @theme(styles/index.css)只做「语义名 → var(--c-*)」的转发,不含任何数字。
|
||
*
|
||
* 注入时机:main.tsx 在 createRoot 之前调 injectThemeVars()。#root 在这之前是空的,
|
||
* 唯一可能露白的是 <html> 自己的底色,所以 index.html 的内联脚本里提前铺了
|
||
* 一个同值字面量(只有这一个数字是抄的,值来自 palette.ts 的 ROOT_BG)。
|
||
*/
|
||
import { PALETTE } from './palette'
|
||
import { barHeight, breakpoints, checkboxSize, checkboxSizeTouch, controlHeight, fieldLabelWidth, fontStack, monoStack, radius, tag, typeScale } from './metrics'
|
||
import type { ThemeMode } from './palette'
|
||
|
||
const STYLE_ID = 'nex-theme-vars'
|
||
|
||
function block(mode: ThemeMode): string {
|
||
const p = PALETTE[mode]
|
||
const colors = Object.entries(p).map(([k, v]) => `--c-${k}:${v}`).join(';')
|
||
return `[data-theme="${mode}"]{${colors}}`
|
||
}
|
||
|
||
function geometry(): string {
|
||
const radii = Object.entries(radius).map(([k, v]) => `--radius-${k}:${v}px`).join(';')
|
||
const type = Object.entries(typeScale)
|
||
.map(([k, v]) => `--fs-${k}:${v.size}px;--fs-${k}-lh:${v.lh}`).join(';')
|
||
return [
|
||
`:root{`,
|
||
`--font-sans:${fontStack};--font-mono:${monoStack};`,
|
||
`--ctl-h-main:${controlHeight.main}px;--ctl-h-md:${controlHeight.sm}px;--ctl-h:var(--ctl-h-main);`,
|
||
/* lg 不进 --ctl-h 那条链(它不是「表单里的一档」):给盖在媒体上的大目标按钮用,
|
||
窄屏不抬——触摸档上限就是 44,所以 touch() 里不覆盖它。
|
||
注意每行末尾的分号:这些字符串是 join('') 拼起来的,漏一个就和下一条黏成
|
||
`44px--radius-chip:5px`,整条声明被 CSS 解析器丢掉,用到它的地方静默退回 auto
|
||
(灯箱箭头就从 42px 塌成 21px)。 */
|
||
`--ctl-h-lg:${controlHeight.lg}px;`,
|
||
`--tag-h-main:${tag.main.h}px;--tag-px-main:${tag.main.px}px;--tag-gap-main:${tag.main.gap}px;--tag-text-main:${tag.main.text}px;`,
|
||
`--tag-h-sm:${tag.sm.h}px;--tag-px-sm:${tag.sm.px}px;--tag-gap-sm:${tag.sm.gap}px;--tag-text-sm:${tag.sm.text}px;`,
|
||
`--tag-h:var(--tag-h-main);--tag-px:var(--tag-px-main);--tag-gap:var(--tag-gap-main);--tag-text:var(--tag-text-main);`,
|
||
radii + ';',
|
||
type + ';',
|
||
`--field-label-w:${fieldLabelWidth}px;--check-size:${checkboxSize}px;--nex-bar:${barHeight}px;`,
|
||
/* 标签列宽的作用域开关:默认 0 = 标签挨着控件(筛选条要的);
|
||
表单栅格(Field 所在容器)把它抬到 --field-label-w,整表共用一条控件左边界。 */
|
||
`--label-w:0px`,
|
||
`}`
|
||
].join('')
|
||
}
|
||
|
||
function touch(): string {
|
||
return `@media (max-width:${breakpoints.narrow}px){:root{`
|
||
+ `--ctl-h-main:${controlHeight.touchMain}px;--ctl-h-md:${controlHeight.touchSm}px;`
|
||
/* 手机档一行一个字段,标签列没有对齐对象,压回 0 */
|
||
+ `--field-label-w:0px;--check-size:${checkboxSizeTouch}px}}`
|
||
}
|
||
|
||
export function injectThemeVars() {
|
||
const css = `${geometry()}${block('dark')}${block('light')}${touch()}`
|
||
let el = document.getElementById(STYLE_ID) as HTMLStyleElement | null
|
||
if (!el) {
|
||
el = document.createElement('style')
|
||
el.id = STYLE_ID
|
||
document.head.appendChild(el)
|
||
}
|
||
el.textContent = css
|
||
}
|
||
|
||
export { STYLE_ID as THEME_VARS_STYLE_ID }
|