-
Notifications
You must be signed in to change notification settings - Fork 62
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
bfintal
merged 8 commits into
develop
from
fix/alignment-browser-compatibility-has-pseudo-selector
Aug 8, 2023
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0130633
added has polyfill
mxkae bc36f66
fix: by default do not load the polyfill
4959336
changed names and added comments
e8547fd
rename classes, revert specificity
mxkae 4292e64
minor fix
mxkae 2ec2129
update detection for Safari
mxkae ee5b595
change detection for Safari
mxkae 482655c
Update src/block-components/alignment/index.php
bfintal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) | ||
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' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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