-
Notifications
You must be signed in to change notification settings - Fork 137
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
Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider #2013
base: canary
Are you sure you want to change the base?
Conversation
…PressBlocksProvider
|
📦 Next.js Bundle Analysis for @faustwp/getting-started-exampleThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
@moonmeister Can you review this please and see if you can still replicate issue #1986 please |
@moonmeister I need to update this PR as some of the logic isn't correct |
@moonmeister This is ready for review. |
Can you explain why/how this fixes the issue? |
What PR should solve is the issue around not allowing null for the theme argument for the WordPressBlockProvider. FWIW I couldn't ever replicate the bug but this is what the PR should do
|
Thank you for help on debugging this issue. I have updated the PR. I have also updated details on how to replicate the issue and also how it appears theme might be a redundant argument. |
} | ||
|
||
return themeContext; | ||
return React.useContext(WordPressThemeContext) as BlocksTheme; |
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 think we should revert useBlocksTheme to its original implementation because it contains a critical check to ensure that an error is triggered when the hook is used outside of the WordPressBlocksProvider
.
import React from 'react';
import { useBlocksTheme } from '@faustwp/blocks';
function MyComponent() {
const theme = useBlocksTheme();
return <div>Current Theme: {JSON.stringify(theme)}</div>;
}
export default function App() {
return (
<div>
{/* Forgot to wrap the app with WordPressBlocksProvider */}
<MyComponent />
</div>
);
}
I think the code needs to change is in WordPressBlocksProvider
so that we conditionally wrap the content with WordPressThemeContext
if the theme exists. Something like that:
export function WordPressBlocksProvider(props: {
children: React.ReactNode;
config: WordPressBlocksProviderConfig;
}) {
const { children, config } = props;
const { blocks, theme } = config;
const content = (
<WordPressBlocksContext.Provider value={blocks}>
{children}
</WordPressBlocksContext.Provider>
);
return theme ? (
<WordPressThemeContext.Provider value={theme}>
{content}
</WordPressThemeContext.Provider>
) : (
content
);
}
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 will still throw an error cause we call useBlocksTheme
in every core block even though, as best I can tell, we don't ever leverage that theme. So while this could be a good fix, it would be incomplete, I believe.
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.
We could just remove the useBlocksTheme
usages inside the blocks. Or maybe even better, always assign a default value for theme as {}
so we won't have to change anything else.
export function WordPressBlocksProvider(props: {
children: React.ReactNode;
config: WordPressBlocksProviderConfig;
}) {
const { children, config } = props;
const { blocks, theme = {} } = config;
return (
<WordPressThemeContext.Provider value={theme}>
<WordPressBlocksContext.Provider value={blocks}>
{children}
</WordPressBlocksContext.Provider>
</WordPressThemeContext.Provider>
);
}
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.
@theodesp I might be wrong here but would we also need to then update the getStyles function and style blocks (e.g. getBorderStyles as useBlocks
is used as the theme argument in all core blocks for this function.
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.
We could just remove the
useBlocksTheme
usages inside the blocks. Or maybe even better, always assign a default value for theme as{}
so we won't have to change anything else.export function WordPressBlocksProvider(props: { children: React.ReactNode; config: WordPressBlocksProviderConfig; }) { const { children, config } = props; const { blocks, theme = {} } = config; return ( <WordPressThemeContext.Provider value={theme}> <WordPressBlocksContext.Provider value={blocks}> {children} </WordPressBlocksContext.Provider> </WordPressThemeContext.Provider> ); }
Thanks @theodesp this does work setting a default for the theme so I am just going to update the PR shortly.
… an undefined themeContext error to be thrown for useBlocksTheme function
Tasks
Description
Fixed an issue with the theme argument for the WordPressBlockProvider to allow for null or undefined #1986
The condition to check if WordPressThemeContext is not needed as theme is an optional argument as per our docs - https://faustjs.org/reference/wordpressblocksprovider
e.g.
So setting the theme argument null, undefined or not at all should not result in an error.
How to replicate the issue
If you setup an Next.js app with Faust Blocks you should be able to replicate the error.
src/pages/_app.js
src/pages/index.js
src/wp-blocks/index.js
if you run
npm run dev
you will get the following error:The following PR fixes that issue.
Further Discussion
After debugging this issue with @moonmeister we also found that the theme argument appears to be redundant.
If you have a look at any of the blocks, it is only passed to the getStyles component which doesn't use the theme argument.
e.g.
https://github.com/wpengine/faustjs/blob/canary/packages/blocks/src/blocks/CoreParagraph.tsx
https://github.com/wpengine/faustjs/blob/canary/packages/blocks/src/utils/get-styles/getStyles.ts
https://github.com/wpengine/faustjs/blob/canary/packages/blocks/src/utils/get-styles/getTextStyles.ts#L10
There is a question on whether this is needed as none of the style components use this argument.
Related Issue(s):
#1986
Testing
As described above using a next.js app
Screenshots
Documentation Changes
Dependant PRs