forked from Automattic/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sidebar intelligent responsive show/hide it when crosses the breakpoi…
…nt (Automattic#536) * Keep window size and just add or subscract sidebar width * Show hide sidebar when widnow crosses the breakpoint
- Loading branch information
Showing
5 changed files
with
51 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { DEFAULT_WIDTH } from '../constants'; | ||
import { getIpcApi } from '../lib/get-ipc-api'; | ||
|
||
const SIDEBAR_BREAKPOINT = DEFAULT_WIDTH; | ||
|
||
export function useSidebarVisibility() { | ||
const [ isSidebarVisible, setIsSidebarVisible ] = useState( true ); | ||
const [ isLowerThanBreakpoint, setIsLowerThanBreakpoint ] = useState( false ); | ||
|
||
useEffect( () => { | ||
const handleResize = () => { | ||
setIsLowerThanBreakpoint( window.innerWidth < SIDEBAR_BREAKPOINT ); | ||
}; | ||
window.addEventListener( 'resize', handleResize ); | ||
return () => { | ||
window.removeEventListener( 'resize', handleResize ); | ||
}; | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( isLowerThanBreakpoint ) { | ||
setIsSidebarVisible( false ); | ||
} else { | ||
setIsSidebarVisible( true ); | ||
} | ||
}, [ isLowerThanBreakpoint ] ); | ||
|
||
const toggleSidebar = useCallback( () => { | ||
getIpcApi().toggleMinWindowWidth( isSidebarVisible ); | ||
setIsSidebarVisible( ! isSidebarVisible ); | ||
}, [ isSidebarVisible ] ); | ||
|
||
return { isSidebarVisible, toggleSidebar }; | ||
} |
This file contains 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 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