Skip to content
Draft
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
2 changes: 2 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

- DataViews: Add Shift+Click range selection through a shared `useSelectionProps` hook that layouts can adopt, wired up in the table and grid layouts.[#80046](https://github.com/WordPress/gutenberg/pull/80046)
- DataViewsPicker: Add Shift+Click range selection to the `picker-table`, `picker-grid`, and `picker-activity` layouts. [#80413](https://github.com/WordPress/gutenberg/pull/80413)
- DataViews: Unload offscreen infinite-scroll items in the list, table, and activity layouts.

### Bug Fix

- DataViewsPicker: Prevent the activity layout from creating an outer scrollbar.
- DataForms: Complete the `richtext` control's autocomplete semantics by associating the textbox with its suggestions list for assistive technology. [#80403](https://github.com/WordPress/gutenberg/pull/80403)
- DataViews: Fix the `list` layout ignoring the density setting, the refreshing state, and the loading state when `groupBy` is set. [#80255](https://github.com/WordPress/gutenberg/pull/80255)
- Fix Dataviews popover hover text color readability issue on WordPress 7.0. [#80105](https://github.com/WordPress/gutenberg/pull/80105)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Stack, VisuallyHidden } from '@wordpress/ui';
import ItemActions, { PrimaryActions } from '../../dataviews-item-actions';
import DataViewsContext from '../../dataviews-context';
import { ItemClickWrapper } from '../utils/item-click-wrapper';
import { useIntersectionObserver } from '../utils/use-infinite-scroll';
import type { NormalizedField, ViewActivityProps } from '../../../types';

function ActivityItem< Item >(
Expand Down Expand Up @@ -51,6 +52,7 @@ function ActivityItem< Item >(
const itemRef = useRef< HTMLDivElement >( null );
const registry = useRegistry();
const { paginationInfo } = useContext( DataViewsContext );
useIntersectionObserver( itemRef, posinset );

const { primaryActions, eligibleActions } = useMemo( () => {
// If an action is eligible for all items, doesn't need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import ActivityItem from './activity-item';
import type { ViewActivityProps } from '../../../types';

type ActivityItemsProps< Item > = ViewActivityProps< Item > & {
isInfiniteScroll?: boolean;
};

function isDefined< T >( item: T | undefined ): item is T {
return !! item;
}

export default function ActivityItems< Item >(
props: ViewActivityProps< Item >
props: ActivityItemsProps< Item >
) {
const { data, fields, getItemId, view } = props;
const { data, fields, getItemId, isInfiniteScroll, view } = props;

// Determine which fields to display based on view configuration
const titleField = fields.find( ( field ) => field.id === view.titleField );
Expand All @@ -23,7 +27,8 @@ export default function ActivityItems< Item >(
.map( ( fieldId ) => fields.find( ( f ) => fieldId === f.id ) )
.filter( isDefined );

return data.map( ( item, index ) => {
return data.map( ( item ) => {
const { position: posinset } = item as { position?: number };
return (
<ActivityItem
{ ...props }
Expand All @@ -33,7 +38,7 @@ export default function ActivityItems< Item >(
titleField={ titleField }
descriptionField={ descriptionField }
otherFields={ otherFields }
posinset={ view.infiniteScrollEnabled ? index + 1 : undefined }
posinset={ isInfiniteScroll ? posinset : undefined }
/>
);
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ export default function ViewActivity< Item >(
<>
<div
className={ wrapperClassName }
role={ view.infiniteScrollEnabled ? 'feed' : undefined }
role={ isInfiniteScroll ? 'feed' : undefined }
// @ts-ignore
inert={ isInert ? 'true' : undefined }
>
<ActivityItems< Item > { ...props } />
<ActivityItems< Item >
{ ...props }
isInfiniteScroll={ isInfiniteScroll }
/>
</div>
{ isInfiniteScroll && isLoading && (
<p className="dataviews-loading-more">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@use "../../../dataviews/style" as *;

.dataviews-view-activity {
position: relative;
margin: 0 0 auto;
padding: 0 var(--wpds-dimension-padding-2xl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
import getDataByGroup from '../utils/get-data-by-group';
import useSelectionProps from '../utils/use-selection-props';
import type { SelectionProps } from '../utils/use-selection-props';
import { useIntersectionObserver } from '../utils/use-infinite-scroll';

interface ListViewItemProps< Item > {
view: ViewListType;
Expand Down Expand Up @@ -163,6 +164,7 @@ function ListItem< Item >( {
const itemRef = useRef< HTMLDivElement >( null );
const labelId = `${ idPrefix }-label`;
const descriptionId = `${ idPrefix }-description`;
useIntersectionObserver( itemRef, posinset );

const registry = useRegistry();
const [ isHovered, setIsHovered ] = useState( false );
Expand Down Expand Up @@ -640,8 +642,11 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
className={ listClassName }
role={ view.infiniteScrollEnabled ? 'feed' : 'grid' }
>
{ data.map( ( item, index ) => {
{ data.map( ( item ) => {
const id = generateCompositeItemIdPrefix( item );
const { position: posinset } = item as {
position?: number;
};
return (
<ListItem
key={ id }
Expand All @@ -660,11 +665,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
onDropdownTriggerKeyDown={
onDropdownTriggerKeyDown
}
posinset={
view.infiniteScrollEnabled
? index + 1
: undefined
}
posinset={ isInfiniteScroll ? posinset : undefined }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dataviews-view-picker-activity {
position: relative;
margin: 0;
padding: 0;
// Fill the wrapper height so the footer sits at the bottom.
Expand Down
105 changes: 103 additions & 2 deletions packages/dataviews/src/components/dataviews-layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useContext,
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
} from '@wordpress/element';
Expand Down Expand Up @@ -43,6 +44,7 @@ import ColumnPrimary from './column-primary';
import { useScrollState } from './use-scroll-state';
import getDataByGroup from '../utils/get-data-by-group';
import useSelectionProps from '../utils/use-selection-props';
import { useIntersectionObserver } from '../utils/use-infinite-scroll';
import { PropertiesSection } from '../../dataviews-view-config/properties-section';
import { useDelayedLoading } from '../../../hooks/use-delayed-loading';

Expand All @@ -66,6 +68,98 @@ interface TableColumnFieldProps< Item > {
align?: 'start' | 'center' | 'end';
}

/**
* Table rows are not reliable native scroll anchors. Track the row closest to
* the viewport center on every scroll and restore it after observer-driven
* data changes.
*
* @param containerRef The DataViews scroll container.
* @param data The currently rendered table data.
* @param isInfiniteScroll Whether infinite scroll is active.
*/
function useTableScrollAnchor< Item >(
containerRef: React.RefObject< HTMLDivElement | null >,
data: Item[],
isInfiniteScroll: boolean
) {
const anchorRef = useRef< {
posinset: number;
viewportOffset: number;
} | null >( null );

useEffect( () => {
const container = containerRef.current;
if ( ! container || ! isInfiniteScroll ) {
anchorRef.current = null;
return;
}

const captureAnchor = () => {
const containerRect = container.getBoundingClientRect();
const centerY = containerRect.top + containerRect.height / 2;
const items = Array.from(
container.querySelectorAll( '[aria-posinset]' )
).filter( ( item ) => {
const itemRect = item.getBoundingClientRect();
return (
itemRect.bottom > containerRect.top &&
itemRect.top < containerRect.bottom
);
} );

if ( ! items.length ) {
return;
}

const anchor = items.reduce( ( closest, item ) => {
const itemRect = item.getBoundingClientRect();
const closestRect = closest.getBoundingClientRect();
const distance = Math.abs(
itemRect.top + itemRect.height / 2 - centerY
);
const closestDistance = Math.abs(
closestRect.top + closestRect.height / 2 - centerY
);
return distance < closestDistance ? item : closest;
} );

anchorRef.current = {
posinset: Number( anchor.getAttribute( 'aria-posinset' ) ),
viewportOffset:
anchor.getBoundingClientRect().top - containerRect.top,
};
};

captureAnchor();
container.addEventListener( 'scroll', captureAnchor );
return () => container.removeEventListener( 'scroll', captureAnchor );
}, [ containerRef, isInfiniteScroll ] );

useLayoutEffect( () => {
const container = containerRef.current;
const anchor = anchorRef.current;
if ( ! container || ! isInfiniteScroll || ! anchor ) {
return;
}

const anchorElement = container.querySelector(
`[aria-posinset="${ anchor.posinset }"]`
);
if ( ! anchorElement ) {
return;
}

const currentOffset =
anchorElement.getBoundingClientRect().top -
container.getBoundingClientRect().top;
const scrollAdjustment = currentOffset - anchor.viewportOffset;

if ( Math.abs( scrollAdjustment ) > 1 ) {
container.scrollTop += scrollAdjustment;
}
}, [ containerRef, data, isInfiniteScroll ] );
}

interface TableRowProps< Item > {
hasBulkActions: boolean;
item: Item;
Expand Down Expand Up @@ -148,6 +242,8 @@ function TableRow< Item >( {
showDescription = true,
infiniteScrollEnabled,
} = view;
const rowRef = useRef< HTMLTableRowElement >( null );
useIntersectionObserver( rowRef, posinset );
const columns = view.fields ?? [];
const hasPrimaryColumn =
( titleField && showTitle ) ||
Expand All @@ -156,6 +252,7 @@ function TableRow< Item >( {

return (
<tr
ref={ rowRef }
className={ clsx( 'dataviews-view-table__row', {
'is-selected': hasPossibleBulkAction && isSelected,
'has-bulk-actions': hasPossibleBulkAction,
Expand Down Expand Up @@ -284,6 +381,8 @@ function ViewTable< Item >( {
? fields.find( ( f ) => f.id === view.groupBy?.field )
: null;
const dataByGroup = groupField ? getDataByGroup( data, groupField ) : null;
const isInfiniteScroll = !! view.infiniteScrollEnabled && ! dataByGroup;
useTableScrollAnchor( containerRef, data, isInfiniteScroll );
// When grouping is enabled the rendered order is by group rather than the
// order of `data`; ranges follow what the user sees.
const orderedData = dataByGroup
Expand Down Expand Up @@ -388,7 +487,6 @@ function ViewTable< Item >( {
headerMenuRefs.current.delete( column );
}
};
const isInfiniteScroll = view.infiniteScrollEnabled && ! dataByGroup;
const isRtl = isRTL();
if ( ! hasData ) {
return (
Expand Down Expand Up @@ -645,6 +743,9 @@ function ViewTable< Item >( {
data.map( ( item, index ) => {
const id =
getItemId( item ) || index.toString();
const { position: posinset } = item as {
position?: number;
};
return (
<TableRow
key={ getItemId( item ) }
Expand Down Expand Up @@ -675,7 +776,7 @@ function ViewTable< Item >( {
}
posinset={
isInfiniteScroll
? index + 1
? posinset
: undefined
}
/>
Expand Down
Loading
Loading