Skip to content

Commit

Permalink
Makes EditorView non-nullable in useLayoutGroupEffect and `useEdito…
Browse files Browse the repository at this point in the history
…rEventCallback` (#83)

* only run callback if the view is defined

* only fire effects when view is available
  • Loading branch information
coolov authored Jan 30, 2024
1 parent 5456570 commit 0662f84
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/25690ba0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@nytimes/react-prosemirror": minor
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function SelectionWidget() {
const ref = useRef()

useEditorEffect((view) => {
if (!view || !ref.current) return
if (!ref.current) return

const viewClientRect = view.dom.getBoundingClientRect()
const coords = view.coordsAtPos(view.state.selection.anchor))
Expand Down Expand Up @@ -232,7 +232,6 @@ import { useEditorEventCallback } from "@nytimes/react-prosemirror";

export function BoldButton() {
const onClick = useEditorEventCallback((view) => {
if (!view) return;
const toggleBoldMark = toggleMark(view.state.schema.marks.bold);
toggleBoldMark(view.state, view.dispatch, view);
});
Expand Down Expand Up @@ -577,7 +576,7 @@ export function SelectionWidget() {
const ref = useRef()

useEditorEffect((view) => {
if (!view || !ref.current) return
if (!ref.current) return

const viewClientRect = view.dom.getBoundingClientRect()
const coords = view.coordsAtPos(view.state.selection.anchor))
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useEditorEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useLayoutGroupEffect } from "../contexts/LayoutGroup.js";
* EditorView lives in an ancestor component.
*/
export function useEditorEffect(
effect: (editorView: EditorView | null) => void | (() => void),
effect: (editorView: EditorView) => void | (() => void),
dependencies?: DependencyList
) {
const { editorView } = useContext(EditorContext);
Expand All @@ -33,7 +33,9 @@ export function useEditorEffect(
// every time it changes, because it will most likely
// be defined inline and run on every re-render.
useLayoutGroupEffect(
() => effect(editorView),
() => {
if (editorView) effect(editorView);
},
// The rules of hooks want to be able to statically
// verify the dependencies for the effect, but this will
// have already happened at the call-site.
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useEditorEventCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useEditorEffect } from "./useEditorEffect.js";
* providers.
*/
export function useEditorEventCallback<T extends unknown[], R>(
callback: (view: EditorView | null, ...args: T) => R
callback: (view: EditorView, ...args: T) => R
) {
const ref = useRef(callback);
const { editorView } = useContext(EditorContext);
Expand All @@ -29,7 +29,9 @@ export function useEditorEventCallback<T extends unknown[], R>(
}, [callback]);

return useCallback(
(...args: T) => ref.current(editorView, ...args),
(...args: T) => {
if (editorView) ref.current(editorView, ...args);
},
[editorView]
);
}

0 comments on commit 0662f84

Please sign in to comment.