Skip to content

Commit

Permalink
fix: provide better error when usePuck used inappropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Dec 16, 2024
1 parent 8680db3 commit 9991c07
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/components/Puck/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type AppContext<
refreshPermissions: RefreshPermissions<UserConfig>;
};

const defaultContext: AppContext = {
export const defaultContext: AppContext = {
state: defaultAppState,
dispatch: () => null,
config: { components: {} },
Expand Down
8 changes: 7 additions & 1 deletion packages/core/lib/use-puck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAppContext } from "../components/Puck/context";
import { defaultContext, useAppContext } from "../components/Puck/context";
import { Config, UserGenerics } from "../types";

export const usePuck = <UserConfig extends Config = Config>() => {
Expand All @@ -12,6 +12,12 @@ export const usePuck = <UserConfig extends Config = Config>() => {
refreshPermissions,
} = useAppContext<UserConfig>();

if (dispatch === defaultContext.dispatch) {
throw new Error(
"usePuck was used outside of the <Puck> component. The usePuck hook must be rendered within the <Puck> context as children, overrides or plugins as described in https://puckeditor.com/docs/api-reference/functions/use-puck."
);
}

return {
appState,
config,
Expand Down

0 comments on commit 9991c07

Please sign in to comment.