Skip to content

Commit

Permalink
Capture element selections through useSelected (#4365)
Browse files Browse the repository at this point in the history
* Capture element selections (#4338)

* Rebase

* Add changeset
  • Loading branch information
samarsault authored Aug 12, 2021
1 parent 62d1a06 commit 906e5af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-crabs-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

fix a bug where element selections were not captured by useSelected
7 changes: 1 addition & 6 deletions packages/slate-react/src/components/element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Editor, Node, Range, NodeEntry, Element as SlateElement } from 'slate'
import Text from './text'
import useChildren from '../hooks/use-children'
import { ReactEditor, useSlateStatic, useReadOnly } from '..'
import { SelectedContext } from '../hooks/use-selected'
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
import {
NODE_TO_ELEMENT,
Expand Down Expand Up @@ -131,11 +130,7 @@ const Element = (props: {
}
})

return (
<SelectedContext.Provider value={!!selection}>
{renderElement({ attributes, children, element })}
</SelectedContext.Provider>
)
return renderElement({ attributes, children, element })
}

const MemoizedElement = React.memo(Element, (prev, next) => {
Expand Down
21 changes: 12 additions & 9 deletions packages/slate-react/src/hooks/use-children.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RenderLeafProps,
RenderPlaceholderProps,
} from '../components/editable'
import { SelectedContext } from './use-selected'

/**
* Children.
Expand Down Expand Up @@ -60,15 +61,17 @@ const useChildren = (props: {

if (Element.isElement(n)) {
children.push(
<ElementComponent
decorations={ds}
element={n}
key={key.id}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={sel}
/>
<SelectedContext.Provider value={!!sel}>
<ElementComponent
decorations={ds}
element={n}
key={key.id}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={sel}
/>
</SelectedContext.Provider>
)
} else {
children.push(
Expand Down

0 comments on commit 906e5af

Please sign in to comment.