-
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.
- Loading branch information
1 parent
fb98795
commit 8f85b7d
Showing
35 changed files
with
761 additions
and
683 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
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> | ||
} | ||
``` | ||
|
||
## useStyle | ||
|
||
A react hook used to retrieve the style applied to a given context. | ||
|
||
```js | ||
import { useStyle } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
// Text color for the site root. | ||
const [ color, setColor ] = useStyle( 'color.text' ); | ||
|
||
// The user modified color for the core paragraph block. | ||
const [ pColor, setPColor ] = useStyle( 'color.text', 'core/paragraph', 'user' ); | ||
|
||
return "Something"; | ||
} | ||
``` | ||
|
||
## useSetting | ||
|
||
A react hook used to retrieve the setting applied to a given context. | ||
|
||
```js | ||
import { useGlobalStylesSetting as useSetting } from '@wordpress/block-editor'; | ||
|
||
function MyComponent() { | ||
// The default color palette. | ||
const [ colorPalette, setColorPalette ] = useSetting( '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 ] = useSetting( '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 useSetting( 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 useStyle( 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,8 @@ | ||
// TODO: Should probably __experimental these. | ||
export { | ||
useGlobalStylesReset, | ||
useStyle, | ||
useSetting as useGlobalStylesSetting, // TODO: Naming conflict with useSetting from @wordpress/block-editor. Are they the same function? | ||
} from './hooks'; | ||
export { useGlobalStylesOutput } from './use-global-styles-output'; | ||
export { GlobalStylesContext } from './context'; |
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.