-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
[docs] Fix scrolling demo to work with React 18 #6489
Conversation
Netlify deploy previewNetlify deploy preview: https://deploy-preview-6489--material-ui-x.netlify.app/ Updated pagesNo updates. These are the results for the performance tests:
|
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.
I need to click twice the Home button to scroll and focus the very first cell. Have you checked this?
Since you're working on this demo, please add a white background to the grid, it's odd everything in gray.
Indeed, I can reproduce it by pressing Home icon after scrolling to the very bottom-right and focusing last cell.
Done |
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.
I got curious about understanding what changes React 18 introduced.
With this change, state.focus.cell
is empty after the navigation button is clicked. You can reproduce by opening the PR preview, doing the reproduction noticing how the document.activeElement is on the right cell, scrolling enough for virtualization to remove the focused cell, scroll back, and seeing that the focus isn't restored to the cell.
Screen.Recording.2022-10-16.at.23.37.39.mov
For what I understand, using useLayoutEffeect over useEffect allows React to render twice, once with the right focus state, then call .focus() and a second time with an empty state, and since we don't blur when the focus state is empty, the activeElement looks correct, but the state behind is wrong.
Things that can be explored:
- Replace this logic with a blur event listener, it would likely be more resilient. We might be able to simplify things.
diff --git a/packages/grid/x-data-grid/src/hooks/features/focus/useGridFocus.ts b/packages/grid/x-data-grid/src/hooks/features/focus/useGridFocus.ts
index 5debc738f..d098cfc3f 100644
--- a/packages/grid/x-data-grid/src/hooks/features/focus/useGridFocus.ts
+++ b/packages/grid/x-data-grid/src/hooks/features/focus/useGridFocus.ts
@@ -230,19 +230,9 @@ export const useGridFocus = (
if (cellParams) {
apiRef.current.setCellFocus(cellParams.id, cellParams.field);
- } else {
- apiRef.current.setState((state) => ({
- ...state,
- focus: { cell: null, columnHeader: null },
- }));
- apiRef.current.forceUpdate();
-
- // There's a focused cell but another element (not a cell) was clicked
- // Publishes an event to notify that the focus was lost
- publishCellFocusOut(focusedCell, event);
}
- If 1. is too much work compared to the benefit, we could wrap the content of
React.useEffect(() => {
in the demo with a setTimeout or anything that allows for the document click event handle to handle the event before the effect runs. - In the demo, we keep the focused cell in sync with one of the data grid,
onCellClick
is not enough, the focus can move with a lot of other ways. It doesn't seem to be aonFocusChange
, so maybe withcellFocusIn
andcellFocusOut
This comment was marked as resolved.
This comment was marked as resolved.
0edfb55
to
082ccf5
Compare
Alright, I finally got back to this issue. I ended up listening to the Before that, I tried listening to the |
This would be a pretty complex change, especially because of virtualization where the focused cell might be unmounted and mounted again. |
Do you think these points may cause some unexpected behavior on the end user's side especially:
|
@MBilalShafi good points!
This one should not matter in our use case, since we are listening for events on
I tested the behavior with the right button (on a trackpad) and didn't notice any differences in how the focus state is handled. |
Yes, correct, shouldn't be an issue, I was only curious what would be the behavior with middle button click (some computer mice have a middle button under the scroll wheel), if that also triggers the focus, that is probably not the user's expectation 🤔 But since I don't have such a mouse available, I can't comment much on this. It shouldn't be much of an issue in any case 👍 |
Let's wait for @m4theushw's review then |
There is an editing issue that this PR fixes #8834 Before: https://codesandbox.io/s/priceless-breeze-xxwmep |
Fixes #6002
Fixes #8834
Preview: https://deploy-preview-6489--material-ui-x.netlify.app/x/react-data-grid/scrolling/#scrolling-to-specific-cells