Skip to content

Commit

Permalink
Fixed conditions to allow null for the theme argument
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmurphy committed Dec 20, 2024
1 parent 1e6fd19 commit 24f4d0e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/blocks/src/components/WordPressBlocksProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@ export function WordPressBlocksProvider(props: {
* const theme = useBlocksTheme();
* ```
*/
export function useBlocksTheme(): BlocksTheme {
export function useBlocksTheme(): BlocksTheme | null {


// If it's an empty object, the provider hasn't been initialized.
if (WordPressBlocksContext == null || WordPressThemeContext == null) {
throw new Error(
// If it's an empty object, the provider hasn't been initialized.±
if (typeof WordPressBlocksContext === 'undefined') {
throw new Error(
'useBlocksTheme hook was called outside of context, make sure your app is wrapped with WordPressBlocksProvider',
);
}

// Theme is optional
if (typeof WordPressBlocksContext === 'undefined' || WordPressBlocksContext === null) {
return null;
}

const themeContext = React.useContext(WordPressThemeContext);
if (themeContext == null) {
throw new Error(
'useBlocksTheme hook was called outside of context, make sure your app is wrapped with WordPressBlocksProvider',
);
if (typeof themeContext === 'undefined' || themeContext === null) {
return null;
}

return themeContext;
Expand Down

0 comments on commit 24f4d0e

Please sign in to comment.