-
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.
Add initial version of the style engine (#37978)
* Add initial version of the style engine * Update Style type to move typography attributes to their own object * Update useEngine check * Clear out changelog * Attempt to fix issue with package-lock.json and the check-local-changes step * Switch getCSSRules to use camelCase and generate to switch to kebabCase * Skip useEngine styles in globalstyles reduce call, update output to use kebabCase * Change order of box sides to match TRBL order of shorthand syntax and fix unit test * Add StyleOptions type and update function signature to include options object instead of selector string * Remove unnecessary return statement * Switch rules reassignment to rules.push * Update style object types to use CSSProperties Co-authored-by: Andrew Serong <[email protected]>
- Loading branch information
1 parent
8d56c4e
commit d3688d8
Showing
21 changed files
with
459 additions
and
16 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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,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)). |
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,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> |
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,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" | ||
} | ||
} |
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,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; | ||
} |
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,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import padding from './padding'; | ||
|
||
export const styleDefinitions = [ padding ]; |
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,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' ], | ||
'padding' | ||
); | ||
}, | ||
}; | ||
|
||
export default padding; |
Oops, something went wrong.