Block Selection
Select and manipulate entire text blocks.
📸 Image
📺 Embed
Features
- Select entire blocks, as opposed to individual words or characters.
- To select an entire block, mouse down outside the text area and then move the cursor into the block. Once it is selected, you'll see a background color.
- Keep moving down or up to select multiple blocks.
- Once selected, the available actions are: copy, cut, and delete.
- Keyboard shortcuts:
Cmd+A
(Mac) /Ctrl+A
(Windows/Linux):- First press: Selects the current block
- Double press: Selects the whole document using block selection
- Note: This behavior can be disabled by setting
handlers.onKeyDown = null
when creating the plugin
Installation
npm install @udecode/plate-selection @udecode/plate-node-id
Usage
import { NodeIdPlugin } from '@udecode/plate-node-id';
import { BlockSelectionPlugin } from '@udecode/plate-selection/react';
const plugins = [
// ...otherPlugins,
NodeIdPlugin,
BlockSelectionPlugin,
];
Add data-plate-selectable to the container or the element you want to start block selection.
Example:
<PlateContent
aria-disabled={disabled}
className={cn(
editorVariants({
disabled,
focusRing,
focused,
size,
variant,
}),
className
)}
data-plate-selectable
disableDefaultStyles
readOnly={disabled ?? readOnly}
{...props}
/>
Styling
You can style the selection area by adding this class to the container:
'[&_.slate-selected]:!bg-primary/20 [&_.slate-selection-area]:border [&_.slate-selection-area]:border-primary [&_.slate-selection-area]:bg-primary/10'
Plugins
BlockSelectionPlugin
Options
- Default:
false
- Default:
false
- Default:
{ maxLevel: 1 }
- Default:
new Set()
Options for the selection area. Example:
{
boundaries: ['#selection-demo #scroll_container'],
container: ['#selection-demo #scroll_container'],
selectables: ['#selection-demo #scroll_container .slate-selectable'],
selectionAreaClass: 'slate-selection-area',
}
The padding-right of the editor.
Enables or disables the context menu for block selection.
Indicates whether block selection is currently active.
A function to handle the keydown
event when selecting.
Options for querying nodes during block selection.
A set of IDs for the currently selected blocks.
BlockContextMenuPlugin
This plugin is used by BlockSelectionPlugin
and doesn't need to be added manually.
API
editor.api.blockSelection.addSelectedRow
Adds a selected row to the block selection.
Parameters
The ID of the row to be selected.
editor.api.blockSelection.getSelectedBlocks
Gets the selected blocks in the editor.
Returns
An array of selected block entries.
editor.api.blockSelection.resetSelectedIds
Resets the set of selected IDs to an empty set.
editor.api.blockSelection.selectedAll
Selects all selectable blocks in the editor.
editor.api.blockSelection.setSelectedIds
Sets the selected IDs based on added and removed elements.
Parameters
editor.api.blockSelection.unselect
Unselects all blocks and sets the isSelecting
flag to false.
API Components
BlockSelectable
Props
BlockSelection
A wrapper component that adds block selection functionality to its children.
Props
The content to be wrapped with block selection functionality.
Hooks
useBlockSelectableState
Returns
Whether the block is active for selection.
The element associated with the block.
The path of the block in the editor.
A ref to the block's DOM element.
useBlockSelectable
Returns
Props to be spread on the block's wrapper element.
useSelectionArea
A hook that initializes and manages the selection area functionality.