-
Notifications
You must be signed in to change notification settings - Fork 4.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
Page List: Move the modal to its own file. #47922
Conversation
<BlockControls group="other"> | ||
<ToolbarButton title={ __( 'Edit' ) } onClick={ openModal }> | ||
{ __( 'Edit' ) } | ||
</ToolbarButton> | ||
</BlockControls> |
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.
If we want to keep the previous behavior, this should only be rendered if allowConvertToLinks
is truthy.
const [ isModalOpen, setModalOpen ] = useState( false ); | ||
const openModal = () => setModalOpen( true ); | ||
const closeModal = () => setModalOpen( false ); |
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.
Moving the state in here instead of its own isolated component means the whole thing will need to rerender instead of just the modal when it opens.
In this specific place, the performance impact is probably going to be negligible especially with many of the complex data fetching and processing wrapped in a useMemo
. But I see complex components like this a lot in gutenberg that I think could be done better, so it seemed worth a note.
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 moved the state back to the child and then used a useEffect to to pass the opening function back to the parent. This avoids the rerender but I'm not sure if it's an anitpattern?
The alternative I considered was using a redux store, but we don't have a store for the block library.
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 pushed up a commit with what I was trying to explain since I didn't do a very good job with English 😅
We can't move the ToolbarButton out because then we're limited to using the Modal from the toolbar which is against the goal of this refactoring. The modal stays separate for reuse, but the open/close controls still get their own component.
99218d1
to
ea15f88
Compare
Size Change: +588 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
Flaky tests detected in 131350d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4186688100
|
{ allowConvertToLinks && ( | ||
<ToolbarButton | ||
title={ __( 'Edit' ) } | ||
onClick={ () => openModal() } |
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 don't think this is a solid approach - openModal can remain null and be called in various circumstances. Plus any component using this modal would need this closure to "receive" an internal method of the modal.
The modal should be mounted (hence open) when it's needed and this is in this edit component (open and close modal are things the edit component does, the modal only needs to know if it should show up or not, and that only to have an onClose event, instead of rendering conditionally).
Not sure what is the correct way suggested in #47922 (comment) but passing props up is basically the opposite of how it should be, I think?
8273c99
to
a43447f
Compare
OK I further simplified this to just move the modal to its own file for now, just to keep the code cleaner. |
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.
This looks good now as a refactoring, moves us closer to enabling an outside action to trigger the page list to convert itself.
What?
This is broken out of #47748.
Why?
These changes move the modal to a separate file.
How?
Refactoring
Testing Instructions