-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Zoom out: Invoke zoom out mode when opening the patterns tab, and move the code to do so to a shared hook #59775
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { | |
myPatternsCategory, | ||
INSERTER_PATTERN_TYPES, | ||
} from './utils'; | ||
import { useZoomOut } from '../../../hooks/use-zoom-out'; | ||
|
||
const noop = () => {}; | ||
|
||
|
@@ -49,6 +50,10 @@ export function PatternCategoryPreviews( { | |
const [ patternSyncFilter, setPatternSyncFilter ] = useState( 'all' ); | ||
const [ patternSourceFilter, setPatternSourceFilter ] = useState( 'all' ); | ||
|
||
// Move to zoom out mode when this component is mounted | ||
// and back to the previous mode when unmounted. | ||
useZoomOut(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd much rather this not be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes I too dislike this hook 😁 |
||
|
||
const availableCategories = usePatternCategories( | ||
rootClientId, | ||
patternSourceFilter | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
|
||
/** | ||
* A hook used to set the editor mode to zoomed out mode, invoking the hook sets the mode. | ||
*/ | ||
export function useZoomOut() { | ||
const { __unstableSetEditorMode } = useDispatch( blockEditorStore ); | ||
const { mode } = useSelect( ( select ) => { | ||
return { | ||
mode: select( blockEditorStore ).__unstableGetEditorMode(), | ||
}; | ||
}, [] ); | ||
|
||
const shouldRevertInitialMode = useRef( null ); | ||
useEffect( () => { | ||
// ignore changes to zoom-out mode as we explictily change to it on mount. | ||
if ( mode !== 'zoom-out' ) { | ||
shouldRevertInitialMode.current = false; | ||
draganescu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, [ mode ] ); | ||
|
||
// Intentionality left without any dependency. | ||
// This effect should only run the first time the component is rendered. | ||
// The effect opens the zoom-out view if it is not open before when applying a style variation. | ||
useEffect( () => { | ||
if ( mode !== 'zoom-out' ) { | ||
__unstableSetEditorMode( 'zoom-out' ); | ||
shouldRevertInitialMode.current = true; | ||
return () => { | ||
// if there were not mode changes revert to the initial mode when unmounting. | ||
if ( shouldRevertInitialMode.current ) { | ||
__unstableSetEditorMode( mode ); | ||
} | ||
}; | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [] ); | ||
} |
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.
This should have been exported as a
privateAPI
instead of being public.