Skip to content
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

Style engine: pre-core migration documentation and house keeping #43840

Merged
merged 29 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
aec32e6
Initial commit. Adding further documentation, cleaning up changelog r…
ramonjd Sep 5, 2022
324c7fb
Typos, formatting
ramonjd Sep 5, 2022
66859c1
Typos, formatting
ramonjd Sep 5, 2022
fda9bd9
Sorting out change log and formatting
ramonjd Sep 5, 2022
aad9a7a
Remove enqueue option. It does nothing.
ramonjd Sep 5, 2022
627dda9
Update packages/style-engine/docs/using-the-style-engine-with-block-s…
ramonjd Sep 5, 2022
960e7a9
Update README.md
ramonjd Sep 5, 2022
29a9369
Updating manifest.json to include new style engine docs
ramonjd Sep 6, 2022
47ab494
Adding type annotation
ramonjd Sep 6, 2022
fb680d7
Updating CHANGELOG.md
ramonjd Sep 6, 2022
a0149a5
Updated version to prerelease
ramonjd Sep 6, 2022
2279755
Rebase after #43886
ramonjd Sep 6, 2022
d1939e4
Updating change logs and emphasizing that new global JS methods are a…
ramonjd Sep 7, 2022
1084864
Checking for functions and classes in tests so that Gutenberg tests d…
ramonjd Sep 7, 2022
2d9b645
Formatting PHP doc comments and params that describe associative arrays
ramonjd Sep 8, 2022
3d5e463
Formatting
ramonjd Sep 8, 2022
09843d4
Combining tests for get_declarations_string
ramonjd Sep 8, 2022
7cc64a2
Adding message parameter to assertions where more than one assertion …
ramonjd Sep 8, 2022
53e4edb
Updating PHP docs
ramonjd Sep 8, 2022
5af5d0a
Text domain added to __()
ramonjd Sep 8, 2022
7991cf1
Refactoring sanitization tests for CSS declarations
ramonjd Sep 8, 2022
469b90f
Formatting styleEngine tests
ramonjd Sep 8, 2022
edf81cb
My blod is typo
ramonjd Sep 8, 2022
6f7ef65
Active voice rather than imperative
ramonjd Sep 8, 2022
fb8edd7
Remove Gutenberg suffix
ramonjd Sep 8, 2022
9ac232c
Adding message to script loader block support enqueuing expectations
ramonjd Sep 9, 2022
ccf6773
Grammar, formatting, removing class_exists checks
ramonjd Sep 9, 2022
afc7d9e
Ignore unused variable. This is a common test pattern in Core.
ramonjd Sep 9, 2022
19b6885
Remove unusued var in tests
ramonjd Sep 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,12 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experiments-page.php';

// Copied package PHP files.
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php' ) ) {
if ( is_dir( __DIR__ . '/../build/style-engine' ) ) {
require_once __DIR__ . '/../build/style-engine/style-engine-gutenberg.php';
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-declarations-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-declarations-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rule-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rule-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rules-store-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rules-store-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-processor-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-processor-gutenberg.php';
}

