-
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
Add initial version of the style engine #37978
Changes from all commits
81c223a
60d6a28
a6d97b1
a1746d2
9b2e35f
bc33ed2
55e18f6
551884d
0cffd1e
a59de6c
91109e1
f0cbeff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. --> | ||
|
||
## Unreleased | ||
|
||
### New Feature | ||
|
||
- Added initial version of the style engine ([#37978](https://github.com/WordPress/gutenberg/pull/37978)). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Style Engine | ||
|
||
The Style Engine powering global styles and block customizations. | ||
|
||
## Installation | ||
|
||
Install the module | ||
|
||
```bash | ||
npm install @wordpress/style-engine --save | ||
``` | ||
|
||
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._ | ||
|
||
## Important | ||
|
||
This Package is considered experimental at the moment. The idea is to have a package used to generate styles based on a style object that is consistent between: backend, frontend, block style object and theme.json. | ||
|
||
Currently it's not a package that generates a wp.styleEngine global because it's not ready yet, it's still a bundled package but ultimately, we want it to be so, once the roadmap is finished: | ||
|
||
**TODO List:** | ||
|
||
- Add style definitions for all the currently supported styles in blocks and theme.json. | ||
- the CSS variable shortcuts for values (for presets...) | ||
- Support generating styles in the frontend. | ||
- Support generating styles in the backend (block supports and theme.json stylesheet). | ||
- Refactor all block styles to use the style engine server side. | ||
- Refactor all blocks to consistently use the "style" attribute for all customizations (get rid of the preset specific attributes). | ||
|
||
## Usage | ||
|
||
<!-- START TOKEN(Autogenerated API docs) --> | ||
|
||
### generate | ||
|
||
Generates a stylesheet for a given style object and selector. | ||
|
||
_Parameters_ | ||
|
||
- _style_ `Style`: Style object. | ||
- _options_ `StyleOptions`: Options object with settings to adjust how the styles are generated. | ||
|
||
_Returns_ | ||
|
||
- `string`: generated stylesheet. | ||
|
||
### getCSSRules | ||
|
||
Returns a JSON representation of the generated CSS rules. | ||
|
||
_Parameters_ | ||
|
||
- _style_ `Style`: Style object. | ||
- _options_ `StyleOptions`: Options object with settings to adjust how the styles are generated. | ||
|
||
_Returns_ | ||
|
||
- `GeneratedCSSRule[]`: generated styles. | ||
|
||
<!-- END TOKEN(Autogenerated API docs) --> | ||
|
||
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "@wordpress/style-engine", | ||
"version": "0.1.0", | ||
"description": "WordPress Style engine.", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"gutenberg", | ||
"styles", | ||
"global styles" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/style-engine/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git", | ||
"directory": "packages/style-engine" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"types": "build-types", | ||
"sideEffects": false, | ||
"dependencies": { | ||
"@babel/runtime": "^7.16.0", | ||
"lodash": "^4.17.21" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { groupBy, kebabCase } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { | ||
Style, | ||
StyleOptions, | ||
GeneratedCSSRule, | ||
StyleDefinition, | ||
} from './types'; | ||
import { styleDefinitions } from './styles'; | ||
|
||
/** | ||
* Generates a stylesheet for a given style object and selector. | ||
* | ||
* @param style Style object. | ||
* @param options Options object with settings to adjust how the styles are generated. | ||
* | ||
* @return generated stylesheet. | ||
*/ | ||
export function generate( style: Style, options: StyleOptions ): string { | ||
const rules = getCSSRules( style, options ); | ||
const groupedRules = groupBy( rules, 'selector' ); | ||
const selectorRules = Object.keys( groupedRules ).reduce( | ||
( acc: string[], subSelector: string ) => { | ||
acc.push( | ||
`${ subSelector } { ${ groupedRules[ subSelector ] | ||
.map( | ||
( rule: GeneratedCSSRule ) => | ||
`${ kebabCase( rule.key ) }: ${ rule.value };` | ||
) | ||
.join( ' ' ) } }` | ||
); | ||
return acc; | ||
}, | ||
[] | ||
); | ||
|
||
return selectorRules.join( '\n' ); | ||
} | ||
|
||
/** | ||
* Returns a JSON representation of the generated CSS rules. | ||
* | ||
* @param style Style object. | ||
* @param options Options object with settings to adjust how the styles are generated. | ||
* | ||
* @return generated styles. | ||
*/ | ||
export function getCSSRules( | ||
style: Style, | ||
options: StyleOptions | ||
): GeneratedCSSRule[] { | ||
const rules: GeneratedCSSRule[] = []; | ||
styleDefinitions.forEach( ( definition: StyleDefinition ) => { | ||
rules.push( ...definition.generate( style, options ) ); | ||
} ); | ||
|
||
return rules; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import padding from './padding'; | ||
|
||
export const styleDefinitions = [ padding ]; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||
/** | ||||
* Internal dependencies | ||||
*/ | ||||
import type { Style, StyleOptions } from '../types'; | ||||
import { generateBoxRules } from './utils'; | ||||
|
||||
const padding = { | ||||
name: 'padding', | ||||
generate: ( style: Style, options: StyleOptions ) => { | ||||
return generateBoxRules( | ||||
style, | ||||
options, | ||||
[ 'spacing', 'padding' ], | ||||
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. Could this path value one day be an options value we get from
Just wondering if the style engine needs to know about the block implementation detail, or if we even need a single source of 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. Good question! I think having a single source of truth is a good idea, but I imagine that might actually live in this package instead of in the blocks package (if we assume that the style engine is the "owner" of how to process the style object)? I suppose we could either move the constants over to the style engine in a central place, or leave it up to each individual style definition to know only about itself? Either way, probably a good thing for us to explore in follow-ups! |
||||
'padding' | ||||
); | ||||
}, | ||||
}; | ||||
|
||||
export default padding; |
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.
Ideally at the end of the style engine implementation, this "constant" might be removed entirely if we did our work properly :)