Skip to content

Commit 60a72e2

Browse files
authored
Improve sidebar and pane dragging and docking (#6078)
## Related issues Follow-up to the local workspace interaction review; no linked issue. ## How AI was used in this PR Codex isolated these changes from a larger workspace-layout experiment, implemented Rich's Electron-review feedback, and ran lint, typecheck, unit tests, and rendered interaction checks. Rich requested grouping sidebar and chat/preview drag-to-hide behavior in this PR. ## Proposed Changes - Let the sidebar, chat, and preview pane close when their divider is dragged 64px beyond the minimum width and released. Reopening restores the width from before that collapse gesture. - Keep the sidebar hover flyout's entrance animation while docking it immediately, preserving focus and scroll position without briefly remounting the flyout. - Keep the sidebar resize highlight crisp on Retina displays at its existing thickness. ## Testing Instructions 1. In a desktop-width window, drag the sidebar divider to resize it. Drag left past the collapse threshold and release; reopen the sidebar and confirm its previous width returns. Releasing just before the threshold should keep it open. 2. With chat and preview visible, drag their divider toward the preview until it closes. Reopen it with the panel control and confirm the previous split returns. Repeat in the other direction to hide chat, then restore chat with the existing icon-only control. 3. Close the sidebar, hover its toggle to reveal the flyout, then click to dock. The entrance should animate, but docking should be immediate with no second slide. Check keyboard focus and scrolling. 4. Hover or focus the sidebar resize divider in light and dark mode on a Retina display; the accent line should have crisp edges. 5. Narrow the window and confirm the sidebar still behaves as an overlay. Validation: - Modified-file ESLint, `git diff --check`, and `npm run typecheck` passed. - 174 tests passed across sidebar layout, panel split frame, panel controls, workspace behavior, and layout telemetry, including threshold boundaries and preserved widths. - Electron review verified sidebar hover/dock behavior and the highlight in light/dark; pixel sampling at 2× showed three solid accent pixels without blended edges. - The PR branch's real-component browser preview passed light/dark sidebar resize/collapse, immediate docking, chat/preview drag-to-hide in both directions, and width restoration, with no page errors. - Static-host behavior was not separately exercised. ## Pre-merge Checklist - [x] Have you checked for TypeScript, React or other console errors?
1 parent f213654 commit 60a72e2

9 files changed

Lines changed: 107 additions & 20 deletions

File tree

‎apps/ui/src/data/resizable-panels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function getDefaultPanelLayout( running = true ): PanelLayout {
2424

2525
export const PANEL_CONTENT_DEFAULT_WIDTH = 360;
2626
export const PANEL_MIN_WIDTH = 360;
27+
export const PANEL_COLLAPSE_OVERSHOOT = 64;
2728
const PANEL_MIN_CONTENT_WIDTH = 360;
2829
export const PANEL_SPLIT_MIN_WIDTH = PANEL_MIN_WIDTH + PANEL_MIN_CONTENT_WIDTH;
2930

‎apps/ui/src/features/sidebar/sidebar-layout/index.tsx‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,24 @@ export function SidebarLayout( {
6666
} ) {
6767
const connector = useConnector();
6868
const hasOpenDialog = useHasOpenDialog();
69+
const [ collapsed, setCollapsed ] = useStoredFlag( SIDEBAR_COLLAPSED_STORAGE_KEY, false );
6970
const sidebarResize = useResizablePanel( {
7071
config: SIDEBAR_PANEL_CONFIG,
7172
storageKey: SIDEBAR_PANEL_STORAGE_KEY,
73+
onCollapse: () => setCollapsed( true ),
7274
} );
7375
const sites = useSites();
7476
const emptyWorkspace = sites.isSuccess && sites.data.length === 0;
75-
const [ collapsed, setCollapsed ] = useStoredFlag( SIDEBAR_COLLAPSED_STORAGE_KEY, false );
7677

7778
const [ sidebarFlyoutState, setSidebarFlyoutState ] = useState< 'closed' | 'hover' | 'open' >(
7879
'closed'
7980
);
8081
const peek = sidebarFlyoutState === 'hover';
8182
const sidebarFlyoutOpen = sidebarFlyoutState === 'open';
83+
const focusDockedSidebar = useRef( false );
8284
const [ instantSidebarDock, setInstantSidebarDock ] = useState( false );
83-
const dockedSidebarRef = useRef< HTMLElement >( null );
84-
const setDockedFade = useScrollFade< HTMLDivElement >();
85-
const setFlyoutFade = useScrollFade< HTMLElement >();
86-
const dockedSidebarScrollRef = useRef< HTMLDivElement >( null );
87-
const dockScrollTop = useRef( 0 );
8885
useLayoutEffect( () => {
8986
if ( ! instantSidebarDock ) return;
90-
if ( dockedSidebarRef.current ) {
91-
dockedSidebarRef.current
92-
.querySelector< HTMLElement >( '[data-sidebar-toggle]' )
93-
?.focus( { preventScroll: true } );
94-
}
9587
let secondFrame = 0;
9688
const firstFrame = requestAnimationFrame( () => {
9789
secondFrame = requestAnimationFrame( () => setInstantSidebarDock( false ) );
@@ -101,6 +93,11 @@ export function SidebarLayout( {
10193
cancelAnimationFrame( secondFrame );
10294
};
10395
}, [ instantSidebarDock ] );
96+
const dockedSidebarRef = useRef< HTMLElement >( null );
97+
const setDockedFade = useScrollFade< HTMLDivElement >();
98+
const setFlyoutFade = useScrollFade< HTMLElement >();
99+
const dockedSidebarScrollRef = useRef< HTMLDivElement >( null );
100+
const dockScrollTop = useRef( 0 );
104101
const hoverBlockedUntil = useRef( 0 );
105102
const restoreSidebarFlyoutFocus = useRef( false );
106103
const restoreSidebarDialogFocus = useRef( false );
@@ -155,6 +152,12 @@ export function SidebarLayout( {
155152
const open = docked || sidebarFlyoutOpen;
156153
const sidebarFlyoutVisible = sidebarFlyoutState !== 'closed';
157154
useLayoutEffect( () => {
155+
if ( docked && focusDockedSidebar.current ) {
156+
focusDockedSidebar.current = false;
157+
dockedSidebarRef.current
158+
?.querySelector< HTMLElement >( '[data-sidebar-toggle]' )
159+
?.focus( { preventScroll: true } );
160+
}
158161
if ( docked && dockedSidebarScrollRef.current ) {
159162
dockedSidebarScrollRef.current.scrollTop = dockScrollTop.current;
160163
dockScrollTop.current = 0;
@@ -181,6 +184,7 @@ export function SidebarLayout( {
181184
const toggleSidebar = useCallback( () => {
182185
if ( hasOpenDialog ) return;
183186
if ( peek && ! overlay ) dockScrollTop.current = sidebarFlyoutRef.current?.scrollTop ?? 0;
187+
focusDockedSidebar.current = peek && ! overlay;
184188
setInstantSidebarDock( peek && ! overlay );
185189
hoverBlockedUntil.current = performance.now() + HOVER_CLOSE_DELAY_MS;
186190
clearHoverTimer();
@@ -230,7 +234,7 @@ export function SidebarLayout( {
230234
className={ styles.sidebarFlyoutAnchor }
231235
aria-hidden
232236
/>
233-
{ ! instantSidebarDock && (
237+
{ ( ! docked || hasOpenDialog ) && (
234238
<Popover.Root
235239
open={ peek || sidebarFlyoutOpen }
236240
onOpenChange={ ( next, details ) => {

‎apps/ui/src/features/sidebar/sidebar-layout/style.module.css‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,16 @@
159159
inset-block: 1px 0;
160160
inset-inline-start: var( --sidebar-width );
161161
width: var( --focus-width );
162-
translate: -50%;
162+
/* A whole CSS-pixel offset keeps the 1.5px Retina line on physical pixels. */
163+
translate: -1px;
163164
z-index: var( --z-overlay );
164165
pointer-events: none;
165166
background-color: var( --ring );
166167
opacity: 0;
167168
}
168169

169170
.root:dir( rtl )::after {
170-
translate: 50%;
171+
translate: 1px;
171172
}
172173

173174
.root:has( > .sidebarResizing )::after,

‎apps/ui/src/features/sidebar/sidebar-layout/use-resizable-panel.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getStoredResizablePanelWidth,
77
getViewportWidth,
88
PANEL_WIDTHS_RESET_EVENT,
9+
PANEL_COLLAPSE_OVERSHOOT,
910
storeResizablePanelWidth,
1011
type ResizablePanelConfig,
1112
} from '@/data/resizable-panels';
@@ -15,9 +16,10 @@ import type { KeyboardEvent } from 'react';
1516
interface UseResizablePanelOptions {
1617
config: ResizablePanelConfig;
1718
storageKey: string;
19+
onCollapse?: () => void;
1820
}
1921

20-
export function useResizablePanel( { config, storageKey }: UseResizablePanelOptions ) {
22+
export function useResizablePanel( { config, storageKey, onCollapse }: UseResizablePanelOptions ) {
2123
// In RTL the panel (and its resize edge) sits mirrored, so a rightward drag
2224
// shrinks it.
2325
const growsWithDeltaX = ! isRTL();
@@ -47,9 +49,14 @@ export function useResizablePanel( { config, storageKey }: UseResizablePanelOpti
4749
const delta = growsWithDeltaX ? deltaX : -deltaX;
4850
const nextWidth = clampResizablePanelWidth( start + delta, config, getViewportWidth() );
4951
setWidth( nextWidth );
50-
return nextWidth;
52+
return start + delta;
5153
},
5254
onCommit: ( latest ) => {
55+
if ( onCollapse && latest <= config.minWidth - PANEL_COLLAPSE_OVERSHOOT ) {
56+
setWidth( width );
57+
onCollapse();
58+
return;
59+
}
5360
saveWidth( latest );
5461
},
5562
} );

‎apps/ui/src/features/sidebar/sidebar-layout/workspace-panes.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ afterEach( () => {
6565
} );
6666

6767
describe( 'workspace sidebar layout', () => {
68+
it.each( [
69+
{ end: 193, closed: false },
70+
{ end: 192, closed: true },
71+
] )( 'closes on release past the sidebar threshold ($end)', async ( { end, closed } ) => {
72+
await setup();
73+
const handle = screen.getByRole( 'separator', { name: 'Resize sidebar' } );
74+
fireEvent.keyDown( handle, { key: 'ArrowRight', shiftKey: true } );
75+
fireEvent.mouseDown( handle, { button: 0, clientX: 296 } );
76+
fireEvent.mouseUp( document, { clientX: end } );
77+
expect( docked() ).toBe( ! closed );
78+
if ( closed ) {
79+
fireEvent.click( toggle() );
80+
expect( screen.getByRole( 'separator', { name: 'Resize sidebar' } ) ).toHaveAttribute(
81+
'aria-valuenow',
82+
'296'
83+
);
84+
}
85+
} );
86+
6887
it( 'preserves a manual close through narrow and wide windows', async () => {
6988
await setup();
7089
fireEvent.click( toggle() );

‎apps/ui/src/features/sites/panel-split-frame/index.test.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ describe( 'PanelSplitFrame', () => {
6666
expect( onPanelClose ).toHaveBeenCalledTimes( 1 );
6767
} );
6868

69+
it.each( [
70+
{ end: 703, closed: null },
71+
{ end: 704, closed: 'panel' },
72+
{ end: 297, closed: null },
73+
{ end: 296, closed: 'chat' },
74+
] )( 'closes $closed only past the drag threshold at $end', ( { end, closed } ) => {
75+
window.localStorage.setItem( SITE_WIDTH_KEY, '480' );
76+
const onPanelClose = vi.fn();
77+
const onChatClose = vi.fn();
78+
render(
79+
<PanelSplitFrame
80+
panelOpen
81+
panel={ () => <aside /> }
82+
onPanelClose={ onPanelClose }
83+
onChatClose={ onChatClose }
84+
/>
85+
);
86+
fireEvent.mouseDown( screen.getByRole( 'separator' ), { button: 0, clientX: 480 } );
87+
fireEvent.mouseUp( document, { clientX: end } );
88+
expect( onPanelClose ).toHaveBeenCalledTimes( closed === 'panel' ? 1 : 0 );
89+
expect( onChatClose ).toHaveBeenCalledTimes( closed === 'chat' ? 1 : 0 );
90+
if ( closed ) expect( window.localStorage.getItem( SITE_WIDTH_KEY ) ).toBe( '480' );
91+
expect( document.body ).not.toHaveStyle( { cursor: 'col-resize' } );
92+
} );
93+
6994
describe( 'closing panel content', () => {
7095
beforeEach( () => {
7196
vi.useFakeTimers();

‎apps/ui/src/features/sites/panel-split-frame/index.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface PanelSplitFrameProps {
2323
panel?: ( props: PanelSplitFramePanelProps ) => ReactNode;
2424
panelOpen?: boolean;
2525
onPanelClose?: () => void;
26+
onChatClose?: () => void;
2627
// The panel fills the frame over the hidden chat; the sidebar stays.
2728
chatHidden?: boolean;
2829
// Skip movement when restoring a different workspace.
@@ -37,6 +38,7 @@ export function PanelSplitFrame( {
3738
panel,
3839
panelOpen = false,
3940
onPanelClose,
41+
onChatClose,
4042
chatHidden = false,
4143
instant = false,
4244
children,
@@ -46,6 +48,8 @@ export function PanelSplitFrame( {
4648
const { rootRef, containerWidth, contentWidthVar, isResizing, handleProps } = usePanelSplit( {
4749
showPanel,
4850
siteId,
51+
onPanelClose,
52+
onChatClose,
4953
} );
5054
const isSidebarCollapsed = useSidebarCollapsed();
5155
const resizable = ( containerWidth ?? Infinity ) >= PANEL_SPLIT_MIN_WIDTH;

‎apps/ui/src/features/sites/panel-split-frame/use-panel-split.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PANEL_CONTENT_WIDTH_STORAGE_KEY,
88
PANEL_CONTENT_DEFAULT_WIDTH,
99
PANEL_MIN_WIDTH,
10+
PANEL_COLLAPSE_OVERSHOOT,
1011
storeResizablePanelWidth,
1112
} from '@/data/resizable-panels';
1213
import { usePointerDrag } from '@/lib/hooks';
@@ -15,6 +16,8 @@ import type { KeyboardEvent, MouseEventHandler, RefObject } from 'react';
1516
interface UsePanelSplitOptions {
1617
siteId?: string;
1718
showPanel: boolean;
19+
onPanelClose?: () => void;
20+
onChatClose?: () => void;
1821
}
1922

2023
interface PanelSplitHandleProps {
@@ -40,7 +43,12 @@ interface UsePanelSplitResult {
4043
// CSS never re-clamps. Keeps two state atoms — the user's preferred content
4144
// width (persisted) and the measured container width — and derives everything
4245
// else.
43-
export function usePanelSplit( { showPanel, siteId }: UsePanelSplitOptions ): UsePanelSplitResult {
46+
export function usePanelSplit( {
47+
showPanel,
48+
siteId,
49+
onPanelClose,
50+
onChatClose,
51+
}: UsePanelSplitOptions ): UsePanelSplitResult {
4452
const rootRef = useRef< HTMLDivElement >( null );
4553

4654
// `preferredContentWidth` is the user's intent (persisted); the displayed
@@ -145,11 +153,28 @@ export function usePanelSplit( { showPanel, siteId }: UsePanelSplitOptions ): Us
145153
);
146154
// The content column is anchored to the inline-start edge, so in RTL a
147155
// rightward drag shrinks it instead of growing it.
148-
const layout = getPanelSplitLayout( container, start + ( isRTL() ? -deltaX : deltaX ) );
156+
const next = start + ( isRTL() ? -deltaX : deltaX );
157+
const layout = getPanelSplitLayout( container, next );
149158
setPreferred( layout.contentWidth );
150-
return layout.contentWidth;
159+
return next;
160+
},
161+
onCommit: ( latest ) => {
162+
const container = measureRootWidth();
163+
if ( container === null ) return;
164+
const layout = getPanelSplitLayout( container, latest );
165+
const close =
166+
latest >= container - layout.panelMinWidth + PANEL_COLLAPSE_OVERSHOOT
167+
? onPanelClose
168+
: latest <= container - layout.panelMaxWidth - PANEL_COLLAPSE_OVERSHOOT
169+
? onChatClose
170+
: undefined;
171+
if ( close ) {
172+
setPreferred( preferredContentWidth ?? PANEL_CONTENT_DEFAULT_WIDTH );
173+
close();
174+
return;
175+
}
176+
persistContentWidth( layout.contentWidth );
151177
},
152-
onCommit: ( latest ) => persistContentWidth( latest ),
153178
} );
154179

155180
useLayoutEffect( () => {

‎apps/ui/src/router/layout-dashboard/index.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ function DashboardLayout() {
479479
}
480480
panelOpen={ showPanel }
481481
onPanelClose={ () => setPanelOpen( false, { userInitiated: true } ) }
482+
onChatClose={ () => setPanelFullscreen( true, { userInitiated: true } ) }
482483
chatHidden={ chatHidden }
483484
instant={ ( ! previewStopping || ! preview.open ) && preview.instant }
484485
panel={ renderPanel }

0 commit comments

Comments
 (0)