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 5 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/index.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
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;
}
25 changes: 25 additions & 0 deletions src/block/icon-label/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// Exit if accessed directly.
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename this file to deprecated.php or something so we can denote that it's for deprecation purposes?

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 the block uses the old icon gap attribute
$use_old_styles = ( isset( $attributes[ 'iconGap' ] ) || isset( $block[ 'innerBlocks' ][0]['attrs']['iconSize'] ) ) && ! isset( $attributes[ 'iconGap2' ] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Check first for the presence of the elements in $block['innerBlocks'] to prevent fatal errors.


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 );
}
5 changes: 5 additions & 0 deletions src/block/icon-label/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const attributes = ( version = VERSION ) => {
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
21 changes: 21 additions & 0 deletions src/block/icon-label/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Styles = props => {

return (
<>
{ /* For the old icon gap */ }
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you should be able to remove the BlockCss entry for the old iconGap here and the block should not error out.

<BlockCss
{ ...propsToPass }
renderIn="save"
Expand All @@ -46,6 +47,26 @@ const Styles = props => {
format="%spx"
responsive="all"
/>
<BlockCss
{ ...propsToPass }
renderIn="save"
selector=".stk-inner-blocks"
attrName="iconGap2"
key="iconGap-save"
styleRule="gap"
format="%spx"
responsive="all"
/>
<BlockCss
{ ...propsToPass }
renderIn="edit"
selector=".stk-inner-blocks .block-editor-block-list__layout"
attrName="iconGap2"
key="iconGap"
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,20 @@
.stk-block-icon-label {
// Default width of the icon width.
.stk-block-icon {
flex: 0 0 64px;
// For old icon gap
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this css to the bottom (isolating it from the rest of the "current" css code) and then label it as "deprecated"

&.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;
}
}

// 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 Down
Loading