-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block editor: add consolidated styles to store #34156
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockEditContext } from '../block-edit'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
/** | ||
* // TODO This is an example targeted at specific style properties. We could consolidate at the highest level, e.g., return a merged styles object for all in packages/block-editor/src/hooks/style.js | ||
* Hook that retrieves consolidated styles from the block editor store, | ||
* and merges them with incoming user styles for a particular property. | ||
* | ||
* @param {Object} userStyles User-selected styles from the editor. | ||
* @param {string} styleProperty Targets a specific property. If not passed then we return the entire merged object. | ||
* | ||
* @return {Object} The merged user styles, or original user styles if no consolidated styles. | ||
* | ||
* @example | ||
* ```js | ||
* const consolidatedStyles = useConsolidatedStyles( props.attributes?.style, 'border' ); | ||
* ``` | ||
*/ | ||
export default function useConsolidatedStyles( userStyles, styleProperty ) { | ||
const { name: blockName } = useBlockEditContext(); | ||
|
||
const consolidatedStyles = useSelect( | ||
( select ) => { | ||
const { getSettings } = select( blockEditorStore ); | ||
const consolidatedBlockStyles = | ||
getSettings().__experimentalStyles?.blocks || {}; | ||
|
||
return consolidatedBlockStyles && | ||
consolidatedBlockStyles[ blockName ] | ||
? consolidatedBlockStyles[ blockName ] | ||
: undefined; | ||
}, | ||
[ blockName, styleProperty ] | ||
); | ||
|
||
if ( ! userStyles || ! styleProperty ) { | ||
return userStyles; | ||
} | ||
|
||
if ( consolidatedStyles ) { | ||
return { | ||
...userStyles, | ||
[ styleProperty ]: { | ||
...consolidatedStyles[ styleProperty ], | ||
...userStyles[ styleProperty ], | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't take into account that some values will be Nor have I asked the question whether we should take it into account at all. If yes, we could see if pre-filling with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Turns out we can access CSS var values in presets: https://github.com/WordPress/gutenberg/pull/34178/files#diff-c7f239b002e1dfa2d70dc9ac84af4975a6c1cd63d00f0c14e4510c5a6a9ecc93 |
||
}; | ||
} | ||
|
||
return userStyles; | ||
} |
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.
TODO: memoize this