Expand Down
4 changes: 4 additions & 0 deletions packages/dependency-extraction-webpack-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Include `@wordpress/style-engine` on the list of external dependencies to allow using `wp.styleEngine` global with WordPress 6.1 and beyond ([#43840](https://github.com/WordPress/gutenberg/pull/43840)).

## 4.0.0 (2022-08-24)

### Breaking Change
Expand Down
6 changes: 1 addition & 5 deletions packages/dependency-extraction-webpack-plugin/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = [
'@wordpress/icons',
'@wordpress/interface',
'@wordpress/style-engine',
];
const BUNDLED_PACKAGES = [ '@wordpress/icons', '@wordpress/interface' ];
ramonjd marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default request to global transformation
Expand Down
50 changes: 18 additions & 32 deletions packages/style-engine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@

## Unreleased

## 0.16.0 (2022-09-13)

## 0.15.0 (2022-08-24)

## 0.14.0 (2022-08-10)

## 0.13.0 (2022-07-27)

## 0.12.0 (2022-07-13)

## 0.11.0 (2022-06-29)

## 0.10.0 (2022-06-15)

## 0.9.0 (2022-06-01)

## 0.8.0 (2022-05-18)

## 0.7.0 (2022-05-04)

## 0.6.0 (2022-04-21)

## 0.5.0 (2022-04-08)

## 0.4.0 (2022-03-23)

## 0.3.0 (2022-03-11)

## 0.2.0 (2022-02-23)

### New Feature

### Enhancement
- Allow for prettified output ([#42909](https://github.com/WordPress/gutenberg/pull/42909)).
- Enqueue block supports styles in Gutenberg ([#42880](https://github.com/WordPress/gutenberg/pull/42880)).

### Internal
- Move backend scripts to package ([#39736](https://github.com/WordPress/gutenberg/pull/39736)).
- Updating docs, formatting, and separating global functions from the main class file ([#43840](https://github.com/WordPress/gutenberg/pull/43840)).

### New Features
- Add a WP_Style_Engine_Processor object ([#42463](https://github.com/WordPress/gutenberg/pull/42463)).
- Add a WP_Style_Engine_CSS_Declarations object ([#42043](https://github.com/WordPress/gutenberg/pull/42043)).
- Add Rules and Store objects ([#42222](https://github.com/WordPress/gutenberg/pull/42222)).
- Add elements styles support ([#41732](https://github.com/WordPress/gutenberg/pull/41732)) and ([#40987](https://github.com/WordPress/gutenberg/pull/40987)).
- Add typography and color support ([#40665](https://github.com/WordPress/gutenberg/pull/40987)) and ([#40332](https://github.com/WordPress/gutenberg/pull/40332)).
- Add border support ([#41803](https://github.com/WordPress/gutenberg/pull/40332)) and ([#40531](https://github.com/WordPress/gutenberg/pull/40531)).
- Add margin support to frontend ([#39790](https://github.com/WordPress/gutenberg/pull/39790)).
- Add basic block supports to backend ([#39446](https://github.com/WordPress/gutenberg/pull/39446)).
- Added initial version of the style engine ([#37978](https://github.com/WordPress/gutenberg/pull/37978)).
- Include `@wordpress/style-engine` on the list of external dependencies to allow using `wp.styleEngine` global with WordPress 6.1 and beyond ([#43840](https://github.com/WordPress/gutenberg/pull/43840)).
58 changes: 30 additions & 28 deletions packages/style-engine/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
# Style Engine

The Style Engine powering global styles and block customizations.
The Style Engine aims to provide a consistent API for rendering styling for blocks across both client-side and server-side applications.

## Important
Initially, it will offer a single, centralized agent responsible for generating block styles, and, in later phases, it will also assume the responsibility of processing and rendering optimized frontend CSS.

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.
## Important

Because this package is experimental and still in development it does not yet generate a `wp.styleEngine` global. To get
there, the following tasks need to be completed:
This package is new as of WordPress 6.1 and therefore in its infancy.

**TODO List:**
Upcoming tasks on the roadmap include, but are not limited to, the following:

- 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. (Ongoing)
- Support generating styles in the backend (block supports and theme.json stylesheet). (Ongoing)
- Refactor all block styles to use the style engine server side. (Ongoing)
- Consolidate global and block style rendering and enqueuing
- Refactor all blocks to consistently use the "style" attribute for all customizations (get rid of the preset specific
attributes).
- Consolidate global and block style rendering and enqueuing (ongoing)
- Explore pre-render CSS rule processing with the intention of deduplicating other common and/or repetitive block styles. (ongoing)
- Extend the scope of semantic class names and/or design token expression, and encapsulate rules into stable utility classes.
- Explore pre-render CSS rule processing with the intention of deduplicating other common and/or repetitive block styles.
- Propose a way to control hierarchy and specificity, and make the style hierarchy cascade accessible and predictable. This might include preparing for CSS cascade layers until they become more widely supported, and allowing for opt-in support in Gutenberg via theme.json.
- Refactor all blocks to consistently use the "style" attribute for all customizations, that is, deprecate preset-specific attributes such as `attributes.fontSize`.

See [Tracking: Add a Style Engine to manage rendering block styles #38167](https://github.com/WordPress/gutenberg/issues/38167)
For more information about the roadmap, please refer to [Block editor styles: initiatives and goals](https://make.wordpress.org/core/2022/06/24/block-editor-styles-initiatives-and-goals/) and the [Github project board](https://github.com/orgs/WordPress/projects/19).

## Backend API

Expand All @@ -30,8 +26,9 @@ See [Tracking: Add a Style Engine to manage rendering block styles #38167](https
Global public function to generate styles from a single style object, e.g., the value of
a [block's attributes.style object](https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles)
or
the [top level styles in theme.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/)
.
the [top level styles in theme.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/).

See also [Using the Style Engine to generate block supports styles](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/using-the-style-engine-with-block-supports).

_Parameters_

Expand Down Expand Up @@ -82,7 +79,7 @@ print_r( $styles );

/*
array(
'css' => '.a-selector{padding:10px}'
'css' => '.a-selector{padding:100px}'
'declarations' => array( 'padding' => '100px' )
)
*/
Expand Down Expand Up @@ -132,7 +129,7 @@ $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
)
);
print_r( $stylesheet ); // .wp-pumpkin, .wp-kumquat {color:orange}.wp-tomato{color:red;padding:100px}
print_r( $stylesheet ); // .wp-pumpkin,.wp-kumquat{color:orange}.wp-tomato{color:red;padding:100px}
```

### wp_style_engine_get_stylesheet_from_context()
Expand Down Expand Up @@ -185,10 +182,7 @@ Install the module
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._
_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._

## Usage

Expand All @@ -205,7 +199,11 @@ _Parameters_

_Returns_

- `string`: generated stylesheet.
- `string`: A generated stylesheet or inline style declarations.

_Changelog_

`6.1.0` Introduced in WordPress core.

### getCSSRules

Expand All @@ -218,7 +216,11 @@ _Parameters_

_Returns_

- `GeneratedCSSRule[]`: generated styles.
- `GeneratedCSSRule[]`: A collection of objects containing the selector, if any, the CSS property key (camelcase) and parsed CSS value.

_Changelog_

`6.1.0` Introduced in WordPress core.

<!-- END TOKEN(Autogenerated API docs) -->

Expand All @@ -239,8 +241,8 @@ A guide to the terms and variable names referenced by the Style Engine package.
<dd>Identifiers that describe stylistic, modifiable features of an HTML element. E.g., <code>border</code>, <code>font-size</code>, <code>width</code>...</dd>
<dt>CSS rule</dt>
<dd>A CSS selector followed by a CSS declarations block inside a set of curly braces. Usually found in a CSS stylesheet.</dd>
<dt>CSS selector</dt>
<dd>The first component of a CSS rule, a CSS selector is a pattern of elements, classnames or other terms that define the element to which the rule&rsquo;s CSS definitions apply. E.g., <code>p.my-cool-classname > span</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors" target="_blank">MDN CSS selectors article</a>.</dd>
<dt>CSS selector (or CSS class selector)</dt>
<dd>The first component of a CSS rule, a CSS selector is a pattern of elements, classnames or other terms that define the element to which the rule&rsquo;s CSS definitions apply. E.g., <code>p.my-cool-classname > span</code>. A CSS selector matches HTML elements based on the contents of the "class" attribute. See <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors" target="_blank">MDN CSS selectors article</a>.</dd>
<dt>CSS stylesheet</dt>
<dd>A collection of CSS rules contained within a file or within an <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style" target="_blank">HTML style tag</a>.</dd>
<dt>CSS value</dt>
Expand Down
29 changes: 14 additions & 15 deletions packages/style-engine/class-wp-style-engine-css-declarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,42 @@ class WP_Style_Engine_CSS_Declarations {
* If a `$declarations` array is passed, it will be used to populate
* the initial $declarations prop of the object by calling add_declarations().
*
* @param array $declarations An array of declarations (property => value pairs).
* @param string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
*/
public function __construct( $declarations = array() ) {
$this->add_declarations( $declarations );
}

/**
* Add a single declaration.
* Adds a single declaration.
*
* @param string $property The CSS property.
* @param string $value The CSS value.
*
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
*/
public function add_declaration( $property, $value ) {

// Sanitize the property.
// Sanitizes the property.
$property = $this->sanitize_property( $property );
// Bail early if the property is empty.
// Bails early if the property is empty.
if ( empty( $property ) ) {
return $this;
}

// Trim the value. If empty, bail early.
// Trims the value. If empty, bail early.
$value = trim( $value );
if ( '' === $value ) {
return $this;
}

// Add the declaration property/value pair.
// Adds the declaration property/value pair.
$this->declarations[ $property ] = $value;

return $this;
}

/**
* Remove a single declaration.
* Removes a single declaration.
*
* @param string $property The CSS property.
*
Expand All @@ -79,7 +78,7 @@ public function remove_declaration( $property ) {
}

/**
* Add multiple declarations.
* Adds multiple declarations.
*
* @param array $declarations An array of declarations.
*
Expand All @@ -93,7 +92,7 @@ public function add_declarations( $declarations ) {
}

/**
* Remove multiple declarations.
* Removes multiple declarations.
*
* @param array $properties An array of properties.
*
Expand All @@ -107,7 +106,7 @@ public function remove_declarations( $properties = array() ) {
}

/**
* Get the declarations array.
* Gets the declarations array.
*
* @return array
*/
Expand All @@ -122,7 +121,7 @@ public function get_declarations() {
* @param string $value The value to be filtered.
* @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
*
* @return string The filtered declaration as a single string.
* @return string The filtered declaration or an empty string.
*/
protected static function filter_declaration( $property, $value, $spacer = '' ) {
$filtered_value = wp_strip_all_tags( $value, true );
Expand All @@ -135,8 +134,8 @@ protected static function filter_declaration( $property, $value, $spacer = '' )
/**
* Filters and compiles the CSS declarations.
*
* @param boolean $should_prettify Whether to add spacing, new lines and indents.
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
* @param bool $should_prettify Whether to add spacing, new lines and indents.
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
*
* @return string The CSS declarations.
*/
Expand All @@ -158,7 +157,7 @@ public function get_declarations_string( $should_prettify = false, $indent_count
}

/**
* Sanitize property names.
* Sanitizes property names.
*
* @param string $property The CSS property.
*
Expand Down
Loading