Download slate/packages/slate-react/src/hooks/use-selected.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/slate/packages/slate-react/src/hooks/use-selected.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/slate/packages/slate-react/src/hooks/use-selected.ts
-
curl -L -o use-selected.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/slate/packages/slate-react/src/hooks/use-selected.ts
1.28 kB
| 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, | |
| }) | |
| } | |