|
5 | 5 | * - 点击切换标签 |
6 | 6 | * - 中键关闭标签 |
7 | 7 | * - 拖拽重排序 |
8 | | - * - Chrome 风格等分宽度(不滚动) |
| 8 | + * - Chrome 风格等分宽度(溢出时可横向滚动) |
9 | 9 | */ |
10 | 10 |
|
11 | 11 | import * as React from 'react' |
@@ -156,6 +156,27 @@ function TabBarInner({ |
156 | 156 | const leaveTimerRef = React.useRef<ReturnType<typeof setTimeout>>() |
157 | 157 | const fadeTimerRef = React.useRef<ReturnType<typeof setTimeout>>() |
158 | 158 | const isWindows = React.useMemo(() => detectIsWindows(), []) |
| 159 | + |
| 160 | + // 滚动容器 ref |
| 161 | + const scrollRef = React.useRef<HTMLDivElement>(null) |
| 162 | + |
| 163 | + // 鼠标滚轮横向滚动 |
| 164 | + const handleWheel = React.useCallback((e: React.WheelEvent) => { |
| 165 | + if (scrollRef.current) { |
| 166 | + e.preventDefault() |
| 167 | + scrollRef.current.scrollLeft += e.deltaY || e.deltaX |
| 168 | + } |
| 169 | + }, []) |
| 170 | + |
| 171 | + // 新增 tab 时自动滚动到最右 |
| 172 | + const prevTabCount = React.useRef(tabs.length) |
| 173 | + React.useEffect(() => { |
| 174 | + if (tabs.length > prevTabCount.current && scrollRef.current) { |
| 175 | + scrollRef.current.scrollTo({ left: scrollRef.current.scrollWidth, behavior: 'smooth' }) |
| 176 | + } |
| 177 | + prevTabCount.current = tabs.length |
| 178 | + }, [tabs.length]) |
| 179 | + |
159 | 180 | React.useEffect(() => { |
160 | 181 | return () => { |
161 | 182 | if (enterTimerRef.current) clearTimeout(enterTimerRef.current) |
@@ -205,7 +226,11 @@ function TabBarInner({ |
205 | 226 | 需要交互的单个 Tab 会在 TabBarItem 内部自己声明 titlebar-no-drag。 */} |
206 | 227 | <div className="absolute inset-0 titlebar-drag-region" /> |
207 | 228 |
|
208 | | - <div className={cn("relative flex items-end flex-1 min-w-0 overflow-x-clip titlebar-drag-region", isWindows && "pr-[140px]")}> |
| 229 | + <div |
| 230 | + ref={scrollRef} |
| 231 | + onWheel={handleWheel} |
| 232 | + className={cn("relative flex items-end flex-1 min-w-0 overflow-x-auto scrollbar-none titlebar-drag-region", isWindows && "pr-[140px]")} |
| 233 | + > |
209 | 234 | {tabs.map((tab) => ( |
210 | 235 | <TabBarItem |
211 | 236 | key={tab.id} |
|
0 commit comments