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

fix (icon label): change icon gap #3209

Merged
merged 10 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
1 change: 1 addition & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ function is_frontend() {
require_once( plugin_dir_path( __FILE__ ) . 'src/block-components/alignment/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/columns/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/timeline/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/icon-label/deprecated.php' );
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/block/icon-label/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import {
} from '~stackable/block-components'

const deprecated = [
{
attributes: attributes( '3.13.1' ),
save: withVersion( '3.13.1' )( Save ),
isEligible: ( attributes, innerBlocks ) => {
const hasIconSize = innerBlocks[ 0 ].attributes.iconSize ? true : false
const hasIconGap = attributes.iconGap ? true : false
const hasIconGap2 = attributes.iconGap2 ? true : false
return hasIconGap || ( hasIconSize && ! hasIconGap2 )
},
migrate: ( attributes, innerBlocks ) => {
const newAttributes = { ...attributes }

const { iconGap } = attributes

const iconBlockAttributes = innerBlocks[ 0 ].attributes
const { iconSize } = iconBlockAttributes

const _iconSize = iconSize ? iconSize : 36
const _iconGap = iconGap ? iconGap : 64

const newIconGap = _iconGap - _iconSize >= 0 ? _iconGap - _iconSize : 0
newAttributes.iconGap2 = newIconGap === 28 ? '' : newIconGap
newAttributes.iconGap = ''

return newAttributes
},
},
{
// Support the new shadow color.
attributes: attributes( '3.12.11' ),
Expand Down
32 changes: 32 additions & 0 deletions src/block/icon-label/deprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if ( ! function_exists( 'stackable_render_block_icon_label' ) ) {
/**
* Adds a special class if block uses the old icon gap attribute
*/
function stackable_render_block_icon_label( $block_content, $block ) {
$attributes = $block[ 'attrs' ];

// check if inner blocks contain an icon block
if ( ! isset( $block[ 'innerBlocks' ][0] ) || ! isset( $block[ 'innerBlocks' ][0]['blockName'] ) || $block[ 'innerBlocks' ][0]['blockName'] !== 'stackable/icon' ) {
return $block_content;
}

$icon_block_attrs = $block[ 'innerBlocks' ][0]['attrs'];

// check if the block uses the old icon gap attribute
$use_old_styles = ( isset( $attributes[ 'iconGap' ] ) || isset( $icon_block_attrs['iconSize'] ) ) && ! isset( $attributes[ 'iconGap2' ] );

if ( $use_old_styles ) {
return preg_replace( '/stk-block-icon-label/i', 'stk-block-icon-label stk-block-icon-label--use-flex-basis', $block_content );
}

return $block_content;
}
add_filter( 'render_block_stackable/icon-label', 'stackable_render_block_icon_label', 10, 2 );
}
4 changes: 2 additions & 2 deletions src/block/icon-label/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ const Edit = props => {
>
<AdvancedRangeControl
label={ __( 'Icon Gap', i18n ) }
attribute="iconGap"
attribute="iconGap2"
responsive="all"
min={ 0 }
sliderMax={ 300 }
placeholder="64"
placeholder="28"
/>
</PanelAdvancedSettings>

Expand Down
8 changes: 6 additions & 2 deletions src/block/icon-label/editor.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.stk-block-icon-label [data-type="stackable/icon"] {
flex: 0 0 64px;
flex: 0 0 0;
}
// Icon label can collapse in mobile in the editor.
.stk-preview-device-mobile .stk-block-icon-label .block-editor-block-list__layout [data-type="stackable/icon"] {
flex: 0 0 64px;
flex: 0 0 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if we remove the flex definition completely since this is for the old 0 0 64px?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it still needs the flex, but i changed it to flex: 0 instead.

}
.stk-preview-device-mobile .stk-block-icon-label .block-editor-block-list__layout [data-type="stackable/heading"] {
flex: 1 1 0;
Expand All @@ -14,3 +14,7 @@
max-width: unset !important;
}
}

.stk-block-icon-label .stk-inner-blocks .block-editor-block-list__layout {
gap: 28px;
}
6 changes: 6 additions & 0 deletions src/block/icon-label/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ export const attributes = ( version = VERSION ) => {
ConditionalDisplay.addAttributes( attrObject )
attrObject.add( {
attributes: {
// iconGap is deprecated but is kept for migration purposes
iconGap: {
type: 'number',
stkResponsive: true,
default: '',
},
iconGap2: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment on iconGap that it's deprecated but kept here for migration purposes

bfintal marked this conversation as resolved.
Show resolved Hide resolved
type: 'number',
stkResponsive: true,
default: '',
},
},
versionAdded: '3.0.0',
versionDeprecated: '',
Expand Down
12 changes: 6 additions & 6 deletions src/block/icon-label/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ const Styles = props => {
<BlockCss
{ ...propsToPass }
renderIn="save"
selector=".stk-block-icon"
attrName="iconGap"
selector=".stk-inner-blocks"
attrName="iconGap2"
key="iconGap-save"
styleRule="flexBasis"
styleRule="gap"
format="%spx"
responsive="all"
/>
<BlockCss
{ ...propsToPass }
renderIn="edit"
selector={ `.stk-inner-blocks [data-block][data-type="stackable/icon"]` }
attrName="iconGap"
selector=".stk-inner-blocks .block-editor-block-list__layout"
attrName="iconGap2"
key="iconGap"
styleRule="flexBasis"
styleRule="gap"
format="%spx"
responsive="all"
/>
Expand Down
18 changes: 15 additions & 3 deletions src/block/icon-label/style.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.stk-block-icon-label {
// Default width of the icon width.
.stk-block-icon {
flex: 0 0 64px;
// used by iconGap2
.stk-inner-blocks {
gap: 28px;
}

// Remove margins and make sure the icon and text are both centered.
:is(.stk-block-icon, .stk-block-heading) {
--stk-block-margin-bottom: 0;
Expand All @@ -25,3 +26,14 @@
width: inherit;
}
}

// Deprecated styles: we changed how icon gap works. Before it was flex basis, now it's a gap
.stk-block-icon-label.stk-block-icon-label--use-flex-basis {
// Default width of the icon width.
.stk-block-icon {
flex: 0 0 64px;
}
.stk-inner-blocks {
gap: 0;
}
}