Skip to content

Commit

Permalink
Remove "scroll to a selected item" overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
okmttdhr committed Nov 25, 2024
1 parent 7115e04 commit a20e140
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions client/sites/components/sites-dataviews/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SiteExcerptData } from '@automattic/sites';
import { usePrevious } from '@wordpress/compose';
import { DataViews, Field } from '@wordpress/dataviews';
import { useI18n } from '@wordpress/react-i18n';
import { useCallback, useMemo, useRef, useLayoutEffect } from 'react';
import { useCallback, useMemo } from 'react';
import JetpackLogo from 'calypso/components/jetpack-logo';
import TimeSince from 'calypso/components/time-since';
import { SitePlan } from 'calypso/sites-dashboard/components/sites-site-plan';
Expand Down Expand Up @@ -58,34 +57,6 @@ const DotcomSitesDataViews = ( {
const { __ } = useI18n();
const userId = useSelector( getCurrentUserId );

// Scroll to selected site in the list when in list view.
const scrollContainerRef = useRef< HTMLElement >();
const previousDataViewsState = usePrevious( dataViewsState );
const previousSelectedItem = usePrevious( selectedItem );
useLayoutEffect( () => {
if ( ! scrollContainerRef.current || previousDataViewsState?.type !== dataViewsState.type ) {
scrollContainerRef.current = document.querySelector( '.dataviews-view-list' ) as HTMLElement;
}

if ( ! previousSelectedItem && selectedItem && dataViewsState.type === 'list' ) {
window.setTimeout(
() => scrollContainerRef.current?.querySelector( 'li.is-selected' )?.scrollIntoView(),
300
);
return;
}

if ( previousDataViewsState?.page !== dataViewsState.page ) {
scrollContainerRef.current?.scrollTo( 0, 0 );
}
}, [
dataViewsState.type,
dataViewsState.page,
selectedItem,
previousDataViewsState,
previousSelectedItem,
] );

// By default, DataViews is in an "uncontrolled" mode, meaning the current selection is handled internally.
// However, each time a site is selected, the URL changes, so, the component is remounted and the current selection is lost.
// To prevent that, we want to use DataViews in "controlled" mode, so that we can pass an initial selection during initial mount.
Expand Down

0 comments on commit a20e140

Please sign in to comment.