diff --git a/packages/editor/api-report.md b/packages/editor/api-report.md index 674692ca4..60e14d077 100644 --- a/packages/editor/api-report.md +++ b/packages/editor/api-report.md @@ -1068,7 +1068,7 @@ export class Editor extends EventEmitter { } // @internal (undocumented) -export const EditorContext: React_2.Context; +export const EditorContext: React_2.Context; // @public (undocumented) export class Ellipse2d extends Geometry2d { diff --git a/packages/editor/src/lib/hooks/useEditor.ts b/packages/editor/src/lib/hooks/useEditor.ts index 71072b3f1..d5c64a253 100644 --- a/packages/editor/src/lib/hooks/useEditor.ts +++ b/packages/editor/src/lib/hooks/useEditor.ts @@ -2,9 +2,15 @@ import React from 'react' import { Editor } from '../editor/Editor' /** @internal */ -export const EditorContext = React.createContext({} as Editor) +export const EditorContext = React.createContext(null) /** @public */ export function useEditor(): Editor { - return React.useContext(EditorContext) + const editor = React.useContext(EditorContext) + if (!editor) { + throw new Error( + 'useEditor must be used inside of the or components' + ) + } + return editor } diff --git a/packages/editor/src/lib/hooks/useEditorComponents.tsx b/packages/editor/src/lib/hooks/useEditorComponents.tsx index 842ceb4b0..2c3c920ea 100644 --- a/packages/editor/src/lib/hooks/useEditorComponents.tsx +++ b/packages/editor/src/lib/hooks/useEditorComponents.tsx @@ -86,7 +86,7 @@ export type TLEditorComponents = Partial< } & ErrorComponents > -const EditorComponentsContext = createContext({} as TLEditorComponents & ErrorComponents) +const EditorComponentsContext = createContext(null) type ComponentsContextProviderProps = { overrides?: TLEditorComponents @@ -140,5 +140,9 @@ export function EditorComponentsProvider({ /** @public */ export function useEditorComponents() { - return useContext(EditorComponentsContext) + const components = useContext(EditorComponentsContext) + if (!components) { + throw new Error('useEditorComponents must be used inside of ') + } + return components } diff --git a/packages/tldraw/api-report.md b/packages/tldraw/api-report.md index 55496c5b1..af4d151ee 100644 --- a/packages/tldraw/api-report.md +++ b/packages/tldraw/api-report.md @@ -2566,7 +2566,7 @@ export function useCanUndo(): boolean; export function useCopyAs(): (ids: TLShapeId[], format?: TLCopyType) => void; // @public (undocumented) -export const useCurrentTranslation: () => TLUiTranslation; +export function useCurrentTranslation(): TLUiTranslation; // @public (undocumented) export function useDefaultColorTheme(): { diff --git a/packages/tldraw/src/lib/ui/components/primitives/menus/TldrawUiMenuContext.tsx b/packages/tldraw/src/lib/ui/components/primitives/menus/TldrawUiMenuContext.tsx index 5ccde749d..b86d03918 100644 --- a/packages/tldraw/src/lib/ui/components/primitives/menus/TldrawUiMenuContext.tsx +++ b/packages/tldraw/src/lib/ui/components/primitives/menus/TldrawUiMenuContext.tsx @@ -16,11 +16,15 @@ export type TldrawUiMenuContextType = const menuContext = createContext<{ type: TldrawUiMenuContextType sourceId: TLUiEventSource -}>({ type: 'menu', sourceId: 'main-menu' }) +} | null>(null) /** @public */ export function useTldrawUiMenuContext() { - return useContext(menuContext) + const context = useContext(menuContext) + if (!context) { + throw new Error('useTldrawUiMenuContext must be used within a TldrawUiMenuContextProvider') + } + return context } /** @public */ diff --git a/packages/tldraw/src/lib/ui/context/actions.tsx b/packages/tldraw/src/lib/ui/context/actions.tsx index 889e87f95..b63c29322 100644 --- a/packages/tldraw/src/lib/ui/context/actions.tsx +++ b/packages/tldraw/src/lib/ui/context/actions.tsx @@ -56,7 +56,7 @@ export interface TLUiActionItem< export type TLUiActionsContextType = Record /** @internal */ -export const ActionsContext = React.createContext({}) +export const ActionsContext = React.createContext(null) /** @public */ export type ActionsProviderProps = { diff --git a/packages/tldraw/src/lib/ui/context/breakpoints.tsx b/packages/tldraw/src/lib/ui/context/breakpoints.tsx index 15ed5ad82..0ec9a110f 100644 --- a/packages/tldraw/src/lib/ui/context/breakpoints.tsx +++ b/packages/tldraw/src/lib/ui/context/breakpoints.tsx @@ -2,7 +2,7 @@ import { useEditor, useValue } from '@tldraw/editor' import React, { ReactNode, useContext } from 'react' import { PORTRAIT_BREAKPOINT, PORTRAIT_BREAKPOINTS } from '../constants' -const BreakpointContext = React.createContext(0) +const BreakpointContext = React.createContext(null) /** @public */ export function BreakPointProvider({ @@ -40,5 +40,9 @@ export function BreakPointProvider({ /** @public */ export function useBreakpoint() { - return useContext(BreakpointContext) + const breakpoint = useContext(BreakpointContext) + if (breakpoint === null) { + throw new Error('useBreakpoint must be used inside of the component') + } + return breakpoint } diff --git a/packages/tldraw/src/lib/ui/context/components.tsx b/packages/tldraw/src/lib/ui/context/components.tsx index 4c09d5dff..2601028a3 100644 --- a/packages/tldraw/src/lib/ui/context/components.tsx +++ b/packages/tldraw/src/lib/ui/context/components.tsx @@ -58,7 +58,7 @@ export type TLUiComponents = Partial<{ [K in keyof BaseTLUiComponents]: BaseTLUiComponents[K] | null }> -const TldrawUiComponentsContext = createContext({} as TLUiComponents) +const TldrawUiComponentsContext = createContext(null) /** @public */ export type TLUiComponentsProviderProps = { @@ -105,5 +105,9 @@ export function TldrawUiComponentsProvider({ /** @public */ export function useTldrawUiComponents() { - return useContext(TldrawUiComponentsContext) + const components = useContext(TldrawUiComponentsContext) + if (!components) { + throw new Error('useTldrawUiComponents must be used within a TldrawUiComponentsProvider') + } + return components } diff --git a/packages/tldraw/src/lib/ui/context/dialogs.tsx b/packages/tldraw/src/lib/ui/context/dialogs.tsx index d2a81c6af..98719022a 100644 --- a/packages/tldraw/src/lib/ui/context/dialogs.tsx +++ b/packages/tldraw/src/lib/ui/context/dialogs.tsx @@ -24,7 +24,7 @@ export type TLUiDialogsContextType = { } /** @internal */ -export const DialogsContext = createContext({} as TLUiDialogsContextType) +export const DialogsContext = createContext(null) /** @internal */ export type DialogsProviderProps = { diff --git a/packages/tldraw/src/lib/ui/context/events.tsx b/packages/tldraw/src/lib/ui/context/events.tsx index 5b84fc6ec..e3492a1c1 100644 --- a/packages/tldraw/src/lib/ui/context/events.tsx +++ b/packages/tldraw/src/lib/ui/context/events.tsx @@ -115,7 +115,7 @@ const defaultEventHandler: TLUiEventHandler = () => void null export type TLUiEventContextType = TLUiEventHandler /** @internal */ -export const EventsContext = React.createContext({} as TLUiEventContextType) +export const EventsContext = React.createContext(null) /** @public */ export type EventsProviderProps = { diff --git a/packages/tldraw/src/lib/ui/context/toasts.tsx b/packages/tldraw/src/lib/ui/context/toasts.tsx index d7fafe71c..911e7733e 100644 --- a/packages/tldraw/src/lib/ui/context/toasts.tsx +++ b/packages/tldraw/src/lib/ui/context/toasts.tsx @@ -33,7 +33,7 @@ export type TLUiToastsContextType = { } /** @internal */ -export const ToastsContext = createContext({} as TLUiToastsContextType) +export const ToastsContext = createContext(null) /** @internal */ export type ToastsProviderProps = { diff --git a/packages/tldraw/src/lib/ui/hooks/useTools.tsx b/packages/tldraw/src/lib/ui/hooks/useTools.tsx index 4e0fae9e9..05d975a0f 100644 --- a/packages/tldraw/src/lib/ui/hooks/useTools.tsx +++ b/packages/tldraw/src/lib/ui/hooks/useTools.tsx @@ -28,7 +28,7 @@ export interface TLUiToolItem< export type TLUiToolsContextType = Record /** @internal */ -export const ToolsContext = React.createContext({} as TLUiToolsContextType) +export const ToolsContext = React.createContext(null) /** @public */ export type TLUiToolsProviderProps = { diff --git a/packages/tldraw/src/lib/ui/hooks/useTranslation/useTranslation.tsx b/packages/tldraw/src/lib/ui/hooks/useTranslation/useTranslation.tsx index 837e752db..fcce8a5e2 100644 --- a/packages/tldraw/src/lib/ui/hooks/useTranslation/useTranslation.tsx +++ b/packages/tldraw/src/lib/ui/hooks/useTranslation/useTranslation.tsx @@ -23,12 +23,16 @@ export interface TLUiTranslationProviderProps { /** @public */ export type TLUiTranslationContextType = TLUiTranslation -const TranslationsContext = React.createContext( - {} as TLUiTranslationContextType -) +const TranslationsContext = React.createContext(null) /** @public */ -export const useCurrentTranslation = () => React.useContext(TranslationsContext) +export function useCurrentTranslation() { + const translations = React.useContext(TranslationsContext) + if (!translations) { + throw new Error('useCurrentTranslation must be used inside of ') + } + return translations +} /** * Provides a translation context to the editor.