From 968a5a46f3483c0daa3d49889abf76a608906397 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 24 May 2021 23:00:24 +0800 Subject: [PATCH] feat: toggle expand on hover&leave --- src/components/ResizeHandler.tsx | 7 ++++++- src/components/SideBar.tsx | 10 +++++++++- src/components/SideBarBodyWrapper.tsx | 24 ++++++++++++++++++++++-- src/components/ToggleShowButton.tsx | 7 +++++-- src/styles/index.scss | 26 ++++++++++++++------------ 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/components/ResizeHandler.tsx b/src/components/ResizeHandler.tsx index 5d0d02b..2675a11 100644 --- a/src/components/ResizeHandler.tsx +++ b/src/components/ResizeHandler.tsx @@ -5,10 +5,13 @@ import { Size } from './SideBarBodyWrapper' type Props = { size: Size onResize(size: Size): void + onResizeStateChange?(state: ResizeState): void style?: React.CSSProperties } -export function HorizontalResizeHandler({ onResize, size, style }: Props) { +type ResizeState = 'idle' | 'resizing' + +export function HorizontalResizeHandler({ onResize, onResizeStateChange, size, style }: Props) { const pointerDown = React.useRef(false) const startX = React.useRef(0) const baseSize = React.useRef(size) @@ -22,6 +25,7 @@ export function HorizontalResizeHandler({ onResize, size, style }: Props) { startX.current = clientX pointerDown.current = true baseSize.current = latestPropSize.current + onResizeStateChange?.('resizing') }, []) React.useEffect(() => { @@ -39,6 +43,7 @@ export function HorizontalResizeHandler({ onResize, size, style }: Props) { if (pointerDown.current) { pointerDown.current = false baseSize.current = latestPropSize.current + onResizeStateChange?.('idle') } } window.addEventListener('mouseup', onPointerUp) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index c4c1cf3..c4fe47e 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -104,13 +104,21 @@ export function SideBar() {
- {!shouldShow && } + $shouldShow.onChange(true) : undefined} + onClick={toggleShowSideBar} + /> $shouldShow.onChange(false)} >
diff --git a/src/components/SideBarBodyWrapper.tsx b/src/components/SideBarBodyWrapper.tsx index 54735e4..b18599e 100644 --- a/src/components/SideBarBodyWrapper.tsx +++ b/src/components/SideBarBodyWrapper.tsx @@ -11,6 +11,7 @@ export type Size = number type Props = { baseSize: Size className?: string + onLeave?: React.HTMLAttributes['onMouseLeave'] } const MINIMAL_CONTENT_VIEWPORT_WIDTH = 100 @@ -20,9 +21,11 @@ export function SideBarBodyWrapper({ baseSize, className, children, + onLeave, }: React.PropsWithChildren) { const [size, setSize] = React.useState(baseSize) const configContext = useConfigs() + const blockLeaveRef = React.useRef(false) React.useEffect(() => { setSize(baseSize) @@ -51,10 +54,27 @@ export function SideBarBodyWrapper({ else if (size < MINIMAL_WIDTH) setSize(MINIMAL_WIDTH) else setSize(size) }, []) + + const onMouseLeave = React.useCallback( + e => { + if (blockLeaveRef.current) return + onLeave?.(e) + }, + [onLeave], + ) + return ( -
+
{children}
- {features.resize && } + {features.resize && ( + { + blockLeaveRef.current = state === 'resizing' + }} + size={size} + /> + )}
) } diff --git a/src/components/ToggleShowButton.tsx b/src/components/ToggleShowButton.tsx index 53a8d1a..243e3c7 100644 --- a/src/components/ToggleShowButton.tsx +++ b/src/components/ToggleShowButton.tsx @@ -7,9 +7,11 @@ import { Icon } from './Icon' type Props = { error?: string | null + className?: React.HTMLAttributes['className'] + onHover?: React.HTMLAttributes['onMouseEnter'] } & Pick, 'onClick'> -export function ToggleShowButton({ error, onClick }: Props) { +export function ToggleShowButton({ error, className, onClick, onHover }: Props) { const ref = React.useRef(null) const config = useConfigs() const [distance, setDistance] = React.useState(config.value.toggleButtonVerticalDistance) @@ -36,12 +38,13 @@ export function ToggleShowButton({ error, onClick }: Props) { const toggleIconMode = config.value.toggleButtonContent return ( -
+