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

DataViews: Add grid keyboard navigation #68225

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function useHasAPossibleBulkAction< Item >(
item: Item
) {
return useMemo( () => {
return actions.some( ( action ) => {
return actions?.some( ( action ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not related to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to move it out to another PR?

return (
action.supportsBulk &&
( ! action.isEligible || action.isEligible( item ) )
Expand Down
113 changes: 78 additions & 35 deletions packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Spinner,
Flex,
FlexItem,
Composite,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -36,7 +37,7 @@ import type {
} from '../../types';
import type { SetSelection } from '../../private-types';
import getClickableItemProps from '../utils/get-clickable-item-props';
import { useUpdatedPreviewSizeOnViewportChange } from './preview-size-picker';
import { usePreviewSize } from './preview-size-picker';
const { Badge } = unlock( componentsPrivateApis );

interface GridItemProps< Item > {
Expand All @@ -56,6 +57,14 @@ interface GridItemProps< Item > {
hasBulkActions: boolean;
}

function chunk( array: any, size: any ) {
Copy link
Member

Choose a reason for hiding this comment

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

Given that we know what we chunk, and size is definitely a number, can we avoid using any types here?

const chunks = [];
for ( let i = 0, j = array.length; i < j; i += size ) {
chunks.push( array.slice( i, i + size ) );
}
return chunks;
}

function GridItem< Item >( {
view,
selection,
Expand Down Expand Up @@ -120,6 +129,7 @@ function GridItem< Item >( {
<VStack
spacing={ 0 }
key={ id }
role="gridcell"
className={ clsx( 'dataviews-view-grid__card', {
'is-selected': hasBulkAction && isSelected,
} ) }
Expand Down Expand Up @@ -267,47 +277,80 @@ export default function ViewGrid< Item >( {
{ regularFields: [], badgeFields: [] }
);
const hasData = !! data?.length;
const updatedPreviewSize = useUpdatedPreviewSizeOnViewportChange();
const previewSize = usePreviewSize();
const hasBulkActions = useSomeItemHasAPossibleBulkAction( actions, data );
const usedPreviewSize = updatedPreviewSize || view.layout?.previewSize;
const gridStyle = usedPreviewSize
? {
gridTemplateColumns: `repeat(${ usedPreviewSize }, minmax(0, 1fr))`,
}
: {};
return (
<>
{ hasData && (
<Grid
gap={ 8 }
columns={ 2 }
alignment="top"
<Composite
role="grid"
className="dataviews-view-grid"
style={ gridStyle }
focusLoop
focusWrap
aria-busy={ isLoading }
>
{ data.map( ( item ) => {
return (
<GridItem
key={ getItemId( item ) }
view={ view }
selection={ selection }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
getItemId={ getItemId }
item={ item }
actions={ actions }
mediaField={ mediaField }
titleField={ titleField }
descriptionField={ descriptionField }
regularFields={ regularFields }
badgeFields={ badgeFields }
hasBulkActions={ hasBulkActions }
/>
);
} ) }
</Grid>
<VStack spacing={ 8 }>
{ chunk( data, previewSize ).map( ( row, i ) => (
<Composite.Row
key={ i }
role="row"
className="dataviews-view-grid__row"
Copy link
Member

Choose a reason for hiding this comment

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

I think this className can be removed, it doesn't seem to serve any purpose.

>
<Grid
templateColumns={ `repeat( ${ previewSize }, minmax(0, 1fr) )` }
gap={ 8 }
>
{ row.map( ( item: any ) => (
<Composite.Item
key={ getItemId( item ) }
render={
<div
id={ getItemId( item ) }
className="dataviews-view-grid__row__gridcell"
>
<GridItem
view={ view }
selection={ selection }
onChangeSelection={
onChangeSelection
}
onClickItem={
onClickItem
}
isItemClickable={
isItemClickable
}
getItemId={ getItemId }
item={ item }
actions={ actions }
mediaField={
mediaField
}
titleField={
titleField
}
descriptionField={
descriptionField
}
regularFields={
regularFields
}
badgeFields={
badgeFields
}
hasBulkActions={
hasBulkActions
}
/>
</div>
}
/>
) ) }
</Grid>
</Composite.Row>
) ) }
</VStack>
</Composite>
) }
{ ! hasData && (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,24 @@ function useViewPortBreakpoint() {
return 'mobile';
}

export function useUpdatedPreviewSizeOnViewportChange() {
export function usePreviewSize() {
const view = useContext( DataViewsContext ).view as ViewGrid;
const viewport = useViewPortBreakpoint();
return useMemo( () => {
const previewSize = view.layout?.previewSize;
let newPreviewSize;
if ( ! previewSize ) {
return;
// For mobile, we want the `min` value as the default.
const breakValueProp = viewport === 'mobile' ? 'min' : 'default';
return viewportBreaks[ viewport ][ breakValueProp ];
}
const breakValues = viewportBreaks[ viewport ];
if ( previewSize < breakValues.min ) {
newPreviewSize = breakValues.min;
return breakValues.min;
}
if ( previewSize > breakValues.max ) {
newPreviewSize = breakValues.max;
return breakValues.max;
}
return newPreviewSize;
return previewSize;
}, [ viewport, view ] );
}

Expand Down
40 changes: 21 additions & 19 deletions packages/dataviews/src/dataviews-layouts/grid/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.dataviews-view-grid {
margin-bottom: auto;
grid-template-rows: max-content;
padding: 0 $grid-unit-60 $grid-unit-30;
transition: padding ease-out 0.1s;
container-type: inline-size;
Expand Down Expand Up @@ -116,27 +115,10 @@
}

.dataviews-view-grid.dataviews-view-grid {
/**
* Breakpoints were adjusted from media queries breakpoints to account for
* the sidebar width. This was done to match the existing styles we had.
*/
@container (max-width: 480px) {
grid-template-columns: repeat(1, minmax(0, 1fr));
@container (max-width: 430px) {
padding-left: $grid-unit-30;
padding-right: $grid-unit-30;
}
@container (min-width: 480px) {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@container (min-width: 780px) {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
@container (min-width: 1140px) {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
@container (min-width: 1520px) {
grid-template-columns: repeat(5, minmax(0, 1fr));
}
}

.dataviews-view-grid__field-value:empty,
Expand All @@ -160,3 +142,23 @@
.dataviews-view-grid__media--clickable {
cursor: pointer;
}


.dataviews-view-grid__row {
Copy link
Member

Choose a reason for hiding this comment

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

We don't seem to need the wrapper class.

.dataviews-view-grid__row__gridcell {
outline: 0;
outline-style: solid;
// outline-offset: calc(-1 * var(--wp-admin-border-width-focus));
border-radius: $grid-unit-10;

&[data-focus-visible] {
outline-color: var(--wp-admin-theme-color);
outline-width: var(--wp-admin-border-width-focus);
}

&:hover {
outline-color: rgba($black, 0.3);
outline-width: var(--wp-admin-border-width-focus);
}
Comment on lines +154 to +162
Copy link
Member

Choose a reason for hiding this comment

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

The outer hover / focus states seem a bit awkward in terms of border-to-content spacing, and border radius:

Screenshot 2024-12-30 at 20 02 28

}
}
Loading