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

fix(suggestion): 🐛 make clientrect prop optional #2813

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion demos/src/Examples/Community/React/suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ReactRenderer } from '@tiptap/react'
import tippy from 'tippy.js'

import { ReactRenderer } from '@tiptap/react'

import { MentionList } from './MentionList'

export default {
Expand All @@ -20,6 +22,10 @@ export default {
editor: props.editor,
})

if (!props.clientRect) {
return
}

popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
Expand All @@ -34,6 +40,10 @@ export default {
onUpdate(props) {
reactRenderer.updateProps(props)

if (!props.clientRect) {
return
}

popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
Expand Down
12 changes: 11 additions & 1 deletion demos/src/Examples/Community/Vue/suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { VueRenderer } from '@tiptap/vue-3'
import tippy from 'tippy.js'

import { VueRenderer } from '@tiptap/vue-3'

import MentionList from './MentionList.vue'

export default {
Expand All @@ -23,6 +25,10 @@ export default {
editor: props.editor,
})

if (!props.clientRect) {
return
}

popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
Expand All @@ -37,6 +43,10 @@ export default {
onUpdate(props) {
component.updateProps(props)

if (!props.clientRect) {
return
}

popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
Expand Down
10 changes: 10 additions & 0 deletions demos/src/Experiments/Commands/Vue/suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import tippy from 'tippy.js'

import { VueRenderer } from '@tiptap/vue-3'

import CommandsList from './CommandsList.vue'

export default {
Expand Down Expand Up @@ -66,6 +68,10 @@ export default {
editor: props.editor,
})

if (!props.clientRect) {
return
}

popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
Expand All @@ -80,6 +86,10 @@ export default {
onUpdate(props) {
component.updateProps(props)

if (!props.clientRect) {
return
}

popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
Expand Down
12 changes: 11 additions & 1 deletion demos/src/Nodes/Mention/React/suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ReactRenderer } from '@tiptap/react'
import tippy from 'tippy.js'

import { ReactRenderer } from '@tiptap/react'

import MentionList from './MentionList.jsx'

export default {
Expand Down Expand Up @@ -46,6 +48,10 @@ export default {
editor: props.editor,
})

if (!props.clientRect) {
return
}

popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
Expand All @@ -60,6 +66,10 @@ export default {
onUpdate(props) {
component.updateProps(props)

if (!props.clientRect) {
return
}
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is nice, I don't think this actually works.

Down below, you see:

getReferenceClientRect: props.clientRect

When you look at Tippy's typings, it goes something like this:

export interface GetReferenceClientRect {
  (): ClientRect | DOMRect;
  contextElement?: Element;
}

...
export interface Props extends LifecycleHooks, RenderProps {
  getReferenceClientRect: null | GetReferenceClientRect;
  ...
}

Tippy could handle if props.clientRect is null, but in practice I've seen it be the function. Tippy expects () => ClientRect | DOMRect whereas we've got () => ClientRect | DOMRect | null.

It is kinda problematic.

On the one hand, you could run the function yourself, see if it is null and then pass it in if it is not, but that doesn't really work because it could be null by the time Tippy runs it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Tricky issue since I wasn't able to reproduce it even by blowing up my machine with 10k console.logs per editor state change. 🤡

I'll think about this a bit and search for a better and cleaner solution.

The worst solution but also the worst case could be that we pass out a fake clientRect object but that feels pretty messy.


popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
Expand Down
12 changes: 11 additions & 1 deletion demos/src/Nodes/Mention/Vue/suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { VueRenderer } from '@tiptap/vue-3'
import tippy from 'tippy.js'

import { VueRenderer } from '@tiptap/vue-3'

import MentionList from './MentionList.vue'

export default {
Expand All @@ -24,6 +26,10 @@ export default {
editor: props.editor,
})

if (!props.clientRect) {
return
}

popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
Expand All @@ -38,6 +44,10 @@ export default {
onUpdate(props) {
component.updateProps(props)

if (!props.clientRect) {
return
}

popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
Expand Down
9 changes: 5 additions & 4 deletions packages/suggestion/src/suggestion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Editor, Range } from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'

import { Editor, Range } from '@tiptap/core'

import { findSuggestionMatch } from './findSuggestionMatch'

export interface SuggestionOptions<I = any> {
Expand Down Expand Up @@ -44,7 +46,7 @@ export interface SuggestionProps<I = any> {
items: I[],
command: (props: I) => void,
decorationNode: Element | null,
clientRect: (() => DOMRect) | null,
clientRect?: (() => DOMRect | null) | null,
}

export interface SuggestionKeyDownProps {
Expand Down Expand Up @@ -123,8 +125,7 @@ export function Suggestion<I = any>({
const { decorationId } = this.key?.getState(editor.state)
const currentDecorationNode = document.querySelector(`[data-decoration-id="${decorationId}"]`)

// @ts-ignore-error
return currentDecorationNode.getBoundingClientRect()
return currentDecorationNode?.getBoundingClientRect() || null
}
: null,
}
Expand Down