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: Grid layout refinements #56441

Merged
merged 7 commits into from
Nov 29, 2023
Merged
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
14 changes: 6 additions & 8 deletions packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ export default function DataViews( {
onChangeView={ onChangeView }
/>
</HStack>
<HStack justify="end" expanded={ false }>
<ViewActions
fields={ fields }
view={ view }
onChangeView={ onChangeView }
supportedLayouts={ supportedLayouts }
/>
</HStack>
<ViewActions
fields={ fields }
view={ view }
onChangeView={ onChangeView }
supportedLayouts={ supportedLayouts }
/>
</HStack>
<ViewComponent
fields={ _fields }
Expand Down
9 changes: 8 additions & 1 deletion packages/edit-site/src/components/dataviews/item-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ export default function ItemActions( { item, actions, isCompact } ) {
);
}
return (
<HStack justify="flex-end">
<HStack
spacing={ 1 }
justify="flex-end"
style={ {
flexShrink: '0',
width: 'auto',
} }
>
{ !! primaryActions.length &&
primaryActions.map( ( action ) => {
if ( !! action.RenderModal ) {
Expand Down
63 changes: 57 additions & 6 deletions packages/edit-site/src/components/dataviews/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,64 @@
}
}

.dataviews-view-grid__media {
width: 100%;
min-height: 200px;
.dataviews-grid-view {
margin-bottom: $grid-unit-30;
grid-template-columns: repeat(2, minmax(0, 1fr)) !important;

@include break-xlarge() {
grid-template-columns: repeat(3, minmax(0, 1fr)) !important; // Todo: eliminate !important dependency
}

@include break-huge() {
grid-template-columns: repeat(4, minmax(0, 1fr)) !important; // Todo: eliminate !important dependency
}

.dataviews-view-grid__card {
h3 { // Todo: A better way to target this
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}

.dataviews-view-grid__media {
width: 100%;
min-height: 200px;
aspect-ratio: 1/1;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
border-radius: $radius-block-ui * 2;
overflow: hidden;

> * {
max-width: 100%;
object-fit: cover;
> * {
object-fit: cover;
width: 100%;
height: 100%;
}
}

.dataviews-view-grid__title {
min-height: $grid-unit-30;

a {
color: $gray-900;
text-decoration: none;
font-weight: 500;
}
}

.dataviews-view-grid__fields {
position: relative;
font-size: 12px;
line-height: 16px;

.dataviews-view-grid__field {
.dataviews-view-grid__field-header {
color: $gray-700;
}
.dataviews-view-grid__field-value {
color: $gray-900;
}
}
}
}

Expand Down
91 changes: 54 additions & 37 deletions packages/edit-site/src/components/dataviews/view-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
__experimentalGrid as Grid,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
FlexBlock,
Placeholder,
} from '@wordpress/components';
import { useAsyncList } from '@wordpress/compose';

Expand All @@ -19,10 +17,15 @@ export function ViewGrid( { data, fields, view, actions, getItemId } ) {
const mediaField = fields.find(
( field ) => field.id === view.layout.mediaField
);
const primaryField = fields.find(
( field ) => field.id === view.layout.primaryField
);
const visibleFields = fields.filter(
( field ) =>
! view.hiddenFields.includes( field.id ) &&
field.id !== view.layout.mediaField
! [ view.layout.mediaField, view.layout.primaryField ].includes(
field.id
)
);
const shownData = useAsyncList( data, { step: 3 } );
return (
Expand All @@ -32,42 +35,56 @@ export function ViewGrid( { data, fields, view, actions, getItemId } ) {
alignment="top"
className="dataviews-grid-view"
>
{ shownData.map( ( item, index ) => {
return (
<VStack key={ getItemId?.( item ) || index }>
<div className="dataviews-view-grid__media">
{ mediaField?.render( { item, view } ) || (
<Placeholder
withIllustration
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jameskoster this used to render a Placeholder and now we just have the empty box-shadowed div. Is that the wanted design?

style={ {
width: '100%',
minHeight: '200px',
} }
/>
) }
</div>

<HStack justify="space-between" alignment="top">
<FlexBlock>
<VStack>
{ visibleFields.map( ( field ) => (
<div key={ field.id }>
{ field.render( { item, view } ) }
</div>
) ) }
{ shownData.map( ( item, index ) => (
<VStack
spacing={ 3 }
key={ getItemId?.( item ) || index }
className="dataviews-view-grid__card"
>
<div className="dataviews-view-grid__media">
{ mediaField?.render( { item, view } ) }
</div>
<HStack
className="dataviews-view-grid__title"
justify="space-between"
>
{ primaryField?.render( { item, view } ) }
<ItemActions
item={ item }
actions={ actions }
isCompact
/>
</HStack>
<VStack
className="dataviews-view-grid__fields"
spacing={ 3 }
>
{ visibleFields.map( ( field ) => {
const renderedValue = field.render( {
item,
view,
} );
if ( ! renderedValue ) {
return null;
}
return (
<VStack
className="dataviews-view-grid__field"
key={ field.id }
spacing={ 1 }
>
<div className="dataviews-view-grid__field-header">
{ field.header }
</div>
<div className="dataviews-view-grid__field-value">
{ field.render( { item, view } ) }
</div>
</VStack>
</FlexBlock>
<FlexBlock style={ { maxWidth: 'min-content' } }>
<ItemActions
item={ item }
actions={ actions }
isCompact
/>
</FlexBlock>
</HStack>
);
} ) }
</VStack>
);
} ) }
</VStack>
) ) }
</Grid>
);
}
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const defaultConfigPerViewType = {
list: {},
grid: {
mediaField: 'featured-image',
primaryField: 'title',
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
VisuallyHidden,
} from '@wordpress/components';
import { __, _x } from '@wordpress/i18n';
import { useState, useMemo, useCallback } from '@wordpress/element';
Expand Down Expand Up @@ -39,14 +40,17 @@ import {
import usePatternSettings from '../page-patterns/use-pattern-settings';
import { unlock } from '../../lock-unlock';

const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );
const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock(
blockEditorPrivateApis
);

const EMPTY_ARRAY = [];

const defaultConfigPerViewType = {
list: {},
grid: {
mediaField: 'preview',
primaryField: 'title',
},
};

Expand Down Expand Up @@ -112,6 +116,7 @@ function AuthorField( { item } ) {

function TemplatePreview( { content, viewType } ) {
const settings = usePatternSettings();
const [ backgroundColor = 'white' ] = useGlobalStyle( 'color.background' );
const blocks = useMemo( () => {
return parse( content );
}, [ content ] );
Expand All @@ -129,6 +134,7 @@ function TemplatePreview( { content, viewType } ) {
<ExperimentalBlockEditorProvider settings={ settings }>
<div
className={ `page-templates-preview-field is-viewtype-${ viewType }` }
style={ { backgroundColor } }
>
<BlockPreview blocks={ blocks } />
</div>
Expand Down Expand Up @@ -173,12 +179,17 @@ export default function DataviewsTemplates() {
id: 'description',
getValue: ( { item } ) => item.description,
render: ( { item } ) => {
return (
item.description && (
<Text variant="muted">
{ decodeEntities( item.description ) }
return item.description ? (
decodeEntities( item.description )
) : (
<>
<Text variant="muted" aria-hidden="true">
&#8212;
</Text>
)
<VisuallyHidden>
{ __( 'No description.' ) }
</VisuallyHidden>
</>
);
},
maxWidth: 200,
Expand Down
7 changes: 1 addition & 6 deletions packages/edit-site/src/components/page-templates/style.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
.page-templates-preview-field {
.block-editor-block-preview__container {
border: 1px solid $gray-300;
border-radius: $radius-block-ui;
}

&.is-viewtype-list {
.block-editor-block-preview__container {
height: 120px;
Expand All @@ -12,7 +7,7 @@

&.is-viewtype-grid {
.block-editor-block-preview__container {
height: 320px;
height: auto;
}
}
}