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

Handle no posts without crashing #547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
15 changes: 8 additions & 7 deletions src/blocks/carousel/create-swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,35 @@ export default function createSwiper( els, config = {} ) {
touchStartPreventDefault: false,
on: {
init() {
forEachNode( this.wrapperEl.querySelectorAll( '.swiper-slide' ), slide =>
forEachNode( swiper.wrapperEl.querySelectorAll( '.swiper-slide' ), ( slide ) =>
deactivateSlide( slide )
);

activateSlide( this.slides[ this.activeIndex ] ); // Set-up our active slide.
const maybeActiveSlide = swiper.slides[ swiper.activeIndex ];
maybeActiveSlide && activateSlide( maybeActiveSlide ); // Set-up our active slide.
},

slideChange() {
const currentSlide = this.slides[ this.activeIndex ];
const currentSlide = swiper.slides[ swiper.activeIndex ];

deactivateSlide( this.slides[ this.previousIndex ] );
deactivateSlide( swiper.slides[ swiper.previousIndex ] );

activateSlide( currentSlide );

/**
* If we're autoplaying, don't announce the slide change, as that would
* be supremely annoying.
*/
if ( ! this.autoplay.running ) {
if ( ! swiper.autoplay.running ) {
// Announce the contents of the slide.
const currentImage = currentSlide.querySelector( 'img' );
const alt = currentImage ? currentImage.alt : false;

const slideInfo = sprintf(
/* translators: current slide number and the total number of slides */
__( 'Slide %s of %s', 'newspack-blocks' ),
this.realIndex + 1,
this.pagination.bullets.length
swiper.realIndex + 1,
swiper.pagination.bullets.length
);

speak(
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Edit extends Component {
ref={ this.paginationRef }
/>
</Fragment>
) }
) || (<div className="swiper-wrapper"></div> ) }
</div>
<InspectorControls>
<PanelBody title={ __( 'Display Settings' ) } initialOpen={ true }>
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/carousel/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ if ( typeof window !== 'undefined' ) {
const blocksArray = Array.from(
document.querySelectorAll( '.wp-block-newspack-blocks-carousel' )
);
blocksArray.forEach( block => {
blocksArray.forEach( ( block ) => {
createSwiper(
{
block,
container: block.querySelector( '.swiper-container' ),
container: block.classList.contains('swiper-container')
? block
: block.querySelector( '.swiper-container' ),
prev: block.querySelector( '.swiper-button-prev' ),
next: block.querySelector( '.swiper-button-next' ),
pagination: block.querySelector( '.swiper-pagination-bullets' ),
Expand Down