Skip to content

Commit

Permalink
chore(storybook): Prevent keyDown propagation with hidden suggestio…
Browse files Browse the repository at this point in the history
…n dropdown
  • Loading branch information
rfgamaral committed Sep 30, 2024
1 parent f1367f7 commit 983b772
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions stories/typist-editor/extensions/hashtag-suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ const HashtagSuggestion: SuggestionExtensionResult<HashtagSuggestionItem> =

// These flag variables control when the renderer functions are allowed to be called,
// and they are needed to work around a few issues with Tiptap's suggestion utility:
// * https://github.com/ueberdosis/tiptap/issues/214
// * https://github.com/ueberdosis/tiptap/issues/2547
// * https://github.com/ueberdosis/tiptap/issues/2592
let isDropdownHidden = false
let isDropdownInitialized = false
let wasDropdownDestroyed = false

Expand All @@ -71,6 +73,12 @@ const HashtagSuggestion: SuggestionExtensionResult<HashtagSuggestionItem> =
getReferenceClientRect() {
return props.clientRect?.() || DOM_RECT_FALLBACK
},
onHide() {
isDropdownHidden = true
},
onShow() {
isDropdownHidden = false
},
content: reactRenderer.element,
duration: [150, 200],
interactive: true,
Expand Down Expand Up @@ -124,13 +132,12 @@ const HashtagSuggestion: SuggestionExtensionResult<HashtagSuggestionItem> =
})
},
onKeyDown(props) {
if (!isDropdownInitialized) {
if (!isDropdownInitialized || isDropdownHidden) {
return false
}

if (props.event.key === 'Escape') {
dropdown[0].destroy()
reactRenderer.destroy()
dropdown[0].hide()
return true
}

Expand Down
10 changes: 9 additions & 1 deletion stories/typist-editor/extensions/mention-suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ const MentionSuggestion: SuggestionExtensionResult<MentionSuggestionItem> =

// These flag variables control when the renderer functions are allowed to be called,
// and they are needed to work around a few issues with Tiptap's suggestion utility:
// * https://github.com/ueberdosis/tiptap/issues/214
// * https://github.com/ueberdosis/tiptap/issues/2547
// * https://github.com/ueberdosis/tiptap/issues/2592
let isDropdownHidden = false
let isDropdownInitialized = false
let wasDropdownDestroyed = false

Expand All @@ -64,6 +66,12 @@ const MentionSuggestion: SuggestionExtensionResult<MentionSuggestionItem> =
getReferenceClientRect() {
return props.clientRect?.() || DOM_RECT_FALLBACK
},
onHide() {
isDropdownHidden = true
},
onShow() {
isDropdownHidden = false
},
content: reactRenderer.element,
duration: [150, 200],
interactive: true,
Expand All @@ -88,7 +96,7 @@ const MentionSuggestion: SuggestionExtensionResult<MentionSuggestionItem> =
})
},
onKeyDown(props) {
if (!isDropdownInitialized) {
if (!isDropdownInitialized || isDropdownHidden) {
return false
}

Expand Down

0 comments on commit 983b772

Please sign in to comment.