-
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
Move Global Styles APIs to @wordpress/block-editor #47098
Merged
noisysocks
merged 4 commits into
trunk
from
try/move-global-styles-apis-to-block-editor
Jan 20, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
78 changes: 78 additions & 0 deletions
78
packages/block-editor/src/components/global-styles/README.md
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,78 @@ | ||
# Global Styles | ||
|
||
This folder contains all the necessary APIs to manipulate the global styles data. It can be potentially extracted to its own package. | ||
|
||
# Available public APIs | ||
|
||
## useGlobalStylesReset | ||
|
||
A React hook used to retrieve whether the Global Styles have been edited and a callback to reset to the default theme values. | ||
|
||
```js | ||
import { useGlobalStylesReset } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
const [ canReset, reset ] = useGlobalStylesReset(); | ||
|
||
return canReset | ||
? <Button onClick={ () => reset() }>Reset</Button> | ||
: null; | ||
} | ||
``` | ||
|
||
## useGlobalStylesOutput | ||
|
||
A React hook used to retrieve the styles array and settings to provide for block editor instances based on the current global styles. | ||
|
||
```js | ||
import { useGlobalStylesOutput, BlockEditorProvider, BlockList } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
const [ styles, settings ] = useGlobalStylesOutput(); | ||
|
||
return <BlockEditorProvider settings={{ | ||
styles, | ||
__experimentalFeatures: settings | ||
}}> | ||
<BlockList /> | ||
</BlockEditorProvider> | ||
} | ||
``` | ||
|
||
## useGlobalStyle | ||
|
||
A react hook used to retrieve the style applied to a given context. | ||
|
||
```js | ||
import { useGlobalStyle } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
// Text color for the site root. | ||
const [ color, setColor ] = useGlobalStyle( 'color.text' ); | ||
|
||
// The user modified color for the core paragraph block. | ||
const [ pColor, setPColor ] = useGlobalStyle( 'color.text', 'core/paragraph', 'user' ); | ||
|
||
return "Something"; | ||
} | ||
``` | ||
|
||
## useGlobalSetting | ||
|
||
A react hook used to retrieve the setting applied to a given context. | ||
|
||
```js | ||
import { useGlobalSetting } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
// The default color palette. | ||
const [ colorPalette, setColorPalette ] = useGlobalSetting( 'color.palette' ); | ||
|
||
// The base (theme + core) color palette for the paragraph block, | ||
// ignoring user provided palette. | ||
// If the palette is not defined for the paragraph block, the root one is returned. | ||
const [ pColor, setPColor ] = useGlobalSetting( 'color.palette', 'core/paragraph', 'base' ); | ||
|
||
return "Something"; | ||
} | ||
``` |
File renamed without changes.
157 changes: 157 additions & 0 deletions
157
packages/block-editor/src/components/global-styles/hooks.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,157 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import fastDeepEqual from 'fast-deep-equal/es6'; | ||
import { get, set } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext, useCallback } from '@wordpress/element'; | ||
import { __EXPERIMENTAL_PATHS_WITH_MERGE as PATHS_WITH_MERGE } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getValueFromVariable, getPresetVariableFromValue } from './utils'; | ||
import { GlobalStylesContext } from './context'; | ||
|
||
const EMPTY_CONFIG = { settings: {}, styles: {} }; | ||
|
||
export const useGlobalStylesReset = () => { | ||
const { user: config, setUserConfig } = useContext( GlobalStylesContext ); | ||
const canReset = !! config && ! fastDeepEqual( config, EMPTY_CONFIG ); | ||
return [ | ||
canReset, | ||
useCallback( | ||
() => setUserConfig( () => EMPTY_CONFIG ), | ||
[ setUserConfig ] | ||
), | ||
]; | ||
}; | ||
|
||
export function useGlobalSetting( path, blockName, source = 'all' ) { | ||
const { | ||
merged: mergedConfig, | ||
base: baseConfig, | ||
user: userConfig, | ||
setUserConfig, | ||
} = useContext( GlobalStylesContext ); | ||
|
||
const fullPath = ! blockName | ||
? `settings.${ path }` | ||
: `settings.blocks.${ blockName }.${ path }`; | ||
|
||
const setSetting = ( newValue ) => { | ||
setUserConfig( ( currentConfig ) => { | ||
// Deep clone `currentConfig` to avoid mutating it later. | ||
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) ); | ||
const pathToSet = PATHS_WITH_MERGE[ path ] | ||
? fullPath + '.custom' | ||
: fullPath; | ||
set( newUserConfig, pathToSet, newValue ); | ||
|
||
return newUserConfig; | ||
} ); | ||
}; | ||
|
||
const getSettingValueForContext = ( name ) => { | ||
const currentPath = ! name | ||
? `settings.${ path }` | ||
: `settings.blocks.${ name }.${ path }`; | ||
|
||
const getSettingValue = ( configToUse ) => { | ||
const result = get( configToUse, currentPath ); | ||
if ( PATHS_WITH_MERGE[ path ] ) { | ||
return result?.custom ?? result?.theme ?? result?.default; | ||
} | ||
return result; | ||
}; | ||
|
||
let result; | ||
switch ( source ) { | ||
case 'all': | ||
result = getSettingValue( mergedConfig ); | ||
break; | ||
case 'user': | ||
result = getSettingValue( userConfig ); | ||
break; | ||
case 'base': | ||
result = getSettingValue( baseConfig ); | ||
break; | ||
default: | ||
throw 'Unsupported source'; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// Unlike styles settings get inherited from top level settings. | ||
const resultWithFallback = | ||
getSettingValueForContext( blockName ) ?? getSettingValueForContext(); | ||
|
||
return [ resultWithFallback, setSetting ]; | ||
} | ||
|
||
export function useGlobalStyle( path, blockName, source = 'all' ) { | ||
const { | ||
merged: mergedConfig, | ||
base: baseConfig, | ||
user: userConfig, | ||
setUserConfig, | ||
} = useContext( GlobalStylesContext ); | ||
const finalPath = ! blockName | ||
? `styles.${ path }` | ||
: `styles.blocks.${ blockName }.${ path }`; | ||
|
||
const setStyle = ( newValue ) => { | ||
setUserConfig( ( currentConfig ) => { | ||
// Deep clone `currentConfig` to avoid mutating it later. | ||
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) ); | ||
set( | ||
newUserConfig, | ||
finalPath, | ||
getPresetVariableFromValue( | ||
mergedConfig.settings, | ||
blockName, | ||
path, | ||
newValue | ||
) | ||
); | ||
return newUserConfig; | ||
} ); | ||
}; | ||
|
||
let result; | ||
switch ( source ) { | ||
case 'all': | ||
result = getValueFromVariable( | ||
mergedConfig, | ||
blockName, | ||
// The stlyes.css path is allowed to be empty, so don't revert to base if undefined. | ||
finalPath === 'styles.css' | ||
? get( userConfig, finalPath ) | ||
: get( userConfig, finalPath ) ?? | ||
get( baseConfig, finalPath ) | ||
); | ||
break; | ||
case 'user': | ||
result = getValueFromVariable( | ||
mergedConfig, | ||
blockName, | ||
get( userConfig, finalPath ) | ||
); | ||
break; | ||
case 'base': | ||
result = getValueFromVariable( | ||
baseConfig, | ||
blockName, | ||
get( baseConfig, finalPath ) | ||
); | ||
break; | ||
default: | ||
throw 'Unsupported source'; | ||
} | ||
|
||
return [ result, setStyle ]; | ||
} |
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,7 @@ | ||
export { | ||
useGlobalStylesReset, | ||
useGlobalSetting, | ||
useGlobalStyle, | ||
} from './hooks'; | ||
export { useGlobalStylesOutput } from './use-global-styles-output'; | ||
export { GlobalStylesContext } from './context'; | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we want to give the whole context object as an API or just the "provider" for external consumers.
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.
There's a few places where we need to read the raw context value e.g. variations.
gutenberg/packages/edit-site/src/components/global-styles/screen-style-variations.js
Line 45 in 64c6912