-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracting style book and adding context props
- Loading branch information
Showing
5 changed files
with
188 additions
and
56 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
98 changes: 98 additions & 0 deletions
98
packages/edit-site/src/components/global-styles/style-book.js
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,98 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
import { useNavigator } from '@wordpress/components'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
import StyleBook from '../style-book'; | ||
import { STYLE_BOOK_COLOR_GROUPS } from '../style-book/constants'; | ||
|
||
const { useLocation, useHistory } = unlock( routerPrivateApis ); | ||
|
||
function useGetCanvasContext() { | ||
// View mode browser query params and location. | ||
const { path: locationPath, query } = useLocation(); | ||
const history = useHistory(); | ||
|
||
// Edit mode (editor) Global styles menu router. | ||
const { location, goTo } = useNavigator(); | ||
|
||
return useMemo( () => { | ||
if ( 'edit' === query?.canvas && location?.path ) { | ||
return [ | ||
location.path ?? '/', | ||
( updatedSection ) => goTo( updatedSection ), | ||
{}, | ||
]; | ||
} | ||
|
||
return [ | ||
query?.section ?? '/', | ||
( updatedSection ) => | ||
history.navigate( | ||
addQueryArgs( locationPath, { | ||
section: updatedSection, | ||
} ) | ||
), | ||
{ | ||
enableResizing: false, | ||
showCloseButton: false, | ||
showTabs: false, | ||
}, | ||
]; | ||
}, [ | ||
locationPath, | ||
location?.path, | ||
query?.section, | ||
query?.canvas, | ||
history, | ||
goTo, | ||
] ); | ||
} | ||
|
||
export default function GlobalStylesStyleBook( props ) { | ||
const [ path, onChangeSection, contextProps ] = useGetCanvasContext(); | ||
|
||
return ( | ||
<StyleBook | ||
{ ...contextProps } | ||
{ ...props } | ||
isSelected={ ( blockName ) => | ||
// Match '/blocks/core%2Fbutton' and | ||
// '/blocks/core%2Fbutton/typography', but not | ||
// '/blocks/core%2Fbuttons'. | ||
path === `/blocks/${ encodeURIComponent( blockName ) }` || | ||
path.startsWith( | ||
`/blocks/${ encodeURIComponent( blockName ) }/` | ||
) | ||
} | ||
onSelect={ ( blockName ) => { | ||
if ( | ||
STYLE_BOOK_COLOR_GROUPS.find( | ||
( group ) => group.slug === blockName | ||
) | ||
) { | ||
// Go to color palettes Global Styles. | ||
onChangeSection( '/colors/palette' ); | ||
return; | ||
} | ||
if ( blockName === 'typography' ) { | ||
// Go to typography Global Styles. | ||
onChangeSection( '/typography' ); | ||
return; | ||
} | ||
|
||
// Now go to the selected block. | ||
onChangeSection( | ||
`/blocks/${ encodeURIComponent( blockName ) }` | ||
); | ||
} } | ||
/> | ||
); | ||
} |
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