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: Implement isItemClickable and onClickItem props #66365

Merged
merged 75 commits into from
Nov 12, 2024

Conversation

gigitux
Copy link
Contributor

@gigitux gigitux commented Oct 23, 2024

What?

This PR fixes #66338.
This PR fixes #66588.

This PR adds two props to the Data Views library:

onClick

This callback is triggered when the user clicks on a media field or a primary field.

isClickable

This function determines if a media field or a primary field are clickable. It takes an item as an argument and returns a boolean value indicating whether the item can be clicked.

Why?

These two callbacks allow developers to implement click handlers for the media field and primary field at the consumer level, rather than within a specific field.

How?

Testing Instructions

Ensure that Quick Edit in DataViews and Quick Edit in DataViews are enabled.

  1. Visit the Site Editor
  2. Click on pages.
  3. Toggle to the table view.
  4. Ensure that when you click on the title you are redirected to the page editor
  5. Go back.
  6. Toggle to the grid view.
  7. Ensure that when you click on the image, you are redirected to the page editor.

Testing Instructions for Keyboard

Screenshots or screencast

@gigitux gigitux changed the title Data Views: Implement isClickable and onClickable props Data Views: Implement isItemClickable and onClickItem props Nov 5, 2024
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Tested this and it works well for me.

}
},
tabIndex: isClickable ? 0 : undefined,
onKeyDown: ( event: React.KeyboardEvent ) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should make this undefined if isClickable is false. Same for onClick prop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is an if inside both functions to ensure that the callback is invoked only when the item is clickable.

onKeyDown: ( event: React.KeyboardEvent ) => {
if (
( event.key === 'Enter' || event.key === '' ) &&
isClickable
) {
onClickItem( item );
}
},

onClick: () => {
if ( isClickable ) {
onClickItem( item );
}
},

I'm fine with replacing both with something like this if you prefer:

onKeyDown: ! isClickable
? undefined
: ( event: React.KeyboardEvent ) => {
		if ( event.key === 'Enter' || event.key === '' ) {
			onClickItem( item );
		}
  },

onClick: ! isClickable
? undefined
: () => {
		onClickItem( item );
},

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I just thought avoiding the handlers on things that are not "button" would be best.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed with c9e418b!

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

I want to acknowledge this is one possible approach, I know you folks are not entirely convinced. I'm not entirely ready to make this layout specific personally.

Would you be ok with moving forward here and judging this over time?

Co-authored-by: Riad Benguella <benguella@gmail.com>
@gigitux
Copy link
Contributor Author

gigitux commented Nov 6, 2024

I want to acknowledge this is one possible approach, I know you folks are not entirely convinced. I'm not entirely ready to make this layout specific personally.

Would you be ok with moving forward here and judging this over time?

The proposed approach works for me. Merging this PR will fix this regression: #66588. We can continue to iterate as needed 💪

@oandregal oandregal changed the title Data Views: Implement isItemClickable and onClickItem props DataViews: Implement isItemClickable and onClickItem props Nov 7, 2024
@@ -64,6 +70,26 @@
overflow: hidden;
}

.dataviews-view-grid__primary-field.dataviews-view-grid__primary-field--clickable
Copy link
Member

Choose a reason for hiding this comment

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

We want these styles to be applied to the title only if the layout supports the item being "clickable". Either we assume these classes become public API (so they should be stable) or we need to figure out better ways to do this.

Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this, Luigi!

I find a bit problematic that consumers need to target dataviews internal classes (so they become public API). This is an area I'd like us to iterate on.

Other than that, I think we should try this approach to gauge whether it's good enough.

packages/dataviews/src/components/dataviews/index.tsx Outdated Show resolved Hide resolved
@@ -17,8 +17,13 @@

.dataviews-view-grid__primary-field {
min-height: $grid-unit-40; // Preserve layout when there is no ellipsis button

&--clickable {
width: fit-content;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed, and why only for --clickable?

@@ -17,8 +17,13 @@

.dataviews-view-grid__primary-field {
min-height: $grid-unit-40; // Preserve layout when there is no ellipsis button

&--clickable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is &--clickable the best way to achieve this? We don't seem to do this in Gutenberg (we have only one occurrence of &--is-hex). What does this do that couldn't be achieved by targeting &[role="button"]?

Copy link
Member

Choose a reason for hiding this comment

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

I agree the way this handles the classes needs to be iterated, see related conversation #66365 (comment)

In discussing with Riad IRL the idea was to land this PR and iterate.

@oandregal
Copy link
Member

I want to build on top of this PR, so I've pushed changes to address @mcsf feedback. This PR is not perfect and we need to iterate, but I'm going to merge to unblock related work.

@mcsf
Copy link
Contributor

mcsf commented Nov 12, 2024

I want to build on top of this PR, so I've pushed changes to address @mcsf feedback. This PR is not perfect and we need to iterate, but I'm going to merge to unblock related work.

Thanks for addressing most of it, @oandregal! I'm quite fine with merging it as is. Let's address the rest afterwards.

@oandregal oandregal merged commit e07351b into trunk Nov 12, 2024
67 of 68 checks passed
@oandregal oandregal deleted the add/select-item-callback branch November 12, 2024 11:52
@github-actions github-actions bot added this to the Gutenberg 19.7 milestone Nov 12, 2024
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this pull request Nov 13, 2024
…ress#66365)

Co-authored-by: gigitux <gigitux@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: louwie17 <louwie17@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: mcsf <mcsf@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] DataViews /packages/dataviews [Type] Experimental Experimental feature or API.
Projects
None yet
5 participants