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

feat(homepage-posts): infinite scroll #1845

Merged
merged 9 commits into from
Sep 19, 2024
4 changes: 4 additions & 0 deletions src/blocks/homepage-articles/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"type": "boolean",
"default": false
},
"infiniteScroll": {
"type": "boolean",
"default": false
},
"readMoreLabel": {
"type": "string",
"default": "Keep reading"
Expand Down
20 changes: 15 additions & 5 deletions src/blocks/homepage-articles/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class Edit extends Component< HomepageArticlesProps > {
mobileStack,
minHeight,
moreButton,
infiniteScroll,
showExcerpt,
showReadMore,
readMoreLabel,
Expand Down Expand Up @@ -434,11 +435,20 @@ class Edit extends Component< HomepageArticlesProps > {
</i>
) : (
! specificMode && (
<ToggleControl
label={ __( 'Show "Load more posts" Button', 'newspack-blocks' ) }
checked={ moreButton }
onChange={ () => setAttributes( { moreButton: ! moreButton } ) }
/>
<>
<ToggleControl
label={ __( 'Show "Load more posts" Button', 'newspack-blocks' ) }
checked={ moreButton }
onChange={ () => setAttributes( { moreButton: ! moreButton } ) }
/>
{ moreButton && (
<ToggleControl
label={ __( 'Infinite Scroll', 'newspack-blocks' ) }
checked={ infiniteScroll }
onChange={ () => setAttributes( { infiniteScroll: ! infiniteScroll } ) }
/>
) }
</>
)
) }
<ToggleControl
Expand Down
23 changes: 23 additions & 0 deletions src/blocks/homepage-articles/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ function buildLoadMoreHandler( blockWrapperEl ) {
return;
}
const postsContainerEl = blockWrapperEl.querySelector( '[data-posts]' );
const isInfiniteScroll = btnEl.getAttribute( 'data-infinite-scroll' );

// Set initial state flags.
let isFetching = false;
let isInfiniteScrolling = false;
let isEndOfData = false;

btnEl.addEventListener( 'click', () => {
Expand Down Expand Up @@ -85,6 +87,8 @@ function buildLoadMoreHandler( blockWrapperEl ) {
isFetching = false;

blockWrapperEl.classList.remove( 'is-loading' );

isInfiniteScrolling = false;
}

/**
Expand All @@ -97,6 +101,25 @@ function buildLoadMoreHandler( blockWrapperEl ) {
blockWrapperEl.classList.add( 'is-error' );
}
} );
if ( isInfiniteScroll ) {
// Create an intersection observer instance
const btnObserver = new IntersectionObserver(
entries => {
entries.forEach( entry => {
if ( entry.isIntersecting && ! isInfiniteScrolling ) {
btnEl.click();
isInfiniteScrolling = true;
}
} );
},
{
root: null,
rootMargin: '0px',
threshold: 1,
}
);
btnObserver.observe( btnEl );
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/blocks/homepage-articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,12 @@ class="<?php echo esc_attr( $classes ); ?>"
<?php

if ( $has_more_button ) :
$load_more = '';
if ( (bool) $attributes['infiniteScroll'] ) {
$load_more = 'data-infinite-scroll="true"';
}
?>
<button type="button" class="wp-block-button__link" data-next="<?php echo esc_url( $articles_rest_url ); ?>">
<button type="button" class="wp-block-button__link" <?php echo esc_attr( $load_more ); ?> data-next="<?php echo esc_url( $articles_rest_url ); ?>">
<span class="label">
<?php
if ( ! empty( $attributes['moreButtonText'] ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ declare global {
imageShape: string;
minHeight: integer;
moreButton: boolean;
infiniteScroll: boolean;
moreButtonText: string;
showAuthor: boolean;
showAvatar: boolean;
Expand Down