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: add typography and color to frontend #40665

Merged
merged 22 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
856765d
Returning inline styles by default.
ramonjd Apr 28, 2022
e096ef0
Adding some dev notes for later brain
ramonjd Apr 28, 2022
db3efea
Adding a couple of typography styles
ramonjd Apr 28, 2022
467d86a
Adding text decoration
ramonjd Apr 29, 2022
def54bd
Adding typography properties letterSpacing.ts and textTransform.ts
ramonjd May 2, 2022
ce1204d
Adding color.text and typography.fontWeight
ramonjd May 2, 2022
1067901
Refactor generic rule generator
ramonjd May 3, 2022
c77f3be
Experiment: adding classname generator
ramonjd May 3, 2022
ac06d20
Fixing bonkers typos because I copy and paste like I just don't care.…
ramonjd May 4, 2022
5c77984
Enable style engine for backgroundColor
ramonjd May 4, 2022
c012938
Regenerate pullquote fixture since the style engine changes the order…
ramonjd May 4, 2022
0832750
Add constants
ramonjd May 6, 2022
c78f5d0
Update packages/style-engine/src/index.ts
ramonjd May 5, 2022
695cd34
Refactoring generate method to split inline and selector logic
ramonjd May 6, 2022
8922ec0
Remove TODO comment. It's something we can do in the future, but it d…
ramonjd May 6, 2022
bdb1fb1
Reinstating color and now parsing style value for `var:`, whereby we'…
ramonjd May 6, 2022
864413e
A bit of defensive coding to check for a string type
ramonjd May 6, 2022
2b5c949
Testing out generating block color classnames using the style engine.
ramonjd May 9, 2022
676936a
Also passing the style color value to `getClassnames` so it will add …
ramonjd May 9, 2022
53ac842
Reverting `getClassnames`. It's a bigger change and should be looked …
ramonjd May 10, 2022
41dbe14
Fix typo in fixture
ramonjd May 10, 2022
c090f55
Flag fontStyle with useEngine: true so that it uses the style engine.
ramonjd May 17, 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
5 changes: 1 addition & 4 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ export function getInlineStyles( styles = {} ) {

// The goal is to move everything to server side generated engine styles
// This is temporary as we absorb more and more styles into the engine.
const extraRules = getCSSRules( styles, { selector: 'self' } );
const extraRules = getCSSRules( styles );
extraRules.forEach( ( rule ) => {
if ( rule.selector !== 'self' ) {
throw "This style can't be added as inline style";
}
output[ rule.key ] = rule.value;
} );

Expand Down
9 changes: 9 additions & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
value: [ 'color', 'background' ],
support: [ 'color', 'background' ],
requiresOptOut: true,
useEngine: true,
},
borderColor: {
value: [ 'border', 'color' ],
Expand Down Expand Up @@ -103,6 +104,7 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
value: [ 'color', 'text' ],
support: [ 'color', 'text' ],
requiresOptOut: true,
useEngine: true,
},
filter: {
value: [ 'filter', 'duotone' ],
Expand All @@ -119,18 +121,22 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
fontSize: {
value: [ 'typography', 'fontSize' ],
support: [ 'typography', 'fontSize' ],
useEngine: true,
},
fontStyle: {
value: [ 'typography', 'fontStyle' ],
support: [ 'typography', '__experimentalFontStyle' ],
useEngine: true,
},
fontWeight: {
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
value: [ 'typography', 'fontWeight' ],
support: [ 'typography', '__experimentalFontWeight' ],
useEngine: true,
},
lineHeight: {
value: [ 'typography', 'lineHeight' ],
support: [ 'typography', 'lineHeight' ],
useEngine: true,
},
margin: {
value: [ 'spacing', 'margin' ],
Expand All @@ -157,14 +163,17 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
textDecoration: {
value: [ 'typography', 'textDecoration' ],
support: [ 'typography', '__experimentalTextDecoration' ],
useEngine: true,
},
textTransform: {
value: [ 'typography', 'textTransform' ],
support: [ 'typography', '__experimentalTextTransform' ],
useEngine: true,
},
letterSpacing: {
value: [ 'typography', 'letterSpacing' ],
support: [ 'typography', '__experimentalLetterSpacing' ],
useEngine: true,
},
'--wp--style--block-gap': {
value: [ 'spacing', 'blockGap' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,8 @@ function getStylesDeclarations( blockStyles = {} ) {

// The goal is to move everything to server side generated engine styles
// This is temporary as we absorb more and more styles into the engine.
const extraRules = getCSSRules( blockStyles, { selector: 'self' } );
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
const extraRules = getCSSRules( blockStyles );
extraRules.forEach( ( rule ) => {
if ( rule.selector !== 'self' ) {
throw "This style can't be added as inline style";
}
const cssProperty = rule.key.startsWith( '--' )
? rule.key
: kebabCase( rule.key );
Expand Down
14 changes: 13 additions & 1 deletion packages/style-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ import { styleDefinitions } from './styles';
*/
export function generate( style: Style, options: StyleOptions ): string {
const rules = getCSSRules( style, options );

// If no selector is provided, treat generated rules as inline styles to be returned as a single string.
if ( ! options?.selector ) {
const inlineRules: string[] = [];
rules.forEach( ( rule ) => {
inlineRules.push( `${ kebabCase( rule.key ) }: ${ rule.value };` );
} );
return inlineRules.join( ' ' );
}

const groupedRules = groupBy( rules, 'selector' );
const selectorRules = Object.keys( groupedRules ).reduce(
( acc: string[], subSelector: string ) => {
Expand Down Expand Up @@ -57,7 +67,9 @@ export function getCSSRules(
): GeneratedCSSRule[] {
const rules: GeneratedCSSRule[] = [];
styleDefinitions.forEach( ( definition: StyleDefinition ) => {
rules.push( ...definition.generate( style, options ) );
if ( typeof definition.generate === 'function' ) {
rules.push( ...definition.generate( style, options ) );
}
} );

return rules;
Expand Down
19 changes: 19 additions & 0 deletions packages/style-engine/src/styles/color/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../../types';
import { generateRule } from '../utils';

const background = {
name: 'background',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'color', 'background' ],
'backgroundColor'
);
},
};

export default background;
19 changes: 19 additions & 0 deletions packages/style-engine/src/styles/color/gradient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../../types';
import { generateRule } from '../utils';

const gradient = {
name: 'gradient',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'color', 'gradient' ],
'background'
);
},
};

export default gradient;
8 changes: 8 additions & 0 deletions packages/style-engine/src/styles/color/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Internal dependencies
*/
import background from './background';
import gradient from './gradient';
import text from './text';

export default [ text, gradient, background ];
14 changes: 14 additions & 0 deletions packages/style-engine/src/styles/color/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../../types';
import { generateRule } from '../utils';

const text = {
name: 'text',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule( style, options, [ 'color', 'text' ], 'color' );
},
};

export default text;
3 changes: 3 additions & 0 deletions packages/style-engine/src/styles/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const VARIABLE_REFERENCE_PREFIX = 'var:';
export const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
export const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
7 changes: 4 additions & 3 deletions packages/style-engine/src/styles/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Internal dependencies
*/
import padding from './padding';
import margin from './margin';
import color from './color';
import spacing from './spacing';
import typography from './typography';

export const styleDefinitions = [ margin, padding ];
export const styleDefinitions = [ ...color, ...spacing, ...typography ];
7 changes: 7 additions & 0 deletions packages/style-engine/src/styles/spacing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Internal dependencies
*/
import padding from './padding';
import margin from './margin';

export default [ margin, padding ];
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../types';
import { generateBoxRules } from './utils';
import type { Style, StyleOptions } from '../../types';
import { generateBoxRules } from '../utils';

const margin = {
name: 'margin',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../types';
import { generateBoxRules } from './utils';
import type { Style, StyleOptions } from '../../types';
import { generateBoxRules } from '../utils';

const padding = {
name: 'padding',
Expand Down
99 changes: 99 additions & 0 deletions packages/style-engine/src/styles/typography/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../../types';
import { generateRule } from '../utils';

const fontSize = {
name: 'fontSize',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'fontSize' ],
'fontSize'
);
},
};

const fontStyle = {
name: 'fontStyle',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'fontStyle' ],
'fontStyle'
);
},
};

const fontWeight = {
name: 'fontWeight',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'fontWeight' ],
'fontWeight'
);
},
};

const letterSpacing = {
name: 'letterSpacing',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'letterSpacing' ],
'letterSpacing'
);
},
};

const lineHeight = {
name: 'letterSpacing',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'lineHeight' ],
'lineHeight'
);
},
};

const textDecoration = {
name: 'textDecoration',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'textDecoration' ],
'textDecoration'
);
},
};

const textTransform = {
name: 'textTransform',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'typography', 'textTransform' ],
'textTransform'
);
},
};

export default [
fontSize,
fontStyle,
fontWeight,
letterSpacing,
lineHeight,
textDecoration,
textTransform,
];
Loading