-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix Colors not updating on click for Calender component in the Admin. #64986
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
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @rutviksavsani! 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. |
t-hamano
left a comment
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.
Thanks for the PR!
The ServerSideRender component is expected to be re-rendered in response to changes in the attributes prop. If it is not working as expected, there may be some problem with how we are using the ServerSideRender component or with the ServerSideRender component itself. This PR certainly solves the problem, but we should look for the root cause.
The ServerSideRender component bounces a function that fetches data:
| const debouncedFetchData = useDebounce( fetchData, 500 ); |
And if the previous props and the new props are different, this debounced function should be called:
gutenberg/packages/server-side-render/src/server-side-render.js
Lines 198 to 199 in 6accdd3
| } else if ( ! fastDeepEqual( prevProps, props ) ) { | |
| debouncedFetchData(); |
But for some reason, it is not called.
As a test, if I make the following change to not debounce the data fetch, the problem will not be reproduced:
diff --git a/packages/server-side-render/src/server-side-render.js b/packages/server-side-render/src/server-side-render.js
index 45a651e8cc..8dd516cd74 100644
--- a/packages/server-side-render/src/server-side-render.js
+++ b/packages/server-side-render/src/server-side-render.js
@@ -196,7 +196,7 @@ export default function ServerSideRender( props ) {
if ( prevProps === undefined ) {
fetchData();
} else if ( ! fastDeepEqual( prevProps, props ) ) {
- debouncedFetchData();
+ fetchData();
}
} );My guess is that the call to the debounced function might being canceled for some reason, but I have not yet been able to find the cause.
Another reason is that only the Calendar block skips serialization of text and background colors, which might be the cause. For example, the Tag Cloud block also supports color and is rendered via the ServerSideRender component, but color changes are reflected immediately.
|
Thanks for the ping 👍
From memory, the reason the Tag Cloud works is that the color styles and classes are applied to the outer block wrapper and filtered from the server-side-rendered attributes. This means the CSS is already present in the editor and targeting the wrapper. When the server-side rendered block is updated the CSS is applied as expected. FWIW the same could be achieved for the Calendar block by using Incomplete diff illustrating the above (don't use, see below)diff --git a/packages/block-library/src/calendar/edit.js b/packages/block-library/src/calendar/edit.js
index a60a18d8ee..7334068a46 100644
--- a/packages/block-library/src/calendar/edit.js
+++ b/packages/block-library/src/calendar/edit.js
@@ -10,7 +10,10 @@ import { calendar as icon } from '@wordpress/icons';
import { Disabled, Placeholder, Spinner } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import ServerSideRender from '@wordpress/server-side-render';
-import { useBlockProps } from '@wordpress/block-editor';
+import {
+ useBlockProps,
+ __experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
+} from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
@@ -34,7 +37,21 @@ const getYearMonth = memoize( ( date ) => {
} );
export default function CalendarEdit( { attributes } ) {
- const blockProps = useBlockProps();
+ // Filter out color block support attributes and apply to the wrapper.
+ // This ensures the immediate update of color selections in the editor.
+ const colorProps = getColorClassesAndStyles( attributes );
+ const serverSideAttributes = {
+ ...attributes,
+ style: {
+ ...attributes?.style,
+ color: undefined,
+ },
+ };
+ const blockProps = useBlockProps( {
+ className: colorProps.className,
+ style: colorProps.style,
+ } );
+
const { date, hasPosts, hasPostsResolved } = useSelect( ( select ) => {
const { getEntityRecords, hasFinishedResolution } = select( coreStore );
@@ -95,7 +112,10 @@ export default function CalendarEdit( { attributes } ) {
<Disabled>
<ServerSideRender
block="core/calendar"
- attributes={ { ...attributes, ...getYearMonth( date ) } }
+ attributes={ {
+ ...serverSideAttributes,
+ ...getYearMonth( date ),
+ } }
/>
</Disabled>
</div>
diff --git a/packages/block-library/src/calendar/style.scss b/packages/block-library/src/calendar/style.scss
index 969d1aafec..eb27c44adb 100644
--- a/packages/block-library/src/calendar/style.scss
+++ b/packages/block-library/src/calendar/style.scss
@@ -19,7 +19,7 @@
width: 100%;
border-collapse: collapse;
- &:where(:not(.has-text-color)) {
+ &:where( :not( .has-text-color ) ) {
color: #40464d;
// Keep the hard-coded border color for backward compatibility.
@@ -40,6 +40,11 @@
}
// Keep the hard-coded header background color for backward compatibility.
-:where(.wp-block-calendar table:not(.has-background) th) {
+:where( .wp-block-calendar table:not( .has-background ) th ) {
background: $gray-300;
}
+
+.wp-block-calendar .wp-block-calendar table {
+ color: inherit;
+ background-color: inherit;
+}
As @t-hamano notes, it would be best to solve this at its core. That could arguably be refactoring the blocks to JS using REST API Unfortunately, I've run out of time and bandwidth to help debug further but quickly adding some logging to the |
|
I found that similar problems occur in other blocks. For example, the following problem can be found in the Latest Comments block:
Maybe there is a fundamental problem with the 101fe6938e6bdfb27651bc0aad903c2e.mp4 |
|
I believe #69237 is the best approach, so I'm closing this PR. @rutviksavsani Thank you for your work! |

Fixes: #64739
What?
This PR addresses an issue where changes to the background and text colors were not immediately reflected in the Calendar block due to the server-side rendering. By adding the
keyprop and handling null values, we force a re-render when color changes are made, ensuring immediate updates in the block editor.Why?
The problem was that changes to the background and text colors in the Calendar block were not reflected immediately when updated in the admin panel. The use of
<ServerSideRender>required a forced re-render to ensure the updated attributes were reflected. By setting thekeyprop and handling null values, we ensure the block behaves as expected.How?
keyprop in the<ServerSideRender>component using a combination ofbackgroundColorandtextColor.'default'when the color values are not present.<ServerSideRender>component whenever color changes occur.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast
Edit.Post.Test.Post.for.Calendar.Block.gutenberg.WordPress.webm