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 slider error on Block creation #1823

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Changes from all 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
61 changes: 35 additions & 26 deletions src/blocks/blocks/slider/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const Edit = ({

const initObserver = useRef( null );
const sliderRef = useRef( null );
const containerRef = useRef( null );

useEffect( () => {
try {
Expand All @@ -89,37 +90,34 @@ const Edit = ({

useEffect( () => {

const container = document.querySelector( `#${ attributes.id }` ) ?? getEditorIframe()?.contentDocument?.querySelector( `#${ attributes.id }` );

if ( container ) {
if ( containerRef.current ) {
initObserver.current = new IntersectionObserver( ( entries ) => {
entries.forEach( entry => {
if ( entry.isIntersecting && 0 <= entry.intersectionRect.height ) {
if ( attributes.images.length ) {
if ( attributes.images && 0 < attributes.images.length ) {
initSlider();
initObserver.current?.unobserve( container );
initObserver.current?.unobserve( containerRef.current );
}
}
});
}, options );

initObserver.current?.observe( container );
initObserver.current?.observe( containerRef.current );
}

return () => {
if ( attributes?.images?.length ) {
sliderRef?.current?.destroy();
}
initObserver.current?.disconnect();
};
}, [ attributes.id ]);

useEffect( () => {
if ( attributes.images.length ) {
if ( attributes.images && 0 < attributes.images.length && attributes.id ) {
setSelectedImage( null );

if ( null !== sliderRef.current && attributes.id ) {
initSlider();
}
initSlider();
}
}, [ isSelected, attributes.id, sliderRef.current, attributes.images, attributes.width ]);

Expand All @@ -136,12 +134,12 @@ const Edit = ({
const initSlider = () => {

// Clean up old references.
if ( null !== sliderRef.current ) {
if ( Boolean( sliderRef.current ) ) {
sliderRef.current?.destroy?.();
sliderRef.current = undefined;
}

const iframe = getEditorIframe();
const container = document?.querySelector( `#${ attributes.id }` ) ?? iframe?.contentDocument?.querySelector( `#${ attributes.id }` );

const config = {
type: 'carousel',
Expand All @@ -161,25 +159,37 @@ const Edit = ({
}
};

// This will prevent the slider from initializing if the block is not in the DOM that it can reach (like Inserter block preview)
if ( ! Boolean( document.querySelector( `#${ attributes.id }` ) ?? iframe?.contentDocument?.querySelector( `#${ attributes.id }` ) ) ) {
return;
}

/**
* Init the Slider inside the iframe.
*/
if ( Boolean( iframe ) ) {
const initFrame = () => {
if ( iframe?.contentWindow?.Glide ) {
sliderRef.current = new iframe.contentWindow.Glide( container, config ).mount();
}
};
try {
if ( Boolean( iframe ) ) {
const initFrame = () => {
if ( iframe?.contentWindow?.Glide ) {
sliderRef.current = new iframe.contentWindow.Glide( containerRef.current, config ).mount();
}
};

if ( ! Boolean( iframe.contentDocument?.querySelector( '#glidejs-js' ) ) ) {
if ( ! Boolean( iframe.contentDocument?.querySelector( '#glidejs-js' ) ) ) {

// Load the JS file into the iframe.
copyScriptAssetToIframe( '#glidejs-js', initFrame );
// Load the JS file into the iframe.
copyScriptAssetToIframe( '#glidejs-js', initFrame );
} else {
initFrame();
}
} else {
initFrame();
sliderRef.current = new window.Glide( containerRef.current, config ).mount();
}
} else {
sliderRef.current = new window.Glide( container, config ).mount();
} catch ( e ) {

// We don't want to break the block if the slider fails to init.
// The main cause is when the block runs inside an iFrame and can not access the root node.
console.warn( e );
}
};

Expand All @@ -192,8 +202,6 @@ const Edit = ({
caption: image.caption
}) )
});

initSlider();
};

const changePerView = value => {
Expand Down Expand Up @@ -257,6 +265,7 @@ const Edit = ({
id={ attributes.id }
className="glide"
style={ inlineStyles }
ref={ containerRef }
>
<div className="glide__track" data-glide-el="track">
<div
Expand Down
Loading