-
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.
Add: Styles section to the browse mode sidebar.
- Loading branch information
1 parent
0ce63b4
commit dea59c1
Showing
7 changed files
with
202 additions
and
130 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
136 changes: 136 additions & 0 deletions
136
packages/edit-site/src/components/global-styles/style-variations-container.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,136 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import fastDeepEqual from 'fast-deep-equal/es6'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo, useContext, useState } from '@wordpress/element'; | ||
import { ENTER } from '@wordpress/keycodes'; | ||
import { __experimentalGrid as Grid } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { mergeBaseAndUserConfigs } from './global-styles-provider'; | ||
import StylesPreview from './preview'; | ||
import { unlock } from '../../private-apis'; | ||
|
||
const { GlobalStylesContext } = unlock( blockEditorPrivateApis ); | ||
|
||
function compareVariations( a, b ) { | ||
return ( | ||
fastDeepEqual( a.styles, b.styles ) && | ||
fastDeepEqual( a.settings, b.settings ) | ||
); | ||
} | ||
|
||
function Variation( { variation } ) { | ||
const [ isFocused, setIsFocused ] = useState( false ); | ||
const { base, user, setUserConfig } = useContext( GlobalStylesContext ); | ||
const context = useMemo( () => { | ||
return { | ||
user: { | ||
settings: variation.settings ?? {}, | ||
styles: variation.styles ?? {}, | ||
}, | ||
base, | ||
merged: mergeBaseAndUserConfigs( base, variation ), | ||
setUserConfig: () => {}, | ||
}; | ||
}, [ variation, base ] ); | ||
|
||
const selectVariation = () => { | ||
setUserConfig( () => { | ||
return { | ||
settings: variation.settings, | ||
styles: variation.styles, | ||
}; | ||
} ); | ||
}; | ||
|
||
const selectOnEnter = ( event ) => { | ||
if ( event.keyCode === ENTER ) { | ||
event.preventDefault(); | ||
selectVariation(); | ||
} | ||
}; | ||
|
||
const isActive = useMemo( () => { | ||
return compareVariations( user, variation ); | ||
}, [ user, variation ] ); | ||
|
||
return ( | ||
<GlobalStylesContext.Provider value={ context }> | ||
<div | ||
className={ classnames( | ||
'edit-site-global-styles-variations_item', | ||
{ | ||
'is-active': isActive, | ||
} | ||
) } | ||
role="button" | ||
onClick={ selectVariation } | ||
onKeyDown={ selectOnEnter } | ||
tabIndex="0" | ||
aria-label={ variation?.title } | ||
aria-current={ isActive } | ||
onFocus={ () => setIsFocused( true ) } | ||
onBlur={ () => setIsFocused( false ) } | ||
> | ||
<div className="edit-site-global-styles-variations_item-preview"> | ||
<StylesPreview | ||
label={ variation?.title } | ||
isFocused={ isFocused } | ||
withHoverView | ||
/> | ||
</div> | ||
</div> | ||
</GlobalStylesContext.Provider> | ||
); | ||
} | ||
|
||
export default function StyleVariationsContainer() { | ||
const { variations } = useSelect( ( select ) => { | ||
return { | ||
variations: | ||
select( | ||
coreStore | ||
).__experimentalGetCurrentThemeGlobalStylesVariations() || [], | ||
}; | ||
}, [] ); | ||
|
||
const withEmptyVariation = useMemo( () => { | ||
return [ | ||
{ | ||
title: __( 'Default' ), | ||
settings: {}, | ||
styles: {}, | ||
}, | ||
...variations.map( ( variation ) => ( { | ||
...variation, | ||
settings: variation.settings ?? {}, | ||
styles: variation.styles ?? {}, | ||
} ) ), | ||
]; | ||
}, [ variations ] ); | ||
|
||
return ( | ||
<> | ||
<Grid | ||
columns={ 2 } | ||
className="edit-site-global-styles-style-variations-container" | ||
> | ||
{ withEmptyVariation?.map( ( variation, index ) => ( | ||
<Variation key={ index } variation={ variation } /> | ||
) ) } | ||
</Grid> | ||
</> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/edit-site/src/components/sidebar-navigation-screen-global-styles/index.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,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
import StyleVariationsContainer from '../global-styles/style-variations-container'; | ||
|
||
export default function SidebarNavigationScreenGlobalStyles() { | ||
return ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Styles' ) } | ||
description={ __( | ||
'Choose a different style combination for the theme styles.' | ||
) } | ||
content={ <StyleVariationsContainer /> } | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.