Back to Plate

Plate Utils

content/docs/api/utils.mdx

53.0.86.9 KB
Original Source

@platejs/utils contains Plate's shared key constants, editor value types, and small utility plugins. @platejs/utils/react adds React hooks and the block placeholder plugin used by registry UI.

Installation

bash
npm install @platejs/utils

Application code usually imports this surface from platejs and platejs/react. Direct package imports are useful inside packages that should not depend on the umbrella platejs package.

Import Paths

ImportRe-exportsUse
@platejs/utilsplate-keys, plate-types, utility pluginsShared node keys, Plate element/mark types, and headless utility plugins.
@platejs/utils/reactReact hooks, BlockPlaceholderPluginRegistry controls and React-only utility behavior.
platejs@platejs/utilsApp-level imports for headless constants, types, and utility plugins.
platejs/react@platejs/utils/reactApp-level imports for React hooks and BlockPlaceholderPlugin.

Key Constants

KEYS is the canonical key map used by Plate packages, registry components, and plugin configuration.

ExportContainsNotes
NODESElement and mark node keys such as p, blockquote, codeBlock, table, bold, and linklink maps to the same node type as a.
STYLE_KEYSStyle property keys such as color, fontSize, indent, and textAlignUsed by style plugins and registry controls.
KEYSNODES, STYLE_KEYS, and plugin keys such as exitBreak, normalizeTypes, singleBlock, and trailingBlockAlso includes grouped values such as heading.
NodeKeyUnion of values from NODESUse for node-type values.
StyleKeyUnion of values from STYLE_KEYSUse for style keys.
PlateKeyUnion of values from KEYSIncludes string values and grouped key arrays.
ts
import { KEYS, TrailingBlockPlugin } from 'platejs';

export const trailingBlock = TrailingBlockPlugin.configure({
  options: {
    type: KEYS.p,
  },
});

Shared Types

plate-types exports common element, prop, media, list, table, mark, and suggestion shapes used across feature packages.

Type groupExamplesUse
Block elementsTCalloutElement, TCodeBlockElement, TColumnElement, TDateElement, TEquationElementTyped element props for feature packages and registry nodes.
Media elementsTImageElement, TAudioElement, TFileElement, TVideoElement, TMediaEmbedElementMedia nodes with url, id, upload, provider, and source metadata.
Table elementsTTableElement, TTableRowElement, TTableCellElement, TTableCellBorderTable structure, spans, sizes, backgrounds, and borders.
Shared propsTIdProps, TCaptionProps, TIndentProps, TResizableProps, TListPropsReusable node property contracts.
MarksTBasicMarks, TFontMarks, TCommentText, TSuggestionTextText marks and collaboration text state.
SuggestionsTSuggestionData, TInsertSuggestionData, TRemoveSuggestionData, TUpdateSuggestionDataSuggestion metadata stored on elements or text.
ts
import type { TImageElement } from 'platejs';

export function getImageUrl(element: TImageElement) {
  return element.url;
}

Utility Plugins

PluginKeyBehavior
ExitBreakPluginKEYS.exitBreakAdds editor.tf.insert and editor.tf.insertBefore wrappers around insertExitBreak.
NormalizeTypesPluginKEYS.normalizeTypesNormalizes configured root paths to a required type or strictType.
SingleBlockPluginKEYS.singleBlockForces the editor value into one block and turns hard breaks into soft breaks.
SingleLinePluginKEYS.singleLineForces one block and strips line-break characters from text nodes.
TrailingBlockPluginKEYS.trailingBlockEnsures a trailing block exists at the configured level and type.
withTrailingBlockOverride editor helperImplements the trailing block normalization logic used by TrailingBlockPlugin.

Use the plugin guide pages for options and examples: Exit Break, Forced Layout, Single Block, and Trailing Block.

React Hooks

HookReturnsUse
useEditorString()stringReads editor.api.string([]) through useEditorSelector.
useFormInputProps(options?){ props }Adds an optional onKeyDownCapture handler that prevents Enter from submitting a wrapper form.
useMarkToolbarButtonState({ nodeType, clear? }){ clear, nodeType, pressed }Reads whether a mark is active.
useMarkToolbarButton(state){ props }Provides pressed, onClick, and onMouseDown props that toggle a mark and focus the editor.
useRemoveNodeButton({ element }){ props }Provides button props that remove an element by path.
useSelectionCollapsed()booleanSelection is collapsed.
useSelectionExpanded()booleanSelection is expanded.
useSelectionWithinBlock()booleanSelection is inside one block.
useSelectionAcrossBlocks()booleanSelection spans blocks.
useSelectionFragment()Descendant[]Reads the selected fragment while unwrapping container types.
useSelectionFragmentProp(options?)unknownReads a property from the selected fragment.
tsx
import {
  useMarkToolbarButton,
  useMarkToolbarButtonState,
} from 'platejs/react';

export function MarkToolbarButton({ nodeType }: { nodeType: string }) {
  const state = useMarkToolbarButtonState({ nodeType });
  const { props } = useMarkToolbarButton(state);

  return <button type="button" aria-pressed={props.pressed} {...props} />;
}

React Plugins

PluginKeyBehavior
BlockPlaceholderPluginKEYS.blockPlaceholderTracks the current empty block and injects placeholder and optional className props into matching block components.

BlockPlaceholderPlugin defaults to paragraph placeholders and only targets a focused, editable, collapsed selection. Configure placeholders by plugin key and query by node/path.

tsx
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';

export const blockPlaceholderPlugin = BlockPlaceholderPlugin.configure({
  options: {
    placeholders: {
      [KEYS.p]: 'Type something...',
    },
    query: ({ path }) => path.length === 1,
  },
});
  • Plate covers the umbrella package that re-exports these APIs.
  • Plate Core covers editor creation, plugin contracts, and stores.
  • React Utils covers @udecode/react-utils, which is re-exported through platejs/react.
  • Toolbar covers registry controls that use the React hook helpers.