-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change how Slate context updates and introduce
useSlateSelection
ho…
…ok (#5041) * Fix DOM selection sync when there are unexpected rerenders * Create a useSlateSelection hook and expose it * update docs * add changeset * Undo the useEffect change and add a useSlateValue method * Use a version counter instead for SlateContext * comment out layout effect prevention for now * Undo useV comparison for now * Change the changeset * Fix lint * Remove the useSlateValue hook * remove some unused imports * Add useSlateWithV to the docs * fix changeset lint * Change changeset to minor instead
- Loading branch information
Showing
7 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'slate-react': minor | ||
--- | ||
|
||
- Introduces a `useSlateSelection` hook that triggers whenever the selection changes. | ||
- This also changes the implementation of SlateContext to use an incrementing value instead of an array replace to trigger updates | ||
- Introduces a `useSlateWithV` hook that includes the version counter which can be used to prevent re-renders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BaseSelection, Range } from 'slate' | ||
|
||
import { useSlateSelector } from './use-slate-selector' | ||
|
||
/** | ||
* Get the current slate selection. | ||
* Only triggers a rerender when the selection actually changes | ||
*/ | ||
export const useSlateSelection = () => { | ||
return useSlateSelector(editor => editor.selection, isSelectionEqual) | ||
} | ||
|
||
const isSelectionEqual = (a: BaseSelection, b: BaseSelection) => { | ||
if (!a && !b) return true | ||
if (!a || !b) return false | ||
return Range.equals(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters