From d30a503dee704dc71bec9ef658216ed2dbafbf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=86=B1?= Date: Tue, 13 Jan 2026 18:01:34 +0800 Subject: [PATCH] feat(sheets-ui): optimize mobile sheetbar (#6461) --- .../views/mobile/sheet-bar/MobileSheetBar.tsx | 80 +++++++++++++------ 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/packages/sheets-ui/src/views/mobile/sheet-bar/MobileSheetBar.tsx b/packages/sheets-ui/src/views/mobile/sheet-bar/MobileSheetBar.tsx index 3dba770f3a..5129fbcbc3 100644 --- a/packages/sheets-ui/src/views/mobile/sheet-bar/MobileSheetBar.tsx +++ b/packages/sheets-ui/src/views/mobile/sheet-bar/MobileSheetBar.tsx @@ -18,7 +18,7 @@ import type { ICommandInfo, Workbook } from '@univerjs/core'; import type { ISetWorksheetActiveOperationParams } from '@univerjs/sheets'; import type { IBaseSheetBarProps } from '../../sheet-bar/sheet-bar-tabs/SheetBarItem'; import { ICommandService } from '@univerjs/core'; -import { borderRightClassName, clsx } from '@univerjs/design'; +import { borderRightClassName, clsx, scrollbarClassName } from '@univerjs/design'; import { InsertSheetMutation, RemoveSheetMutation, @@ -42,9 +42,11 @@ export function MobileSheetBar() { function MobileSheetBarImpl(props: { workbook: Workbook }) { const { workbook } = props; + const [sheetList, setSheetList] = useState([]); const [activeKey, setActiveKey] = useState(''); const tabMapRef = useRef>(new Map()); + const containerRef = useRef(null); const commandService = useDependency(ICommandService); @@ -69,8 +71,12 @@ function MobileSheetBarImpl(props: { workbook: Workbook }) { if (tabMapRef.current.has(currentSubUnitId)) { const element = tabMapRef.current.get(currentSubUnitId); - if (element) { - element.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); + if (element && containerRef.current) { + const containerWidth = containerRef.current.clientWidth; + const elementWidth = element.clientWidth; + const elementLeft = element.offsetLeft; + const scrollLeft = elementLeft - (containerWidth - elementWidth) / 2; + containerRef.current.scrollTo({ left: Math.max(0, scrollLeft), behavior: 'smooth' }); } } @@ -79,12 +85,12 @@ function MobileSheetBarImpl(props: { workbook: Workbook }) { useEffect(() => updateSheetItems(), [updateSheetItems]); - const onTabClick = useCallback((sheetId: string) => { + const handleClick = useCallback((sheetId: string) => { commandService.executeCommand(SetWorksheetActiveOperation.id, { unitId: workbook.getUnitId(), subUnitId: sheetId, } as ISetWorksheetActiveOperationParams); - }, [commandService, workbook]); + }, [workbook]); useEffect(() => { const disposable = commandService.onCommandExecuted((commandInfo: ICommandInfo) => { @@ -103,36 +109,62 @@ function MobileSheetBarImpl(props: { workbook: Workbook }) { }); return () => disposable.dispose(); - }, [commandService, updateSheetItems]); + }, [updateSheetItems]); return (
-
+
{sheetList.map((sheet) => (
{ tabMapRef.current.set(sheet.sheetId!, element); }} - className={clsx( - ` - univer-box-border univer-h-full univer-min-w-12 univer-max-w-[120px] univer-shrink-0 - univer-flex-nowrap univer-items-center univer-truncate univer-px-1 univer-py-0.5 - univer-text-center univer-text-xs univer-leading-7 - `, - borderRightClassName, - { - 'univer-bg-white univer-text-primary-600 dark:!univer-bg-slate-600': sheet.sheetId === activeKey, - } - )} + className={clsx(` + univer-relative univer-box-border univer-flex univer-h-full univer-max-w-36 univer-shrink-0 + univer-cursor-pointer univer-select-none univer-items-center univer-justify-center + univer-truncate univer-px-1 univer-py-0.5 univer-text-sm univer-font-medium univer-leading-6 + univer-transition-all + `, borderRightClassName, { + 'univer-bg-white univer-text-blue-600 dark:!univer-bg-gray-700 dark:!univer-text-blue-400': sheet.sheetId === activeKey, + 'univer-text-gray-600 hover:univer-bg-gray-50 active:univer-bg-gray-100 dark:!univer-text-gray-300 dark:hover:!univer-bg-gray-700': sheet.sheetId !== activeKey, + })} key={sheet.sheetId} - onClick={() => onTabClick(sheet.sheetId!)} + role="tab" + aria-selected={sheet.sheetId === activeKey} + aria-controls={`sheet-${sheet.sheetId}`} + tabIndex={sheet.sheetId === activeKey ? 0 : -1} + onClick={() => handleClick(sheet.sheetId!)} > - {sheet.label} + {/* Sheet Label */} + {sheet.label} + + {/* Active Indicator */} + {sheet.sheetId === activeKey && ( + ))}