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: add has polyfill for alignment #2814

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 .config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ module.exports = [
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
'frontend_block_components_alignment_has_polyfill': path.resolve( __dirname, '../src/block-components/alignment/frontend-has-polyfill.js' ),
},

output: {
Expand Down
1 change: 1 addition & 0 deletions .config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ module.exports = [
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
'frontend_block_components_alignment_has_polyfill': path.resolve( __dirname, '../src/block-components/alignment/frontend-has-polyfill.js' ),
},

output: {
Expand Down
1 change: 1 addition & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ function stackable_deactivation_cleanup() {
require_once( plugin_dir_path( __FILE__ ) . 'src/block/progress-circle/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/horizontal-scroller/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/tabs/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block-components/alignment/index.php' );

/**
* Welcome screen.
Expand Down
42 changes: 42 additions & 0 deletions src/block-components/alignment/frontend-has-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready'

class StackableAlignmentPolyfill {
init = () => {
const modifyCSS = () => {
const containers = document.querySelectorAll( '.stk-container' )
containers.forEach( container => {
const columnFlex = container.querySelector( '.stk--column-flex' )
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the selector we're trying to mimic, we need this to be for the direct descendent

Suggested change
const columnFlex = container.querySelector( '.stk--column-flex' )
const columnFlex = container.querySelector( ':scope > .stk--column-flex' )

if ( columnFlex ) {
container.classList.add( 'stk-container--has-child-column-flex' )
} else if ( ! columnFlex && container.classList.contains( 'stk-container--has-child-column-flex' ) ) {
container.classList.remove( 'stk-container--has-child-column-flex' )
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you still need this?

}
} )

const blocks = document.querySelectorAll( ':is(.stk-block-content, .stk-inner-blocks):not(.stk--column-flex)' )
blocks.forEach( block => {
const hasMargin = block.querySelector( ':scope > .stk--block-margin-top-auto, :scope > .stk--block-margin-bottom-auto' )
if ( hasMargin ) {
block.classList.add( 'stk--height-100' )
} else if ( ! hasMargin && block.classList.contains( 'stk--height-100' ) ) {
block.classList.remove( 'stk--height-100' )
}
} )
}

modifyCSS()
const observerOptions = {
childList: true,
subtree: true,
}

const observer = new MutationObserver( modifyCSS )
observer.observe( document, observerOptions )
}
}

window.stackableAlignmentPolyfill = new StackableAlignmentPolyfill()
domReady( window.stackableAlignmentPolyfill.init )
59 changes: 59 additions & 0 deletions src/block-components/alignment/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* In charge of loading the frontend polyfill for alignment :has() selector
* support
*/

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

if ( ! function_exists( 'stackable_load_alignment_frontend_polyfill_script' ) ) {
function stackable_load_alignment_frontend_polyfill_script() {

$user_agent = ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';

if ( ! $user_agent ) {
return;
}

$load_polyfill = false;

// Safari <= 15.3
if ( stripos( $user_agent, 'Safari/' ) !== false ) {
$start = stripos( $user_agent, 'Version/' ) + 8;
$end = strpos( $user_agent, ' ', $start );
$safari_version = substr( $user_agent, $start, $end - $start );

// Convert version string to an array of parts
$version_parts = explode( '.', $safari_version );

if (
// Safari < 15
( isset( $version_parts[ 0 ] ) && intval( $version_parts[ 0 ] ) < 15 ) ||
// Safari <= 15.3
( isset( $version_parts[ 0 ] ) && intval( $version_parts[ 0 ] ) == 15 &&
(
( isset( $version_parts[ 1 ] ) && intval( $version_parts[ 1 ] ) <= 3 ) ||
! isset( $version_parts[ 1 ] )
)
)
) {
$load_polyfill = true;
}
} else if ( stripos( $user_agent, 'Firefox/' ) !== false ) {
$load_polyfill = true;
}

if ( $load_polyfill ) {
wp_enqueue_script(
'stk-frontend-alginment-has-polyfill',
plugins_url( 'dist/frontend_block_components_alignment_has_polyfill.js', STACKABLE_FILE ),
array(),
STACKABLE_VERSION
);
}
}
add_action( 'wp_footer', 'stackable_load_alignment_frontend_polyfill_script' );
}
11 changes: 11 additions & 0 deletions src/block-components/alignment/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@
display: flex;
flex-direction: column;
}

/**
* Polyfill for browsers which do not support the `:has()` selector.
*/
.stk--height-100 {
height: 100%;
}
.stk-container--has-child-column-flex {
display: flex;
flex-direction: column;
}
Loading