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 (carousel block): show inner column margins in frontend #3257

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/block/carousel/frontend-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class _StackableCarousel {

this.slideEls[ this.currentSlide - 1 ].classList.add( 'stk-block-carousel__slide--active' )

this.fixInnerColumnMargins()
this.unpauseAutoplay()
}

Expand Down Expand Up @@ -588,6 +589,17 @@ class _StackableCarousel {

this.hasTouched = false
}

// Adds a div wrapper for inner columns with left and right margins
fixInnerColumnMargins = () => {
const innerColumns = this.sliderEl.querySelectorAll( '.has-inner-column-margin' )
innerColumns.forEach( el => {
const wrapper = document.createElement( 'div' )
el.parentNode.insertBefore( wrapper, el )
wrapper.appendChild( el )
el.classList.remove( 'has-inner-column-margin' )
} )
}
}

class StackableCarousel {
Expand Down
27 changes: 27 additions & 0 deletions src/block/carousel/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ function stackable_skip_loading_lazy_carousel_image( $value, $image ) {
add_filter( 'wp_img_tag_add_loading_attr', 'stackable_skip_loading_lazy_carousel_image', 10, 2 );
}
}

if ( ! function_exists( 'stackable_carousel_add_has_inner_column_margin_class' ) ) {
function stackable_carousel_add_has_inner_column_margin_class( $block_content, $block ) {
if ( empty( $block['innerBlocks'] ) ) {
return;
}

if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
return $block_content;
}

$html_tag = new WP_HTML_Tag_Processor( $block_content );

foreach ( $block['innerBlocks'] as $inner_block ) {
if ( $inner_block['blockName'] == 'stackable/column' && ( isset( $inner_block['attrs']['blockMargin']['left'] ) || isset( $inner_block['attrs']['blockMargin']['right'] ) ) ) {
if ( $html_tag->next_tag( array( 'class_name' => 'stk-'.$inner_block['attrs']['uniqueId'] ) ) ) {
error_log( 'has-inner-column-margin' );
$html_tag->add_class( 'has-inner-column-margin' );
}
}
}

return $html_tag->get_updated_html();
}

add_filter( 'render_block_stackable/carousel', 'stackable_carousel_add_has_inner_column_margin_class', 10, 2 );
}
Loading