-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
Spinner, | ||
Flex, | ||
FlexItem, | ||
Composite, | ||
privateApis as componentsPrivateApis, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
@@ -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 > { | ||
|
@@ -56,6 +57,14 @@ interface GridItemProps< Item > { | |
hasBulkActions: boolean; | ||
} | ||
|
||
function chunk( array: any, size: any ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we know what we chunk, and |
||
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, | ||
|
@@ -120,6 +129,7 @@ function GridItem< Item >( { | |
<VStack | ||
spacing={ 0 } | ||
key={ id } | ||
role="gridcell" | ||
className={ clsx( 'dataviews-view-grid__card', { | ||
'is-selected': hasBulkAction && isSelected, | ||
} ) } | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this |
||
> | ||
<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 | ||
|
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; | ||
|
@@ -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, | ||
|
@@ -160,3 +142,23 @@ | |
.dataviews-view-grid__media--clickable { | ||
cursor: pointer; | ||
} | ||
|
||
|
||
.dataviews-view-grid__row { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?