Skip to content

Conversation

@p-jackson
Copy link
Member

@p-jackson p-jackson commented Aug 15, 2025

What?

Fixes a crash caused by <PreviewSizePicker> when an invalid previewSize is passed to the data view.

Why?

When a grid dataview is passed a previewSize option which doesn't match one of the discrete sizes available, it falls back to the next smallest option. But if previewSize was smaller than the smallest available size it would throw an exception.

previewSize could be invalid for a number of reasons. For us it happened because we persist the user's selected size as a page query parameter. Since the page query parameter is editable, it is possible users edit this data and corrupt it. Since dataviews doesn't export the list of valid preview sizes we aren't able to reliably sanitize the input on our end.

How?

The preview size picker control now shows the smallest available size as selected if the previewSize prop is too small.

Testing Instructions

I've added a unit test to cover this situation, but you could manually test this in storybook.

First change this to 'grid' so that the default dataview in storybook is a grid.

Then add previewSize: 100 to the to the default layout options.

When you use storybook the dataviews will show small icons, but when you open the view options it will no longer crash. When you change the preview size it will pop back to one of the allowed preview sizes.

CleanShot.2025-08-15.at.16.31.54.mp4

Here's the crash:

CleanShot.2025-08-15.at.16.33.18.mp4

Testing Instructions for Keyboard

No UI changes.

When a grid dataview is passed a previewSize option which doesn't match
one of the discrete sizes available, it falls back to the next smallest
option. But if previewSize was smaller than the smallest available size
it would throw an exception.

The preview size picker control now uses the smallest available size if
the previewSize option is too small.
@p-jackson p-jackson self-assigned this Aug 15, 2025
@p-jackson p-jackson requested a review from oandregal as a code owner August 15, 2025 04:37
@github-actions
Copy link

github-actions bot commented Aug 15, 2025

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.

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

Co-authored-by: p-jackson <[email protected]>
Co-authored-by: arthur791004 <[email protected]>
Co-authored-by: andrewserong <[email protected]>

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

@p-jackson p-jackson added [Type] Bug An existing feature does not function as intended [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Package] DataViews /packages/dataviews labels Aug 15, 2025
@github-actions
Copy link

Flaky tests detected in cf8e1d7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/16983121288
📝 Reported issues:

.filter( ( size ) => size.value <= layoutPreviewSize )
.filter(
( size ) => size.index === 0 || size.value <= layoutPreviewSize
)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about checking whether the result is available before accessing it's value. Would it be clearer to handle the error 🤔

[ 0 ]?.index ?? breakValues[ 0 ].index

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 hmmm maybe this is clearer. I was so focused on making sure the array wasn't empty that I didn't just use ?.

Is breakValues[ 0 ].index just guaranteed to always be 0 though? I think it is.

I'll just use 0.

Copy link
Contributor

@arthur791004 arthur791004 left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

For us it happened because we persist the user's selected size as a page query parameter. Since the page query parameter is editable, it is possible users edit this data and corrupt it.

Oh, fascinating, I hadn't even thought of that as a possibility. Nice catch with this PR!

This is looking good to me, too, with the default of having the 3rd size be the default, but the 1st option preserved for this case. Thanks for including a test case, too 👍

Before

image

After

image

LGTM 🚀

@p-jackson p-jackson merged commit f503dd4 into trunk Aug 15, 2025
77 of 78 checks passed
@p-jackson p-jackson deleted the fix/preview-size-picker-crash-on-invalid-prop branch August 15, 2025 08:38
@github-actions github-actions bot added this to the Gutenberg 21.5 milestone Aug 15, 2025
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 [Package] DataViews /packages/dataviews [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants