Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b58dce5
Try adding optional infinite scroll to dataviews
tellthemachines Jul 29, 2025
a8042d9
try infinite scroll on all view types
tellthemachines Jul 30, 2025
5ef7214
make `infiniteScrollHandler` a property of `paginationInfo`
tellthemachines Jul 30, 2025
8b6f5f4
Add some more moons to storybook fixtures
tellthemachines Aug 4, 2025
61736d3
Add infinite scroll story
tellthemachines Aug 4, 2025
c8f47ab
update tests referring to data fixture
tellthemachines Aug 4, 2025
83c5644
Add aria roles for feed pattern
tellthemachines Aug 5, 2025
6269827
add feed aria roles to list and table layouts
tellthemachines Aug 5, 2025
afc4ac7
try toggle group control
tellthemachines Aug 5, 2025
c7b4fba
Changelog entry
tellthemachines Aug 5, 2025
c86bb1c
Fix storybook example
tellthemachines Aug 7, 2025
47dec0d
Fix styling issues with story
tellthemachines Aug 7, 2025
ac60b59
Hide pagination when infinite scroll is enabled
tellthemachines Aug 7, 2025
9a7d7aa
Remove memoed const
tellthemachines Aug 11, 2025
bf8010e
Make posinset optional prop and remove semantics from list layout
tellthemachines Aug 11, 2025
55302da
add aria attribs to div wrapper in list layout
tellthemachines Aug 11, 2025
fd50cfa
update changelog
tellthemachines Aug 11, 2025
be34116
Add loading more text
tellthemachines Aug 11, 2025
fd42a2c
Disable when data by group
tellthemachines Aug 11, 2025
b1ecc53
move infiniteScroll to base view
tellthemachines Aug 13, 2025
074c8ae
Try simple toggle and hiding per page values
tellthemachines Aug 13, 2025
3d54442
Center-align "loading more results"
tellthemachines Aug 13, 2025
1a9d90a
Change loading more text to spinner
tellthemachines Aug 13, 2025
07b49dc
Remove post list handler meant for testing
tellthemachines Aug 14, 2025
0cb6ad2
Update property name and clean up
tellthemachines Aug 15, 2025
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
Prev Previous commit
Next Next commit
Add infinite scroll story
  • Loading branch information
tellthemachines committed Aug 13, 2025
commit 61736d3d371bce1de9601c0f6862ca8193ad2299
118 changes: 118 additions & 0 deletions packages/dataviews/src/components/dataviews/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { Meta } from '@storybook/react';
import {
useState,
useMemo,
useCallback,
useEffect,
createInterpolateElement,
} from '@wordpress/element';
import {
Expand Down Expand Up @@ -389,3 +391,119 @@ export const GroupByLayout = () => {
/>
);
};

export const InfiniteScroll = () => {
const [ view, setView ] = useState< View >( {
type: LAYOUT_GRID,
search: '',
page: 1,
perPage: 6, // Start with a small number to demonstrate pagination
filters: [],
fields: [ 'satellites' ],
titleField: 'title',
descriptionField: 'description',
mediaField: 'image',
layout: {
badgeFields: [ 'satellites' ],
infiniteScroll: true, // Enable infinite scroll by default
},
} );
const { data: shownData } = useMemo( () => {
return filterSortAndPaginate( data, view, fields );
}, [ view ] );

// Custom pagination handler that simulates server-side pagination
const [ allLoadedRecords, setAllLoadedRecords ] = useState< SpaceObject[] >(
[]
);
const [ isLoadingMore, setIsLoadingMore ] = useState( false );

const totalItems = data.length;
const totalPages = Math.ceil( totalItems / 6 ); // perPage is 6.
const currentPage = view.page || 1;
const hasMoreData = currentPage < totalPages;
const getItemId = ( item: {
id: any;
title?: string;
description?: string;
image?: string;
type?: string;
isPlanet?: boolean;
categories?: string[];
satellites?: number;
date?: string;
datetime?: string;
email?: string;
} ) => item.id.toString();

const infiniteScrollHandler = useCallback( () => {
if ( isLoadingMore || currentPage >= totalPages ) {
return;
}

setIsLoadingMore( true );

setView( {
...view,
page: currentPage + 1,
} );
}, [ isLoadingMore, currentPage, totalPages, view ] );

// Initialize data on first load or when view changes significantly
useEffect( () => {
if ( currentPage === 1 ) {
// First page - replace all data
setAllLoadedRecords( shownData );
} else {
// Subsequent pages - append to existing data
setAllLoadedRecords( ( prev ) => {
const existingIds = new Set( prev.map( getItemId ) );
const newRecords = shownData.filter(
( record ) => ! existingIds.has( getItemId( record ) )
);
return [ ...prev, ...newRecords ];
} );
}
setIsLoadingMore( false );
}, [ view.search, view.filters, view.perPage, currentPage ] );

const paginationInfo = {
totalItems,
totalPages,
infiniteScrollHandler,
};

return (
<div style={ { height: '800px', overflow: 'auto' } }>
<Text
style={ {
marginBottom: '16px',
padding: '8px',
background: '#f0f0f0',
borderRadius: '4px',
display: 'block',
} }
>
{ __( 'Infinite Scroll Demo' ) }: { allLoadedRecords.length } of{ ' ' }
{ totalItems } items loaded.
{ isLoadingMore && __( 'Loading more…' ) }
{ ! hasMoreData && __( 'All items loaded!' ) }
</Text>
<DataViews
getItemId={ ( item ) => item.id.toString() }
paginationInfo={ paginationInfo }
data={ allLoadedRecords }
view={ view }
fields={ fields }
onChangeView={ setView }
actions={ actions }
isLoading={ isLoadingMore }
Copy link
Member

Choose a reason for hiding this comment

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

I wanted to try the "loading" experience. Layouts need to combine it with "has data":

  • when there is no data, display a spinner instead like they do (e.g., table)
  • when there is actually data but is still loading: what do we do? display a "loading item" at the end of the list?
Screen.Recording.2025-08-06.at.13.35.36.mov

Copy link
Member

Choose a reason for hiding this comment

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

Additionally, what do we do about the existing pagination? The current state is that we display the last page that has been fetched. And interacting with it doesn't do anything. Perhaps we hide it if infinite scroll is on?

Screen.Recording.2025-08-06.at.13.37.14.mov

Copy link
Member

Choose a reason for hiding this comment

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

And interacting with it doesn't do anything. Perhaps we hide it if infinite scroll is on?

Interacting does something, but I don't think it's expected. The page jumps when you select pages after all have loaded, and the n items changes.

Kapture.2025-08-07.at.14.40.23.mp4

I wonder if infinite scroll needs some sort of custom footer, e.g., how many have been loaded and/or selected.

Otherwise it's working nicely for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additionally, what do we do about the existing pagination? The current state is that we display the last page that has been fetched. And interacting with it doesn't do anything. Perhaps we hide it if infinite scroll is on?

Yeah I think we just hide the pagination bit.

how many have been loaded and/or selected

This bit is working fine so we can leave it as is for now.

when there is no data, display a spinner instead like they do (e.g., table)

When would we have no data? If the server doesn't respond, shouldn't we display an error message in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I think I get it. When the data is slow to load on the page, the spinner displays. That'll happen for the initial data whether infinite scroll is enabled or not.

If we're loading additional data with infinite scroll, that's where we might display a "loading items...". I can have a play with that and test it on the Pages screen. It might need some design input though.

defaultLayouts={ {
[ LAYOUT_GRID ]: {},
[ LAYOUT_LIST ]: {},
[ LAYOUT_TABLE ]: {},
} }
/>
</div>
);
};