-
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
Access global styles from the post editor #46894
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 |
---|---|---|
|
@@ -348,6 +348,10 @@ _Returns_ | |
|
||
Undocumented declaration. | ||
|
||
### findInPresetsBy | ||
|
||
Undocumented declaration. | ||
|
||
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. I'll add docs for this once we've determined that this is, in fact, the best place for these utils 😅 |
||
### FontSizePicker | ||
|
||
_Related_ | ||
|
@@ -527,6 +531,35 @@ _Returns_ | |
|
||
- `Object`: Typography block support derived CSS classes & styles. | ||
|
||
### getTypographyFontSizeValue | ||
|
||
Returns a font-size value based on a given font-size preset. | ||
Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values. | ||
|
||
_Parameters_ | ||
|
||
- _preset_ `Preset`: | ||
- _typographySettings_ `Object`: | ||
- _typographySettings.fluid_ `boolean|TypographySettings`: Whether fluid typography is enabled, and, optionally, fluid font size options. | ||
|
||
_Returns_ | ||
|
||
- `string|*`: A font-size value or the value of preset.size. | ||
|
||
### getValueFromVariable | ||
|
||
Attempts to fetch the value of a theme.json CSS variable. | ||
|
||
_Parameters_ | ||
|
||
- _features_ `Object`: GlobalStylesContext config, e.g., user, base or merged. Represents the theme.json tree. | ||
- _blockName_ `string`: The name of a block as represented in the styles property. E.g., 'root' for root-level, and 'core/${blockName}' for blocks. | ||
- _variable_ `string|*`: An incoming style value. A CSS var value is expected, but it could be any value. | ||
|
||
_Returns_ | ||
|
||
- `string|*|{ref}`: The value of the CSS var, if found. If not found, the passed variable argument. | ||
|
||
### InnerBlocks | ||
|
||
_Related_ | ||
|
@@ -616,6 +649,10 @@ _Related_ | |
|
||
- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/plain-text/README.md> | ||
|
||
### PRESET_METADATA | ||
|
||
Undocumented declaration. | ||
|
||
### RichText | ||
|
||
_Related_ | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||
/** | ||||
* External dependencies | ||||
*/ | ||||
import { get } from 'lodash'; | ||||
|
||||
/** | ||||
* WordPress dependencies | ||||
*/ | ||||
import { useSelect } from '@wordpress/data'; | ||||
import { useMemo } from '@wordpress/element'; | ||||
|
||||
/** | ||||
* Internal dependencies | ||||
*/ | ||||
import { useBlockEditContext } from '../block-edit'; | ||||
import { store as blockEditorStore } from '../../store'; | ||||
import { getValueFromVariable } from '../../utils/style-variable-resolution'; | ||||
|
||||
/** | ||||
* Hook that retrieves the global styles of a block. | ||||
* It works with nested objects using by finding the value at path. | ||||
* | ||||
* @param {string|Array} path The path to the setting. | ||||
* | ||||
* @return {any} Returns the style value defined for the path. | ||||
* | ||||
* @example | ||||
* ```js | ||||
* const backgroundColor = useStyle( 'color.background' ); | ||||
* ``` | ||||
*/ | ||||
export default function useStyle( path ) { | ||||
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. If you squint this looks the same as the
|
||||
const { name: blockName } = useBlockEditContext(); | ||||
|
||||
const settings = useSelect( ( select ) => { | ||||
return select( blockEditorStore ).getSettings(); | ||||
}, [] ); | ||||
const stylesForBlock = get( settings, [ | ||||
'__experimentalStyles', | ||||
'blocks', | ||||
blockName, | ||||
] ); | ||||
const value = get( stylesForBlock, path ); | ||||
return useMemo( () => { | ||||
return getValueFromVariable( | ||||
settings.__experimentalFeatures, | ||||
blockName, | ||||
value | ||||
); | ||||
}, [ settings.__experimentalFeatures, blockName, value ] ); | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export { default as transformStyles } from './transform-styles'; | ||
export * from './block-variation-transforms'; | ||
export { default as getPxFromCssUnit } from './parse-css-unit-to-px'; | ||
export { | ||
PRESET_METADATA, | ||
findInPresetsBy, | ||
getValueFromVariable, | ||
} from './style-variable-resolution'; | ||
export { getTypographyFontSizeValue } from './typography'; | ||
Comment on lines
+5
to
+9
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. I think these functions are too low level to export as |
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.
What is this change for? I don't see this REST API endpoint used in this PR.