Skip to content

Commit 4e156f6

Browse files
committed
feat(sidebar): 优化侧边栏列表高度设置,使用 ResizeObserver 代替 setTimeout
1 parent 5a985b4 commit 4e156f6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

packages/cherry-markdown/src/toolbars/Sidebar.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ export default class Sidebar extends Toolbar {
2929
list.appendChild(menus);
3030
this.options.dom.appendChild(list);
3131
// 将侧栏列表高度写入 CSS 变量,便于定位
32-
if (typeof document !== 'undefined') {
33-
setTimeout(() => {
32+
if (typeof document !== 'undefined' && typeof ResizeObserver !== 'undefined') {
33+
const observer = new ResizeObserver(() => {
3434
const height = list.scrollHeight;
3535
// 高度为 0 时保持默认 120px,不覆盖
3636
if (height > 0) {
3737
this.$cherry.wrapperDom.style.setProperty('--sidebar-list-height', `${height}px`);
3838
}
39-
}, 0);
39+
});
40+
observer.observe(list);
4041
}
4142
}
4243

0 commit comments

Comments
 (0)