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

### Enhancements

- Fade the trailing edge of a table layout while there are more columns to scroll to, including tables without actions ([#85949](https://github.com/WordPress/gutenberg/pull/85949)).
- Use `Button` and `IconButton` from `@wordpress/ui` in the bulk actions bar. Hovered actions now get a background fill, a running action shows a spinner in place of its label and is announced to screen readers, and the `dataviews-bulk-actions__action` class is removed ([#85939](https://github.com/WordPress/gutenberg/pull/85939)).
- Use the UI SelectControl for Sort by and relative-date Unit settings ([#86167](https://github.com/WordPress/gutenberg/pull/86167)).
- DataForm: Tighten the line height of field values in the panel layout, so values that wrap onto several lines take less room ([#85828](https://github.com/WordPress/gutenberg/pull/85828)).
Expand All @@ -29,6 +30,7 @@

### Bug Fixes

- Show the divider on the sticky table actions column at the start of the scroll range in RTL, and update it when columns are added or the table resizes ([#85949](https://github.com/WordPress/gutenberg/pull/85949)).
- DataForm: Keep the first line of a panel field label that wraps level with its value ([#85828](https://github.com/WordPress/gutenberg/pull/85828)).
- Replace the pagination page select with a page number input above 100 pages, so a dataset with tens of thousands of pages no longer freezes the browser ([#85303](https://github.com/WordPress/gutenberg/pull/85303)).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
Spinner as WCSpinner,
Popover as WCPopover,
} from '@wordpress/components';
import { useMergeRefs, useResizeObserver } from '@wordpress/compose';
import {
useMergeRefs,
useRefEffect,
useResizeObserver,
} from '@wordpress/compose';
import {
useContext,
useEffect,
Expand Down Expand Up @@ -369,10 +373,26 @@ function ViewTable< Item >( {
hadSelectionRef.current = showBulkActions;
}, [ showBulkActions ] );

const { isHorizontalScrollEnd, isVerticallyScrolled } = useScrollState( {
scrollContainerRef: containerRef,
enabledHorizontal: !! actions?.length,
} );
const { isHorizontalScrollEnd, isVerticallyScrolled, scrollContentRef } =
useScrollState( { scrollContainerRef: containerRef } );
// Size the scroll fade to the visible area of the scroll container it is
// stuck in. Written to the DOM directly, like the bulk actions toolbar
// position, and measured whenever the fade mounts.
const scrollFadeRef = useRefEffect( ( fade: HTMLDivElement ) => {
const container = fade.parentElement;
if ( ! container ) {
return;
}
const syncScrollFadeHeight = () =>
fade.style.setProperty(
'--wp-dataviews-table-scroll-fade-height',
`${ container.clientHeight }px`
);
syncScrollFadeHeight();
const resizeObserver = new ResizeObserver( syncScrollFadeHeight );
resizeObserver.observe( container );
return () => resizeObserver.disconnect();
}, [] );

const disableHeaderControls = showBulkActions;

Expand Down Expand Up @@ -480,6 +500,15 @@ function ViewTable< Item >( {

return (
<>
{ /* With actions, the sticky actions column carries the fade. */ }
{ ! actions?.length && (
<div
className="dataviews-view-table__scroll-fade"
ref={ scrollFadeRef }
hidden={ isHorizontalScrollEnd }
aria-hidden="true"
/>
) }
{ ( hasBulkActions || isActionInProgress ) && (
<div
className={ clsx(
Expand Down Expand Up @@ -511,6 +540,7 @@ function ViewTable< Item >( {
'has-media-aspect-ratio': !! mediaAspectRatio,
} ) }
style={ tableStyle }
ref={ scrollContentRef }
aria-busy={ isLoading }
aria-describedby={ tableNoticeId }
role={ isInfiniteScroll ? 'feed' : undefined }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@
width: 1px;
background-color: var(--wpds-color-stroke-surface-neutral-weak);
}

// Fades the columns scrolling under the sticky actions column, in
// the cell's own background so selected rows keep their color.
&::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
right: 100%;
width: var(--wpds-dimension-size-lg);
pointer-events: none;
background-color: inherit;
// RTL styles don't flip `mask-image`, so flip it explicitly.
mask-image: linear-gradient(to left, #000, transparent) #{"/*rtl:linear-gradient(to right, #000, transparent);*/"};
}
}

&.dataviews-view-table__checkbox-column {
Expand Down Expand Up @@ -349,6 +364,34 @@
@include dataviews-refreshing();
}

// Fades the trailing edge of the visible area while there are more columns to
// scroll to, for tables without actions (their rows share one background).
// Sticky and zero height like the bulk actions toolbar, so it stays over the
// visible area without taking up space.
.dataviews-view-table__scroll-fade {
position: sticky;
inset-block-start: 0;
inset-inline-start: 0;
z-index: 1;
height: 0;
flex-shrink: 0;
pointer-events: none;
// Inherit the surface behind the table, like the sticky actions column.
background-color: inherit;

&::after {
content: "";
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
width: var(--wpds-dimension-size-lg);
height: var(--wp-dataviews-table-scroll-fade-height, 0);
background-color: inherit;
// RTL styles don't flip `mask-image`, so flip it explicitly.
mask-image: linear-gradient(to left, #000, transparent) #{"/*rtl:linear-gradient(to right, #000, transparent);*/"};
}
}

.dataviews-view-table__bulk-actions-overlay {
position: sticky;
inset-block-start: 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import type { RefObject } from 'react';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { isRTL } from '@wordpress/i18n';
import { useCallback, useLayoutEffect, useState } from '@wordpress/element';

const isScrolledToEnd = ( element: Element ) => {
if ( isRTL() ) {
const scrollLeft = Math.abs( element.scrollLeft );
return scrollLeft <= 1;
}

return element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;
};
// `scrollLeft` is negative in RTL, so its absolute value is the distance
// scrolled from the start in both directions.
const isScrolledToEnd = ( element: Element ) =>
Math.abs( element.scrollLeft ) + element.clientWidth >=
element.scrollWidth - 1;

/**
* A hook to track the scroll state of a container element.
*
* Returns whether the container has been scrolled vertically (for sticky header styling)
* and whether it has reached the horizontal scroll end (for sticky actions column styling).
* and whether it has reached the horizontal scroll end (for the sticky actions column
* and the scroll fade).
*
* The state is measured again when the container or the scrolled content resizes, so
* attach the returned `scrollContentRef` to the element that scrolls inside the container.
*
* The current way receives "refs" as arguments, but it lacks a mechanism to detect when a ref has changed.
* As a result, when the "ref" is updated and attached to a new div, the computation should trigger again.
* However, this isn't possible in the current setup because the hook is unaware that the ref has changed.
*
* See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.
*
* @param params The parameters for the hook.
* @param params.scrollContainerRef The ref to the scroll container element.
* @param [params.enabledHorizontal=false] Whether to track horizontal scroll end.
* @return The scroll state.
* @param params The parameters for the hook.
* @param params.scrollContainerRef The ref to the scroll container element.
* @return The scroll state, and the ref to attach to the scrolled content.
*/
export function useScrollState( {
scrollContainerRef,
enabledHorizontal = false,
}: {
scrollContainerRef: RefObject< HTMLDivElement | null >;
enabledHorizontal?: boolean;
} ): { isHorizontalScrollEnd: boolean; isVerticallyScrolled: boolean } {
} ): {
isHorizontalScrollEnd: boolean;
isVerticallyScrolled: boolean;
scrollContentRef: ( element: Element | null ) => void;
} {
const [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =
useState( false );
const [ isVerticallyScrolled, setIsVerticallyScrolled ] = useState( false );
Expand All @@ -45,28 +46,35 @@ export function useScrollState( {
return;
}

if ( enabledHorizontal ) {
setIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );
}

setIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );
setIsVerticallyScrolled( scrollContainer.scrollTop > 0 );
}, [ scrollContainerRef, enabledHorizontal ] );
useEffect( () => {
if ( typeof window === 'undefined' || ! scrollContainerRef.current ) {
return () => {};
}
}, [ scrollContainerRef ] );

const [ scrollContent, scrollContentRef ] = useState< Element | null >(
null
);

// A layout effect measures before the first paint, so the scroll cues
// don't flash.
useLayoutEffect( () => {
const scrollContainer = scrollContainerRef.current;
if ( ! scrollContainer ) {
return () => {};
}

handleScroll();
scrollContainer.addEventListener( 'scroll', handleScroll );
window.addEventListener( 'resize', handleScroll );
const resizeObserver = new ResizeObserver( handleScroll );
resizeObserver.observe( scrollContainer );
if ( scrollContent ) {
resizeObserver.observe( scrollContent );
}

return () => {
scrollContainer.removeEventListener( 'scroll', handleScroll );
window.removeEventListener( 'resize', handleScroll );
resizeObserver.disconnect();
};
}, [ scrollContainerRef, enabledHorizontal, handleScroll ] );
}, [ scrollContainerRef, handleScroll, scrollContent ] );

return { isHorizontalScrollEnd, isVerticallyScrolled };
return { isHorizontalScrollEnd, isVerticallyScrolled, scrollContentRef };
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import filterSortAndPaginate from '../../utils/filter-sort-and-paginate';
globalThis.wpVitest.mockMatchMedia();
globalThis.wpVitest.mockPointerEvent();
globalThis.wpVitest.mockCSSSupports();
globalThis.wpVitest.mockResizeObserver();

vi.mock( import( '@wordpress/a11y' ), async ( importOriginal ) => ( {
...( await importOriginal() ),
Expand Down
Loading
Loading