-
Notifications
You must be signed in to change notification settings - Fork 86
Add resizable sidebar #2853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add resizable sidebar #2853
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5ee4c58
Add resizable sidebar design spec
shaunandrews e64c811
Update resizable sidebar spec after review
shaunandrews 6944938
Add resizable sidebar with drag handle and persistence
shaunandrews f9f43cb
Remove design spec from PR
shaunandrews da52e8f
Increase sidebar min width and prevent footer text wrapping
shaunandrews 05815db
Merge branch 'trunk' of github.com:Automattic/studio into resizable-s…
6caa470
Add shared utility function
e66bf69
Merge branch 'trunk' into resizable-sidebar
katinthehatsite 0a67d45
Merge branch 'trunk' of github.com:Automattic/studio into resizable-s…
6f36355
Remove snap behaviour
29d3808
Merge branch 'trunk' of github.com:Automattic/studio into resizable-s…
401dfa4
Undo the unintended changes
a12a4ab
Apply suggestion from @nightnei
katinthehatsite ea4327b
Fix linter
10fae44
Merge branch 'resizable-sidebar' of github.com:Automattic/studio into…
e00c4ff
Fix linter
e39dfed
Update apps/studio/src/components/app.tsx
katinthehatsite 4b44fa3
Merge branch 'trunk' into resizable-sidebar
katinthehatsite 16a9189
Merge branch 'trunk' into resizable-sidebar
katinthehatsite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { useState, useCallback, useRef, useEffect } from 'react'; | ||
| import { | ||
| SIDEBAR_MIN_WIDTH, | ||
| SIDEBAR_MAX_WIDTH, | ||
| LOCAL_STORAGE_SIDEBAR_WIDTH_KEY, | ||
| } from 'src/constants'; | ||
| import { getSavedSidebarWidth } from 'src/lib/sidebar-utils'; | ||
|
|
||
| function saveWidth( width: number ) { | ||
| localStorage.setItem( LOCAL_STORAGE_SIDEBAR_WIDTH_KEY, String( width ) ); | ||
| } | ||
|
|
||
| export function useSidebarResize( isSidebarVisible: boolean, toggleSidebar: () => void ) { | ||
| const [ sidebarWidth, setSidebarWidth ] = useState( getSavedSidebarWidth ); | ||
| const [ isDragging, setIsDragging ] = useState( false ); | ||
| const dragStartX = useRef( 0 ); | ||
| const dragStartWidth = useRef( 0 ); | ||
|
|
||
| const handleMouseDown = useCallback( | ||
| ( e: React.MouseEvent ) => { | ||
| e.preventDefault(); | ||
| setIsDragging( true ); | ||
| dragStartX.current = e.clientX; | ||
| dragStartWidth.current = isSidebarVisible ? sidebarWidth : 0; | ||
| }, | ||
| [ sidebarWidth, isSidebarVisible ] | ||
| ); | ||
|
|
||
| useEffect( () => { | ||
| if ( ! isDragging ) { | ||
| return; | ||
| } | ||
|
|
||
| let rafId: number; | ||
|
|
||
| const handleMouseMove = ( e: MouseEvent ) => { | ||
| cancelAnimationFrame( rafId ); | ||
| rafId = requestAnimationFrame( () => { | ||
| const delta = e.clientX - dragStartX.current; | ||
| const newWidth = dragStartWidth.current + delta; | ||
| setSidebarWidth( Math.min( SIDEBAR_MAX_WIDTH, Math.max( SIDEBAR_MIN_WIDTH, newWidth ) ) ); | ||
| } ); | ||
| }; | ||
|
|
||
| const handleMouseUp = ( e: MouseEvent ) => { | ||
| setIsDragging( false ); | ||
| cancelAnimationFrame( rafId ); | ||
|
|
||
| const delta = e.clientX - dragStartX.current; | ||
| const finalWidth = dragStartWidth.current + delta; | ||
| const clampedWidth = Math.min( SIDEBAR_MAX_WIDTH, Math.max( SIDEBAR_MIN_WIDTH, finalWidth ) ); | ||
| setSidebarWidth( clampedWidth ); | ||
| saveWidth( clampedWidth ); | ||
|
|
||
| if ( ! isSidebarVisible ) { | ||
| toggleSidebar(); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener( 'mousemove', handleMouseMove ); | ||
| document.addEventListener( 'mouseup', handleMouseUp ); | ||
|
|
||
| return () => { | ||
| cancelAnimationFrame( rafId ); | ||
| document.removeEventListener( 'mousemove', handleMouseMove ); | ||
| document.removeEventListener( 'mouseup', handleMouseUp ); | ||
| }; | ||
| }, [ isDragging, isSidebarVisible, toggleSidebar ] ); | ||
|
|
||
| return { | ||
| sidebarWidth, | ||
| isDragging, | ||
| handleMouseDown, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { | ||
| SIDEBAR_WIDTH, | ||
| SIDEBAR_MIN_WIDTH, | ||
| SIDEBAR_MAX_WIDTH, | ||
| LOCAL_STORAGE_SIDEBAR_WIDTH_KEY, | ||
| } from 'src/constants'; | ||
|
|
||
| export function getSavedSidebarWidth(): number { | ||
| const saved = localStorage.getItem( LOCAL_STORAGE_SIDEBAR_WIDTH_KEY ); | ||
| if ( saved ) { | ||
| const parsed = Number( saved ); | ||
| if ( ! isNaN( parsed ) && parsed >= SIDEBAR_MIN_WIDTH && parsed <= SIDEBAR_MAX_WIDTH ) { | ||
| return parsed; | ||
| } | ||
| } | ||
| return SIDEBAR_WIDTH; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we decide on a local storage approach?
We already have some UI properties (window size) living in the
app.jsonconfig file.I don't have anything against local storage, I just believe it should be consistent with our other properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have the answer specifically for that bit since it was not originally my PR but it makes sense to be consistent with our other properties. I will check how we can align our approach here 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a chance to check this a bit further and I think the localStorage approach would be all right here. The sidebar width uses
localStoragerather than app.json because it's renderer-side state: it's read synchronously at component init (allowing it to be passed directly as a useState initializer) and never needed by the Main process.windowBoundslives inapp.jsonbecause Electron's Main process must restore the window size before the renderer exists. Moving sidebar width toapp.jsonwould introduce an async IPC to initialize a CSS value, requiring a loading state and added complexity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes a lot of sense, thanks for the explanation! 👍