-
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
Lodash: Remove more _.isEmpty()
from block editor
#51098
Conversation
Size Change: -5 B (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
@@ -23,7 +18,8 @@ export default createHigherOrderComponent( ( WrappedComponent ) => { | |||
props.disableCustomColors === undefined | |||
? disableCustomColorsFeature | |||
: props.disableCustomColors; | |||
const hasColorsToChoose = ! isEmpty( colors ) || ! disableCustomColors; | |||
const hasColorsToChoose = | |||
( colors && colors.length > 0 ) || ! disableCustomColors; |
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.
The colors
argument is expected to be an array:
colors={ [ { color: '#f00', name: 'red' } ] } |
@@ -43,14 +42,14 @@ export const PanelColorGradientSettingsInner = ( { | |||
const panelId = useInstanceId( PanelColorGradientSettingsInner ); | |||
const { batch } = useRegistry(); | |||
if ( | |||
isEmpty( colors ) && | |||
isEmpty( gradients ) && | |||
( ! colors || colors.length === 0 ) && |
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.
Colors are expected to be an array in both tests and theme.json
:
colors={ [ |
https://developer.wordpress.org/themes/advanced-topics/theme-json/#color-palette
disableCustomColors && | ||
disableCustomGradients && | ||
settings?.every( | ||
( setting ) => | ||
isEmpty( setting.colors ) && | ||
isEmpty( setting.gradients ) && | ||
( ! setting.colors || setting.colors.length === 0 ) && |
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.
Colors are expected to be an array in both tests and theme.json
:
colors={ [ |
https://developer.wordpress.org/themes/advanced-topics/theme-json/#color-palette
isEmpty( colors ) && | ||
isEmpty( gradients ) && | ||
( ! colors || colors.length === 0 ) && | ||
( ! gradients || gradients.length === 0 ) && |
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.
Gradients are expected to be an array in both tests and theme.json
:
gradients={ [ |
https://developer.wordpress.org/themes/advanced-topics/theme-json/#color-palette
isEmpty( setting.colors ) && | ||
isEmpty( setting.gradients ) && | ||
( ! setting.colors || setting.colors.length === 0 ) && | ||
( ! setting.gradients || setting.gradients.length === 0 ) && |
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.
Gradients are expected to be an array in both tests and theme.json
:
gradients={ [ |
https://developer.wordpress.org/themes/advanced-topics/theme-json/#color-palette
@@ -58,10 +57,11 @@ function ColorGradientControlInner( { | |||
headingLevel, | |||
} ) { | |||
const canChooseAColor = | |||
onColorChange && ( ! isEmpty( colors ) || ! disableCustomColors ); | |||
onColorChange && | |||
( ( colors && colors.length > 0 ) || ! disableCustomColors ); |
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.
Colors are expected to be an array in both tests and theme.json
:
colors={ [ |
https://developer.wordpress.org/themes/advanced-topics/theme-json/#color-palette
@@ -685,7 +685,7 @@ export const getNodesWithSettings = ( tree, blockSelectors ) => { | |||
// Top-level. | |||
const presets = pickPresets( tree.settings ); | |||
const custom = tree.settings?.custom; | |||
if ( ! isEmpty( presets ) || custom ) { | |||
if ( Object.keys( presets ).length > 0 || custom ) { |
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.
@@ -698,7 +698,7 @@ export const getNodesWithSettings = ( tree, blockSelectors ) => { | |||
( [ blockName, node ] ) => { | |||
const blockPresets = pickPresets( node ); | |||
const blockCustom = node.custom; | |||
if ( ! isEmpty( blockPresets ) || blockCustom ) { | |||
if ( Object.keys( blockPresets ).length > 0 || blockCustom ) { |
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.
@@ -964,7 +964,7 @@ export const toStyles = ( | |||
} | |||
|
|||
const classes = getPresetsClasses( selector, presets ); | |||
if ( ! isEmpty( classes ) ) { | |||
if ( classes.length > 0 ) { |
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.
blockType?.selectors && | ||
Object.keys( blockType?.selectors ).length > 0 |
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.
Selectors, if defined, will be an object:
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#selectors
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.
Super nit: ?
in blockType?.selector
at the second check is not needed.
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.
Indeed, good find!
Cleaned it up in 262c8db
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.
Thank you Marin!
blockType?.selectors && | ||
Object.keys( blockType?.selectors ).length > 0 |
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.
Super nit: ?
in blockType?.selector
at the second check is not needed.
dd0de89
to
262c8db
Compare
Flaky tests detected in 262c8db. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5143262280
|
* Lodash: Remove more _.isEmpty() from block editor * Remove unnecessary boolean check * Remove unnecessary optional chaining
What?
This PR removes Lodash's
_.isEmpty()
from the block editor package.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're checking if the props are truthy, and if they are, we verify that it's not an empty object by using
Object.keys().length
.Testing Instructions