@wordpress/global-styles-engine 是一个用于读写全局样式数据结构(theme.json 格式)的通用库。它提供函数来操作全局样式对象,处理嵌套结构,并支持块特定样式和设置。
// 读取全局文本颜色
const textColor = getStyle( globalStyles, 'color.text' );
// 设置按钮背景颜色
const updated = setStyle(
globalStyles,
'color.background',
'#0073aa',
'core/button'
);
// 获取段落特定字体大小
const fontSizes = getSetting(
globalStyles,
'typography.fontSizes',
'core/paragraph'
);使用前需确保块及其样式已全局注册,因为块样式从全局块存储中获取。
A generic library for reading and writing global styles data structures (theme.json format).
This is a framework-agnostic utility library that provides functions for manipulating global styles objects. It handles the complex nested structure of theme.json format, including block-specific styles and settings.
getStyle(globalStyles, path, blockName?, shouldDecodeEncode?)Get a style value from the global styles object.
// Get global text color
const textColor = getStyle( globalStyles, 'color.text' );
// Get button background color
const buttonBg = getStyle( globalStyles, 'color.background', 'core/button' );
// Get raw value without CSS variable resolution
const rawValue = getStyle( globalStyles, 'color.text', undefined, false );
getSetting(globalStyles, path, blockName?)Get a setting value with fallback hierarchy (block-specific global).
// Get global color palette
const colors = getSetting( globalStyles, 'color.palette' );
// Get paragraph-specific font sizes
const fontSizes = getSetting(
globalStyles,
'typography.fontSizes',
'core/paragraph'
);
setStyle(globalStyles, path, newValue, blockName?)Immutably set a style value. Returns a new global styles object.
// Set global text color
const updated = setStyle( globalStyles, 'color.text', '#333333' );
// Set button background color
const updated = setStyle(
globalStyles,
'color.background',
'#0073aa',
'core/button'
);
setSetting(globalStyles, path, newValue, blockName?)Immutably set a setting value. Returns a new global styles object.
// Set global color palette
const updated = setSetting( globalStyles, 'color.palette', newPalette );
// Set block-specific settings
const updated = setSetting(
globalStyles,
'typography.fontSizes',
sizes,
'core/heading'
);
getPalettes(globalStyles)Extract color palettes organized by origin (theme, custom, default).
generateGlobalStyles(globalStyles)Generate CSS from global styles object.
mergeGlobalStyles(base, user)Merge multiple global styles objects with proper precedence.
The global styles object follows the theme.json schema:
{
styles: {
color: { text: '#000', background: '#fff' },
typography: { fontSize: '16px' },
elements: {
button: { color: { background: 'blue' } }
},
blocks: {
'core/paragraph': { color: { text: '#333' } }
}
},
settings: {
color: { palette: [...] },
typography: { fontSizes: [...] },
blocks: {
'core/paragraph': { color: { text: true } }
}
}
}
This library is designed to be used as the foundation for any global styles editing interface. It provides the core data manipulation functions while remaining completely generic and reusable.
It is a prerequisite for use that blocks and their styles be globally registered, because block styles are fetched from the global blocks store.