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

Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider #2013

Open
wants to merge 10 commits into
base: canary
Choose a base branch
from

Conversation

colinmurphy
Copy link
Contributor

@colinmurphy colinmurphy commented Dec 20, 2024

Tasks

  • I have signed a Contributor License Agreement (CLA) with WP Engine.
  • If a code change, I have written testing instructions that the whole team & outside contributors can understand.
  • I have written and included a comprehensive changeset to properly document the changes I've made.

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.

      <WordPressBlocksProvider
        config={{
          blocks,
          theme: null
        }}>
        <Component {...pageProps} key={router.asPath} />
      </WordPressBlocksProvider>

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.

cd directory/
npx create-next-app@latest
cd my-app #assuming you set my-app as the project name
npm install @faustwp/blocks

src/pages/_app.js

import "@/styles/globals.css";
import { WordPressBlocksProvider } from "@faustwp/blocks";
import blocks from "../wp-blocks";


export default function App({ Component, pageProps }) {

  return (
    <WordPressBlocksProvider config={{ blocks, theme: undefined}}>
      <Component {...pageProps} />
    </WordPressBlocksProvider>
  );
}

src/pages/index.js

import { WordPressBlocksViewer } from "@faustwp/blocks";

export default function Home() {
  return (
    <div>
      <h1>Home</h1>
      <WordPressBlocksViewer blocks={
[
  {
    "name": "core/paragraph",
    "__typename": "CoreParagraph",
    "apiVersion": 3,
    "isDynamic": false,
    "attributes": {
      "cssClassName": null,
      "backgroundColor": null,
      "content": "A JavaScript framework that makes building headless WordPress simple and easy.",
      "style": null,
      "textColor": null,
      "fontSize": null,
      "fontFamily": null,
      "direction": null,
      "dropCap": false,
      "gradient": null,
      "align": null
    }
  },
  {
    "name": "core/paragraph",
    "__typename": "CoreParagraph",
    "apiVersion": 3,
    "isDynamic": false,
    "attributes": {
      "cssClassName": null,
      "backgroundColor": null,
      "content": "A familiar publisher experience, providing a cohesive experience with wp-admin. Easily extensible with built in filters, empowering developers to customize for different use cases.",
      "style": null,
      "textColor": null,
      "fontSize": null,
      "fontFamily": null,
      "direction": null,
      "dropCap": false,
      "gradient": null,
      "align": null
    }
  }
        ]
      } />
    </div>
  );
};

src/wp-blocks/index.js

import { CoreBlocks } from "@faustwp/blocks";

export default {
	...CoreBlocks,
};

if you run npm run dev you will get the following error:

useBlocksTheme hook was called outside of context, make sure your app is wrapped with WordPressBlocksProvider

The following PR fixes that issue.

Screenshot 2024-12-24 at 08 57 05

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

Screenshot 2024-12-20 at 15 17 11

Documentation Changes

Dependant PRs

@colinmurphy colinmurphy self-assigned this Dec 20, 2024
Copy link

changeset-bot bot commented Dec 20, 2024

⚠️ No Changeset found

Latest commit: 0d65fa3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@colinmurphy colinmurphy changed the title Issue #1986 - Fixing null/undefined theme argument for the WordPressBlocksProvider Issue 1986 - Fixing null/undefined theme argument for the WordPressBlocksProvider Dec 20, 2024
@colinmurphy colinmurphy changed the title Issue 1986 - Fixing null/undefined theme argument for the WordPressBlocksProvider Fixing null/undefined theme argument for the WordPressBlocksProvider Dec 20, 2024
@colinmurphy colinmurphy changed the title Fixing null/undefined theme argument for the WordPressBlocksProvider Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider Dec 20, 2024
@colinmurphy colinmurphy changed the title Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider Dec 20, 2024
Copy link
Contributor

github-actions bot commented Dec 20, 2024

📦 Next.js Bundle Analysis for @faustwp/getting-started-example

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@colinmurphy
Copy link
Contributor Author

@moonmeister Can you review this please and see if you can still replicate issue #1986 please

@colinmurphy
Copy link
Contributor Author

@moonmeister I need to update this PR as some of the logic isn't correct

@colinmurphy
Copy link
Contributor Author

@moonmeister This is ready for review.

@moonmeister
Copy link
Collaborator

Can you explain why/how this fixes the issue?

@colinmurphy
Copy link
Contributor Author

@moonmeister

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

  1. Checks if both WordPressBlocksContext and WordPressThemeContext is defined, if not then it should throw an error (spotted a typo in the code)

  2. Then it should allow WordPressThemeContext values to be null. It will only throw an error if undefined

@colinmurphy
Copy link
Contributor Author

@moonmeister

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.

@colinmurphy colinmurphy marked this pull request as ready for review December 24, 2024 09:24
@colinmurphy colinmurphy requested a review from a team as a code owner December 24, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants