injectThinScrollbar.ts
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 细滚动条规则「同步注入」。
*
* 为什么不用 CSS import:@uj/ui 的 ui.css 原本通过 `import './styles/ui.css'` 注入,
* 但在 Vite **dev 模式**下 CSS 是异步插入 <head> 的,时机可能晚于 rc-table 首次测量
* —— 于是表格体滚动条被收成 8px、表头却按原生 ~15px 预留,永久错位,且每次刷新随
* 加载时序漂移(同子应用部分页对齐、部分不对齐)。
*
* 这里改为在模块求值(必然早于任何表格组件 render)时同步 append 一条 <style>,
* 让规则在 rc-table 测量前就已落进 document.head,首次测量即量到 8px,从根上消除竞态。
* --uj-scrollbar-injected 是给 useScrollbarRemeasure 的可靠信号(HMR 等边缘情况的兜底)。
*/
const STYLE_ID = 'uj-thin-scrollbar';
const RULE = `
* {
scrollbar-width: thin;
scrollbar-color: var(--uj-border-strong) transparent;
}
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-thumb {
border-radius: 4px;
background: var(--uj-border-strong);
}
::-webkit-scrollbar-thumb:hover {
background: var(--uj-border-strong);
}
::-webkit-scrollbar-track {
background: transparent;
}
html {
--uj-scrollbar-injected: 1;
}
`;
export function injectThinScrollbar(): void {
if (typeof document === 'undefined') return;
if (document.getElementById(STYLE_ID)) return;
const style = document.createElement('style');
style.id = STYLE_ID;
style.setAttribute('data-uj', 'thin-scrollbar');
style.textContent = RULE;
// 同步插入 <head>,早于后续 React 渲染 —— 这是消除竞态的关键。
document.head.appendChild(style);
}
injectThinScrollbar();