-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't load isSidebarOpened preference from localStorage on mobile.
- Loading branch information
1 parent
1fb6853
commit cd2f040
Showing
5 changed files
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import scssVariables from '!!sass-variables-loader!./assets/stylesheets/_variables.scss'; | ||
|
||
export const BREAK_HUGE = parseInt( scssVariables.breakHuge ); | ||
export const BREAK_WIDE = parseInt( scssVariables.breakWide ); | ||
export const BREAK_LARGE = parseInt( scssVariables.breakLarge ); | ||
export const BREAK_MEDIUM = parseInt( scssVariables.breakMedium ); | ||
export const BREAK_SMALL = parseInt( scssVariables.breakSmall ); | ||
export const BREAK_MOBILE = parseInt( scssVariables.breakMobile ); | ||
|
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,37 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { BREAK_MEDIUM } from '../../constants'; | ||
|
||
/** | ||
* Checks if we are in a mobile resolution using window.innerWidth if available | ||
* | ||
* @return {Boolean} Returns true if on mobile resolution and false if on non mobile or impossible to check. | ||
*/ | ||
const isMobileChecker = () => 'object' === typeof window && window.innerWidth < BREAK_MEDIUM; | ||
|
||
/** | ||
* Disables isSidebarOpened on rehydrate payload if the user is on a mobile screen size. | ||
* | ||
* @param {Object} payload rehydrate payload | ||
* @param {Boolean} isMobile flag indicating if executing on mobile screen sizes or not | ||
* | ||
* @return {Object} rehydrate payload with isSidebarOpened disabled if on mobile | ||
*/ | ||
export const disableIsSidebarOpenedOnMobile = ( payload, isMobile = isMobileChecker() ) => ( | ||
isMobile ? { ...payload, isSidebarOpened: false } : payload | ||
); | ||
|
||
/** | ||
* Middleware | ||
*/ | ||
|
||
export const mobileMiddleware = () => next => action => { | ||
if ( action.type === 'REDUX_REHYDRATE' ) { | ||
return next( { | ||
type: 'REDUX_REHYDRATE', | ||
payload: disableIsSidebarOpenedOnMobile( action.payload ), | ||
} ); | ||
} | ||
return next( action ); | ||
}; |
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,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { disableIsSidebarOpenedOnMobile } from '../'; | ||
|
||
describe( 'disableIsSidebarOpenOnMobile()', () => { | ||
it( 'should disable isSidebarOpen on mobile and keep other properties as before', () => { | ||
const input = { | ||
isSidebarOpened: true, | ||
dummyPref: 'dummy', | ||
}, | ||
output = { | ||
isSidebarOpened: false, | ||
dummyPref: 'dummy', | ||
}, | ||
isMobile = true; | ||
|
||
expect( disableIsSidebarOpenedOnMobile( input, isMobile ) ).toEqual( output ); | ||
} ); | ||
|
||
it( 'should keep isSidebarOpen on non-mobile and keep other properties as before', () => { | ||
const input = { | ||
isSidebarOpened: true, | ||
dummy: 'non-dummy', | ||
}, | ||
output = { | ||
isSidebarOpened: true, | ||
dummy: 'non-dummy', | ||
}, | ||
isMobile = false; | ||
expect( disableIsSidebarOpenedOnMobile( input, isMobile ) ).toEqual( output ); | ||
} ); | ||
} ); |
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