-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Global Styles APIs to @wordpress/block-editor (#47098)
* Move things around * lock() the Global Styles APIs * Update package-lock.json * Move tests to block-editor
- Loading branch information
1 parent
64c6912
commit 77c6c5d
Showing
44 changed files
with
1,068 additions
and
815 deletions.
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.