Skip to content

DataViews: Make grid and table item preview aspect ratio configurable - #79329

Merged
ntsekouras merged 6 commits into
WordPress:trunkfrom
vianasw:add/dataviews-grid-configurable-aspect-ratio
Aug 5, 2026
Merged

DataViews: Make grid and table item preview aspect ratio configurable#79329
ntsekouras merged 6 commits into
WordPress:trunkfrom
vianasw:add/dataviews-grid-configurable-aspect-ratio

Conversation

@vianasw

@vianasw vianasw commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

What?

See #60891

The DataViews grid layout renders item previews (the media field) at a hard-coded 1/1 (square) aspect ratio, and the table layout does the same for the primary column's media preview. This adds an aspectRatio option to the grid and table layout configs so consumers can choose the shape, defaulting to 1/1 so nothing changes for existing consumers.

Why?

#60891 asks for the grid preview aspect ratio to be consumer configurable — a square crop isn't ideal for every dataset (tall templates, wide media, video thumbnails, etc.). For example, a video library wants 16/9 previews so the thumbnail matches how the video actually appears.

A previous attempt (#63487) stalled on the UX of a user-facing toggle between 1/1 and auto (variable per-item ratios), which raised "visual rivers" / masonry concerns. This PR deliberately sidesteps that debate: it exposes a single consumer-set, uniform aspect ratio, applied identically to every item — the form even the prior reviewers were comfortable with ("it should change the aspect ratio of all previews"). The user-facing toggle and auto/variable per-item ratios are intentionally left as a follow-up.

Scope is limited to the grid and table layouts; pickerGrid/pickerTable parity is a straightforward follow-up.

How?

  • Adds aspectRatio?: MediaAspectRatio to ViewGrid['layout'] and ViewTable['layout'], where MediaAspectRatio is a union of preset ratios ('1/1' | '4/3' | '3/4' | '3/2' | '2/3' | '16/9' | '9/16', mirroring Core's default aspect-ratio presets) — the same constrained-union pattern as density. Starting with presets keeps the API easy to widen later without a breaking change, and avoids consumers setting degenerate ratios.
  • The value is surfaced as a --wp-dataviews-media-aspect-ratio CSS custom property (matching the existing --wp-dataviews-color-background naming) on the layout root (the grid container — both the standard and infinite-scroll roots — and the table element). The property is always set, defaulting to 1/1, so a same-named variable set by a consumer on an ancestor can't leak into the previews.
  • Grid: the existing .dataviews-view-grid__media rule reads it with a 1/1 fallback: aspect-ratio: var(--wp-dataviews-media-aspect-ratio, 1/1).
  • Table: gated behind a has-media-aspect-ratio modifier (same pattern as has-*-density) that the table only carries when the view sets layout.aspectRatio. Under the modifier, the .dataviews-column-primary__media wrapper takes height + aspect-ratio, with the img filling it — sizing the wrapper rather than the img keeps custom media field renders (nested elements instead of a bare img) covered too. Row heights are unchanged; the preview widens instead, and a 16/9 preview at the default height still fits the wrapper's existing max-width. Without aspectRatio, the base rules are exactly what trunk ships today.
  • Net effect: every existing consumer is unaffected — the grid keeps its 1/1 default and the table keeps its current sizing until a view opts in.

Testing Instructions

  1. In a grid DataView, set the layout's aspectRatio, e.g. view = { type: 'grid', layout: { aspectRatio: '16/9' }, … }.
  2. Confirm the item previews render at the configured ratio (16:9 rectangles) instead of squares, with rows staying aligned (uniform heights).
  3. Try other fixed ratios (e.g. '4/3', '1/1') and confirm they apply uniformly to every item.
  4. In a table DataView with a media field, set layout.aspectRatio: '16/9' and confirm the primary column's thumbnail renders 16:9 at the same height — row heights don't change.
  5. Remove aspectRatio (or use an existing grid/table consumer such as the Pages/Templates views) and confirm previews stay square (1/1) — no regression.
  6. Confirm previewSize and density still work alongside it.

Testing Instructions for Keyboard

No interaction changes — the option only affects preview dimensions, so existing keyboard navigation of the grid and table is unaffected.

Screenshots or screencast

Before (1/1) After (aspectRatio: '16/9')
Table layout square Table layout wide
Square previews 16:9 previews

Use of AI Tools

This PR was authored with the assistance of AI tooling (Claude Code). The implementation, scope decisions, and this description were AI-assisted and reviewed by me before submission; I take responsibility for the contents.

@vianasw
vianasw force-pushed the add/dataviews-grid-configurable-aspect-ratio branch from 0a12995 to 3ce3734 Compare June 18, 2026 16:30
@github-actions github-actions Bot added [Package] DataViews /packages/dataviews First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Jun 18, 2026
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @vianasw! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@vianasw
vianasw force-pushed the add/dataviews-grid-configurable-aspect-ratio branch from 3ce3734 to 54c2fd0 Compare June 18, 2026 17:04
@vianasw
vianasw force-pushed the add/dataviews-grid-configurable-aspect-ratio branch from cb8fbd8 to ef4af68 Compare July 16, 2026 12:35
@vianasw vianasw changed the title DataViews: Make grid item preview aspect ratio configurable DataViews: Make grid and table item preview aspect ratio configurable Jul 16, 2026
@vianasw
vianasw marked this pull request as ready for review July 16, 2026 15:49
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @vianasw.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: vianasw.

Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment thread packages/dataviews/src/components/dataviews-layouts/table/style.scss Outdated
Comment thread packages/dataviews/src/components/dataviews-layouts/table/index.tsx Outdated
Comment thread packages/dataviews/src/components/dataviews-layouts/table/style.scss Outdated
@ntsekouras ntsekouras added [Type] Enhancement A suggestion for improvement. [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond labels Jul 17, 2026
Comment thread packages/dataviews/src/components/dataviews-layouts/table/style.scss Outdated
@ntsekouras

Copy link
Copy Markdown
Contributor

Thanks for the PR! Let's also get some design feedback (@WordPress/gutenberg-design) and maybe @oandregal has thoughts too.

@ntsekouras
ntsekouras requested a review from a team July 17, 2026 08:45
@jasmussen

Copy link
Copy Markdown
Contributor

At a glance, this seems valid enough to me, assuming it looks good with image-less placeholders too. It also addresses a point raised by Jay, who has worked with DataViews long enough that I trust his previously asserted instincts. 👍

@vianasw
vianasw requested a review from ntsekouras July 21, 2026 17:38
@vianasw
vianasw force-pushed the add/dataviews-grid-configurable-aspect-ratio branch 2 times, most recently from b997531 to 21d6390 Compare July 31, 2026 15:59
Comment thread packages/dataviews/src/components/dataviews-layouts/table/index.tsx
Comment thread packages/dataviews/src/components/dataviews-layouts/table/index.tsx Outdated
@ntsekouras

Copy link
Copy Markdown
Contributor

We'd need to update view-config schema to include this new prop.

I think it's fine to handle as a follow-up, as there's no functional impact in the meantime. It also involves a couple of extra small changes, because the PHP layout schemas are shared between table/pickerTable and grid/pickerGrid while the new prop doesn't apply to the picker layouts, so there's a small decision about whether to split them. I can pick this up. @oandregal are you fine with a follow up for the schema changes?

@vianasw
vianasw requested a review from ntsekouras August 4, 2026 16:12
vianasw and others added 6 commits August 4, 2026 18:43
The grid layout's media preview is hard-coded to a `1/1` (square) aspect
ratio. Some consumers need a different shape — e.g. a video library wants
`16/9` to match how the media actually appears when embedded.

Add an `aspectRatio` option to the grid `layout` config. It is applied
uniformly to every item via a `--dataviews-grid-media-aspect-ratio` CSS
custom property and defaults to `1/1`, so existing consumers are
unaffected.

This implements the uniform, consumer-set case from WordPress#60891 and leaves the
user-facing toggle (and `auto`/variable per-item ratios) out of scope —
that was the part that stalled the earlier attempt in WordPress#63487.

Part of WordPress#60891

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add the aspectRatio layout option to ViewTable as well: the value is
surfaced on the table element and read by the primary column's media
styles, which switch from a fixed square (width/height) to
height + aspect-ratio so row heights are unchanged and the preview
widens instead. A 16/9 preview at the default height still fits the
wrapper's existing max-width.

Since the CSS custom property is now read by more than one layout,
rename it from --dataviews-grid-media-aspect-ratio to
--dataviews-media-aspect-ratio (safe pre-release: the original commit
is part of this unmerged PR).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Constrain the aspectRatio layout option from a free-form string to a
MediaAspectRatio union of preset ratios (1/1, 4/3, 3/4, 3/2, 2/3, 16/9,
9/16 — mirroring Core's default aspect-ratio presets), following the
same pattern as the density option. Starting narrow is the safe API
direction: widening the union later is backward compatible, while
narrowing a shipped free-form string would be a breaking change.
Runtime behavior is unchanged — the value still reaches CSS as a
custom property.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The trunk merge landed while three @wordpress/dataviews releases had
shipped since this branch was cut, so the entry's original surroundings
(the old Unreleased section) had become the 17.0.0 section — taking the
entry with them. Restore 17.0.0 to its released contents and list the
entry under Unreleased, where the changelog CI check expects it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Rename the custom property to --wp-dataviews-media-aspect-ratio,
  matching the existing --wp-dataviews-color-background convention.
- Always set the property (with the 1/1 default) on the grid and table
  roots, so an identically-named variable set by a consumer on an
  ancestor can't leak into previews when the view doesn't configure a
  ratio.
- Gate the table media sizing behind a has-media-aspect-ratio modifier
  (same pattern as has-*-density): the base
  .dataviews-column-primary__media rules are restored to exactly what
  trunk ships, and the fixed-height + ratio sizing (with the img
  filling the box) only applies when the view sets layout.aspectRatio.
  Views without it see no change, including media renders taller than
  the 32px default.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…x sub-square table previews

Address the second round of review feedback:

- Add a shared MEDIA_ASPECT_RATIOS constant and derive the MediaAspectRatio
  type from it, so the runtime list and the type can't drift apart. Both
  layouts now validate layout.aspectRatio against the presets (like density)
  and fall back to the square default; the table only applies the
  has-media-aspect-ratio modifier for a valid preset.
- Drop the redundant CSSProperties annotation in favor of the single cast,
  in both layouts.
- Release the base rule's min-width under the has-media-aspect-ratio
  modifier: it equals the fixed height, so sub-square presets (3/4, 2/3,
  9/16) derived a width below it and rendered square.
- Widen the table's srcset sizes hint from the validated ratio: with a
  ratio configured the preview box is width-variable (up to the 60px
  max-width), and the hardcoded 32px hint made consumers that honor it
  pick an undersized, blurry source for wide ratios.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014CG2FSyrhQ581KmpSmjcco
@vianasw
vianasw force-pushed the add/dataviews-grid-configurable-aspect-ratio branch from f5cd7c4 to bca4649 Compare August 4, 2026 16:43

@ntsekouras ntsekouras left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This LGTM. Thanks for effort and the iterations! Great work!

@oandregal

Copy link
Copy Markdown
Member

👍 to prepare a follow-up with the schema changes. Heads up that I'm working on generating the docs and the PHP schema from the JSON schema at #81168 so, depending on timing, one of them will need rebase.

@ntsekouras

Copy link
Copy Markdown
Contributor

👍 to prepare a follow-up with the schema changes. Heads up that I'm working on generating the docs and the PHP schema from the JSON schema at #81168 so, depending on timing, one of them will need rebase.

Sounds good. I can open the follow up after your changes have landed.

@ntsekouras
ntsekouras merged commit acf17cd into WordPress:trunk Aug 5, 2026
60 checks passed
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Congratulations on your first merged pull request, @vianasw! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

@github-actions github-actions Bot added this to the Gutenberg 23.8 milestone Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] DataViews /packages/dataviews [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants