forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-selected.ts
More file actions
37 lines (32 loc) · 1.25 KB
/
use-selected.ts
File metadata and controls
37 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useCallback } from 'react'
import { Editor, Range } from 'slate'
import { useElementIf } from './use-element'
import { useSlateSelector } from './use-slate-selector'
import { ReactEditor } from '../plugin/react-editor'
/**
* Get the current `selected` state of an element.
*/
export const useSelected = (): boolean => {
const element = useElementIf()
// Breaking the rules of hooks is fine here since `!element` will remain true
// or false for the entire lifetime of the component this hook is called from.
// TODO: Decide if we want to throw an error instead when calling
// `useSelected` outside of an element (potentially a breaking change).
if (!element) return false
// eslint-disable-next-line react-hooks/rules-of-hooks
const selector = useCallback(
(editor: Editor) => {
if (!editor.selection) return false
const path = ReactEditor.findPath(editor, element)
const range = Editor.range(editor, path)
return !!Range.intersection(range, editor.selection)
},
[element]
)
// eslint-disable-next-line react-hooks/rules-of-hooks
return useSlateSelector(selector, undefined, {
// Defer the selector until after `Editable` has rendered so that the path
// will be accurate.
deferred: true,
})
}