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

Editor Performance Rework #3290

Open
wants to merge 63 commits into
base: develop
Choose a base branch
from
Open

Editor Performance Rework #3290

wants to merge 63 commits into from

Conversation

bfintal
Copy link
Contributor

@bfintal bfintal commented Aug 14, 2024

This PR is a refactor of how the block styles are generated. There are the major changes in this PR:

  1. The BlockCss component will be deprecated, instead of using a ReactJS Component to generate styles, we will now have a single class / object that should be able to generate CSS styles faster. This requires a rewrite of all BlockCss components into function calls.
  2. CSS style generation from the save.js will be removed so that there is zero CSS computations performed during block saving. Instead, CSS style generation will only be done in edit.js when attributes are being changed.
  3. We refactored some usage of useBlockContext (our own hook) and replaced it with useSelect using
    getBlockRootClientId and getBlockOrder
  4. Removed the minifyCSS call when saving CSS
  5. Some instances of useAttributeEditHandlers is replaced with getAttributeNameFunc to prevent rerenders
  6. Memo the alignment block toolbar button

Block Changes

These changes will need to be applied across all Stackable blocks:

edit.js

  1. Remove useGeneratedCss( props.attributes )
  2. Remove the <BlockStyles /> component, and replace with a hook that generates the editor css:
// Generate the CSS styles for the block.
const blockCss = useBlockCssGenerator( {
	attributes: props.attributes,
	blockStyles,
	clientId: props.clientId,
	context: props.context,
	setAttributes: props.setAttributes,
	blockState: props.blockState,
	version: VERSION,
} )

// In the rendered edit component:
{ blockCss && <style key="block-css">{ blockCss }</style> }
  1. Separate the inspector controls into a separated memoed component to prevent rerenders
const Edit = props => {
 	return (
- 		<>
-			<InspectorTabs />
-			<InspectorLayoutControls prop1={ props.attributes.prop1 } />
-			// ... 
- 		</>
+		<InspectorControls prop1={ props.attributes.prop1 } />
	)
}

+ const InspectorControls = memo( props => {
+ 	return (
+ 		<>
+ 			<InspectorTabs />
+ 			<InspectorLayoutControls prop1={ props.prop1 } />
+ 			// ...
+ 		</>
+ 	)
+ } )

save.js

  1. Remove all CSS generation, instead just display the attribute generatedCss:
{ attributes.generatedCss && <style>{ attributes.generatedCss }</style> }

style.js

  1. Rewrite all <BlockCss /> components to BlockStyleGenerator object
- const propsToPass = {
- 	...props,
- 	version: props.version,
- 	versionAdded: '3.0.0',
- 	versionDeprecated: '',
- }
-
- <BlockCss
- 	{ ...propsToPass }
- 	selector=".%s-container"
- 	styleRule="marginTop"
- 	attrName="columnSpacing"
- 	key="columnSpacing-top"
- 	responsive="all"
- 	hasUnits="px"
- 	valuePreCallback={ callbacks.marginTop.valuePreCallback }
- />

+ export const blockStyles = new BlockStyleGenerator( {
+ 	versionAdded: '3.0.0',
+ 	versionDeprecated: '',
+ } )
+ 
+ blockStyles.addBlockStyles( 'columnSpacing', [ {
+ 	selector: '.%s-container',
+ 	styleRule: 'marginTop',
+ 	attrName: 'columnSpacing',
+ 	key: 'columnSpacing-top',
+ 	responsive: 'all',
+ 	hasUnits: 'px',
+ 	valuePreCallback: callbacks.marginTop.valuePreCallback,
+ } ] )
  1. Remove memos and replace components with the new BlockStyleGenerator methods:
- <Advanced.Style { ...props } />
+ Advanced.Style.addStyles( blockStyles )

This is assuming we're built Advanced.Style.addStyles

  1. Remove the whole BlockStyles.Content

Block Component Changes

  1. We need to replace all <BlockCss /> components to BlockStyleGenerator object

Other Optimizations Implemented

AdvancedRangeControl:

  • Changed updating of internal value - removed useEffect
  • Removed useEffect and useLayoutEffect for computing dynamic placeholder values

⚠️ Pending things

  • See if we can remove useBlockContext
  • Remove the useGeneratedCss hook in src/block-components/style/use-generated-css.js
  • Remove all instances of <BlockCss /> component
  • One room for improvement is the useBlockCssGenerator, we have 2 hooks one for edit and one for save, maybe we can share the process somehow so as not to compute things twice. e.g. do not minify the saved css (maybe leave that to PHP instead, or don't) so that we can improve performance

⚠️ Optimization Issues found

  • When you have an Inner Column block, if you drag left/right rapidly the Column Spacing control, it stalls.
Screen.Recording.2024-08-15.at.8.30.44.AM.mov

fixes #3261

Copy link

github-actions bot commented Aug 14, 2024

🤖 Pull request artifacts

file commit
pr3290-stackable-3290-merge.zip e8777a3

github-actions bot added a commit that referenced this pull request Aug 14, 2024
github-actions bot added a commit that referenced this pull request Aug 14, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 15, 2024
@bfintal bfintal self-assigned this Aug 15, 2024
github-actions bot added a commit that referenced this pull request Aug 17, 2024
github-actions bot added a commit that referenced this pull request Aug 17, 2024
github-actions bot added a commit that referenced this pull request Aug 19, 2024
github-actions bot added a commit that referenced this pull request Aug 19, 2024
github-actions bot added a commit that referenced this pull request Aug 19, 2024
github-actions bot added a commit that referenced this pull request Sep 2, 2024
github-actions bot added a commit that referenced this pull request Sep 2, 2024
github-actions bot added a commit that referenced this pull request Sep 4, 2024
github-actions bot added a commit that referenced this pull request Sep 4, 2024
github-actions bot added a commit that referenced this pull request Sep 4, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
github-actions bot added a commit that referenced this pull request Sep 5, 2024
@bfintal bfintal marked this pull request as ready for review September 6, 2024 04:50
github-actions bot added a commit that referenced this pull request Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[WP 6.6] Stackable blocks with WP 6.6 causes slow down
2 participants