Skip to content

Commit

Permalink
Make onDomSelectionChange triggered after onClick. (#4132)
Browse files Browse the repository at this point in the history
* Delay onDomSelectionChange but not cause performance issue.

* Add changeset

* Update changeset

Changeset editor via GitHub's UI seems to violate our prettier rules 🤦🏼‍♂️

Co-authored-by: Dylan Schiemann <[email protected]>
  • Loading branch information
ulion and dylans authored Aug 22, 2021
1 parent 3e7ff3b commit 48b7129
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-beans-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'slate-react': patch
'slate': patch
---

Make onDomSelectionChange trigger after onClick.
14 changes: 11 additions & 3 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,22 +511,30 @@ export const Editable = (props: EditableProps) => {
[readOnly]
)

const scheduleOnDOMSelectionChange = useCallback(
() => setTimeout(onDOMSelectionChange),
[onDOMSelectionChange]
)

// Attach a native DOM event handler for `selectionchange`, because React's
// built-in `onSelect` handler doesn't fire for all selection changes. It's a
// leaky polyfill that only fires on keypresses or clicks. Instead, we want to
// fire for any change to the selection inside the editor. (2019/11/04)
// https://github.com/facebook/react/issues/5785
useIsomorphicLayoutEffect(() => {
const window = ReactEditor.getWindow(editor)
window.document.addEventListener('selectionchange', onDOMSelectionChange)
window.document.addEventListener(
'selectionchange',
scheduleOnDOMSelectionChange
)

return () => {
window.document.removeEventListener(
'selectionchange',
onDOMSelectionChange
scheduleOnDOMSelectionChange
)
}
}, [onDOMSelectionChange])
}, [scheduleOnDOMSelectionChange])

const decorations = decorate([editor, []])

Expand Down

0 comments on commit 48b7129

Please sign in to comment.