From 18642e60becfec59e289471ccae74275e6bc3de9 Mon Sep 17 00:00:00 2001 From: Luigi Date: Fri, 7 Jan 2022 16:59:29 +0100 Subject: [PATCH] fix crash on WP 5.8 --- .../blocks/product-elements/title/index.ts | 20 ++++++++++--------- assets/js/hooks/style-attributes.ts | 3 ++- assets/js/utils/global-style.js | 8 ++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 assets/js/utils/global-style.js diff --git a/assets/js/atomic/blocks/product-elements/title/index.ts b/assets/js/atomic/blocks/product-elements/title/index.ts index acb55c20264..2fae80a1862 100644 --- a/assets/js/atomic/blocks/product-elements/title/index.ts +++ b/assets/js/atomic/blocks/product-elements/title/index.ts @@ -16,6 +16,7 @@ import { BLOCK_DESCRIPTION as description, } from './constants'; import { Save } from './save'; +import { hasSpacingStyleSupport } from '../../../../utils/global-style'; const blockConfig: BlockConfiguration = { ...sharedConfig, @@ -36,15 +37,16 @@ const blockConfig: BlockConfiguration = { __experimentalFontFamily: true, }, } ), - ...( isFeaturePluginBuild() && { - color: { - text: true, - background: true, - link: false, - gradients: true, - __experimentalSkipSerialization: true, - }, - } ), + ...( isFeaturePluginBuild() && + hasSpacingStyleSupport() && { + color: { + text: true, + background: true, + link: false, + gradients: true, + __experimentalSkipSerialization: true, + }, + } ), ...( isFeaturePluginBuild() && { spacing: { margin: true, diff --git a/assets/js/hooks/style-attributes.ts b/assets/js/hooks/style-attributes.ts index da7a94b8daa..9acb889d848 100644 --- a/assets/js/hooks/style-attributes.ts +++ b/assets/js/hooks/style-attributes.ts @@ -12,6 +12,7 @@ import { */ import { isFeaturePluginBuild } from '../settings/blocks/feature-flags'; import { isString, isObject } from '../types/type-guards'; +import { hasSpacingStyleSupport } from '../utils/global-style'; type WithClass = { className: string; @@ -34,7 +35,7 @@ const parseStyle = ( style: unknown ): Record< string, unknown > => { }; export const useSpacingProps = ( attributes: unknown ): WithStyle => { - if ( ! isFeaturePluginBuild() ) { + if ( ! isFeaturePluginBuild() || ! hasSpacingStyleSupport() ) { return { style: {}, }; diff --git a/assets/js/utils/global-style.js b/assets/js/utils/global-style.js new file mode 100644 index 00000000000..0a05b0378bd --- /dev/null +++ b/assets/js/utils/global-style.js @@ -0,0 +1,8 @@ +/* eslint-disable @wordpress/no-unsafe-wp-apis */ +/** + * External dependencies + */ +import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor'; + +export const hasSpacingStyleSupport = () => + typeof __experimentalGetSpacingClassesAndStyles === 'function';