From 59264b999650621a38ef3350122168cd110f6bd1 Mon Sep 17 00:00:00 2001 From: JimmFly Date: Wed, 6 Nov 2024 06:19:38 +0000 Subject: [PATCH] feat(core): add tooltips to sidebar resize handle (#8717) close PD-1865 ![CleanShot 2024-11-06 at 12 52 35@2x](https://github.com/user-attachments/assets/b7353448-f8c5-4cfb-bfed-d1acff2923c4) --- .../components/resize-panel/resize-panel.tsx | 64 ++++++++++++++----- .../component/src/ui/tooltip/tooltip.tsx | 17 ++++- .../modules/app-sidebar/views/index.css.ts | 5 ++ .../src/modules/app-sidebar/views/index.tsx | 19 ++++++ .../views/sidebar-header/sidebar-switch.tsx | 12 ---- packages/frontend/i18n/src/resources/en.json | 2 + .../frontend/i18n/src/resources/zh-Hans.json | 2 + 7 files changed, 89 insertions(+), 32 deletions(-) diff --git a/packages/frontend/component/src/components/resize-panel/resize-panel.tsx b/packages/frontend/component/src/components/resize-panel/resize-panel.tsx index 0e298e3e0533c..8d3194ca748a0 100644 --- a/packages/frontend/component/src/components/resize-panel/resize-panel.tsx +++ b/packages/frontend/component/src/components/resize-panel/resize-panel.tsx @@ -3,6 +3,7 @@ import clsx from 'clsx'; import { forwardRef, useCallback, useLayoutEffect, useRef } from 'react'; import { useTransition } from 'react-transition-state'; +import { Tooltip, type TooltipProps } from '../../ui/tooltip'; import * as styles from './resize-panel.css'; export interface ResizeHandleProps @@ -17,6 +18,10 @@ export interface ResizeHandleProps onOpen: (open: boolean) => void; onResizing: (resizing: boolean) => void; onWidthChange: (width: number) => void; + tooltip?: TooltipProps['content']; + tooltipShortcut?: TooltipProps['shortcut']; + tooltipOptions?: Partial>; + tooltipShortcutClassName?: string; } export interface ResizePanelProps @@ -29,6 +34,12 @@ export interface ResizePanelProps resizeHandlePos: 'left' | 'right'; resizeHandleOffset?: number; resizeHandleVerticalPadding?: number; + resizeHandleTooltip?: TooltipProps['content']; + resizeHandleTooltipShortcut?: TooltipProps['shortcut']; + resizeHandleTooltipShortcutClassName?: string; + resizeHandleTooltipOptions?: Partial< + Omit + >; enableAnimation?: boolean; width: number; unmountOnExit?: boolean; @@ -49,6 +60,10 @@ const ResizeHandle = ({ onOpen, onResizing, onWidthChange, + tooltip, + tooltipShortcut, + tooltipOptions, + tooltipShortcutClassName, ...rest }: ResizeHandleProps) => { const ref = useRef(null); @@ -101,24 +116,31 @@ const ResizeHandle = ({ ); return ( -
-
-
+
+
+
+ ); }; @@ -143,6 +165,10 @@ export const ResizePanel = forwardRef( resizeHandlePos, resizeHandleOffset, resizeHandleVerticalPadding, + resizeHandleTooltip, + resizeHandleTooltipShortcut, + resizeHandleTooltipShortcutClassName, + resizeHandleTooltipOptions, ...rest }, ref @@ -176,6 +202,10 @@ export const ResizePanel = forwardRef( resizeHandlePos={resizeHandlePos} resizeHandleOffset={resizeHandleOffset} resizeHandleVerticalPadding={resizeHandleVerticalPadding} + tooltip={resizeHandleTooltip} + tooltipOptions={resizeHandleTooltipOptions} + tooltipShortcut={resizeHandleTooltipShortcut} + tooltipShortcutClassName={resizeHandleTooltipShortcutClassName} maxWidth={maxWidth} minWidth={minWidth} onOpen={onOpen} diff --git a/packages/frontend/component/src/ui/tooltip/tooltip.tsx b/packages/frontend/component/src/ui/tooltip/tooltip.tsx index 0f814d0cbd46b..3a885bb660046 100644 --- a/packages/frontend/component/src/ui/tooltip/tooltip.tsx +++ b/packages/frontend/component/src/ui/tooltip/tooltip.tsx @@ -40,15 +40,22 @@ export interface TooltipProps { rootOptions?: Omit; portalOptions?: TooltipPortalProps; options?: Omit; + shortcutClassName?: string; } -const TooltipShortcut = ({ shortcut }: { shortcut: string | string[] }) => { +const TooltipShortcut = ({ + shortcut, + className, +}: { + shortcut: string | string[]; + className?: string; +}) => { const commands = (Array.isArray(shortcut) ? shortcut : [shortcut]) .map(cmd => cmd.trim()) .map(cmd => getCommand(cmd)); return ( -
+
{commands.map((cmd, index) => (
{ if (!content) { return children; @@ -94,7 +102,10 @@ export const Tooltip = ({ {shortcut ? (
{content}
- +
) : ( content diff --git a/packages/frontend/core/src/modules/app-sidebar/views/index.css.ts b/packages/frontend/core/src/modules/app-sidebar/views/index.css.ts index 270e9a33d399c..c9b2b97927650 100644 --- a/packages/frontend/core/src/modules/app-sidebar/views/index.css.ts +++ b/packages/frontend/core/src/modules/app-sidebar/views/index.css.ts @@ -105,3 +105,8 @@ export const sidebarFloatMaskStyle = style({ }, }, }); + +export const resizeHandleShortcutStyle = style({ + alignItems: 'flex-end', + marginBottom: '2px', +}); diff --git a/packages/frontend/core/src/modules/app-sidebar/views/index.tsx b/packages/frontend/core/src/modules/app-sidebar/views/index.tsx index 1b91c31315022..4ea22eee3debf 100644 --- a/packages/frontend/core/src/modules/app-sidebar/views/index.tsx +++ b/packages/frontend/core/src/modules/app-sidebar/views/index.tsx @@ -3,6 +3,7 @@ import { ResizePanel } from '@affine/component/resize-panel'; import { useAppSettingHelper } from '@affine/core/components/hooks/affine/use-app-setting-helper'; import { NavigateContext } from '@affine/core/components/hooks/use-navigate-helper'; import { WorkspaceNavigator } from '@affine/core/components/workspace-selector'; +import { useI18n } from '@affine/i18n'; import { useLiveData, useService, @@ -23,6 +24,7 @@ import { navHeaderStyle, navStyle, navWrapperStyle, + resizeHandleShortcutStyle, sidebarFloatMaskStyle, } from './index.css'; import { SidebarHeader } from './sidebar-header'; @@ -170,6 +172,13 @@ export function AppSidebar({ children }: PropsWithChildren) { })} resizeHandleOffset={0} resizeHandleVerticalPadding={clientBorder ? 16 : 0} + resizeHandleTooltip={} + resizeHandleTooltipOptions={{ + side: 'right', + align: 'center', + }} + resizeHandleTooltipShortcut={['$mod', '/']} + resizeHandleTooltipShortcutClassName={resizeHandleShortcutStyle} data-transparent data-open={sidebarState !== 'close'} data-has-border={hasRightBorder} @@ -198,6 +207,16 @@ export function AppSidebar({ children }: PropsWithChildren) { ); } +const ResizeHandleTooltipContent = () => { + const t = useI18n(); + return ( +
+
{t['com.affine.rootAppSidebar.resize-handle.tooltip.drag']()}
+
{t['com.affine.rootAppSidebar.resize-handle.tooltip.click']()}
+
+ ); +}; + export function FallbackHeader() { return (
diff --git a/packages/frontend/core/src/modules/app-sidebar/views/sidebar-header/sidebar-switch.tsx b/packages/frontend/core/src/modules/app-sidebar/views/sidebar-header/sidebar-switch.tsx index 019c345b8880e..353e63c7ba044 100644 --- a/packages/frontend/core/src/modules/app-sidebar/views/sidebar-header/sidebar-switch.tsx +++ b/packages/frontend/core/src/modules/app-sidebar/views/sidebar-header/sidebar-switch.tsx @@ -1,5 +1,4 @@ import { IconButton } from '@affine/component'; -import { useI18n } from '@affine/i18n'; import { track } from '@affine/track'; import { SidebarIcon } from '@blocksuite/icons/rc'; import { useLiveData, useService } from '@toeverything/infra'; @@ -45,11 +44,6 @@ export const SidebarSwitch = ({ appSidebarService.toggleSidebar(); }, [appSidebarService, open]); - const t = useI18n(); - const tooltipContent = open - ? t['com.affine.sidebarSwitch.collapse']() - : t['com.affine.sidebarSwitch.expand'](); - return (
{{search}}", diff --git a/packages/frontend/i18n/src/resources/zh-Hans.json b/packages/frontend/i18n/src/resources/zh-Hans.json index d05f4e3d1ba6f..af10717d8dfb1 100644 --- a/packages/frontend/i18n/src/resources/zh-Hans.json +++ b/packages/frontend/i18n/src/resources/zh-Hans.json @@ -977,6 +977,8 @@ "com.affine.rootAppSidebar.tags.empty.new-tag-button": "新建标签", "com.affine.rootAppSidebar.tags.new-tag": "新建标签", "com.affine.rootAppSidebar.tags.no-doc": "没有文档", + "com.affine.rootAppSidebar.resize-handle.tooltip.drag": "拖动调整宽度", + "com.affine.rootAppSidebar.resize-handle.tooltip.click": "点击收起侧栏", "com.affine.search-tags.placeholder": "在这输入...", "com.affine.selectPage.empty": "选择页面为空", "com.affine.selectPage.empty.tips": "没有文档标题包含 <1>{{search}}",