āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/udecode/plate/(plugins)/(elements)/link ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
title: Link docs:
The fastest way to add link functionality is with the LinkKit, which includes pre-configured LinkPlugin with floating toolbar and Plate UI components.
LinkElement: Renders link elements.LinkFloatingToolbar: Provides floating toolbar for link editing.Add the kit to your plugins:
import { createPlateEditor } from 'platejs/react';
import { LinkKit } from '@/components/editor/plugins/link-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...LinkKit,
],
});
</Steps>
npm install @platejs/link
Include LinkPlugin in your Plate plugins array when creating the editor.
import { LinkPlugin } from '@platejs/link/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
LinkPlugin,
],
});
Configure the plugin with floating toolbar and custom components.
import { LinkPlugin } from '@platejs/link/react';
import { createPlateEditor } from 'platejs/react';
import { LinkElement } from '@/components/ui/link-node';
import { LinkFloatingToolbar } from '@/components/ui/link-toolbar';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
LinkPlugin.configure({
render: {
node: LinkElement,
afterEditable: () => <LinkFloatingToolbar />,
},
}),
],
});
render.afterEditable: Renders LinkFloatingToolbar after the editable area for link editing.render.node: Assigns LinkElement to render link elements.You can add LinkToolbarButton to your Toolbar to insert and edit links.
LinkPluginPlugin for link formatting.
<API name="LinkPlugin"> <APIOptions type="object"> <APIItem name="forceSubmit" type="boolean" optional> Determines whether to force the submission of the link form. </APIItem> <APIItem name="rangeBeforeOptions" type="RangeBeforeOptions" optional> Allows custom configurations for rangeBeforeOptions. - **Default:** ```ts { matchString: ' ', skipInvalid: true, afterMatch: true, } ``` </APIItem> <APIItem name="triggerFloatingLinkHotkeys" type="string | string[]" optional> Hotkeys to trigger floating link. - **Default:** **`'meta+k, ctrl+k'`** </APIItem> <APIItem name="allowedSchemes" type="string[]" optional> List of allowed URL schemes. - **Default:** **`['http', 'https', 'mailto', 'tel']`** </APIItem> <APIItem name="dangerouslySkipSanitization" type="boolean" optional> Determines whether the sanitation of links should be skipped. - **Default:** **`false`** </APIItem> <APIItem name="defaultLinkAttributes" type="AnchorHTMLAttributes<HTMLAnchorElement>" optional> Default HTML attributes for link elements. - **Default:** **`{}`** </APIItem> <APIItem name="keepSelectedTextOnPaste" type="boolean" optional> Keeps selected text on pasting links by default. - **Default:** **`true`** </APIItem> <APIItem name="isUrl" type="(text: string) => boolean" optional> Callback function to validate a URL. - **Default:** **`isUrl`** </APIItem> <APIItem name="getUrlHref" type="(url: string) => string | undefined" optional> Callback function to optionally get the href for a URL. It returns an optional link that is different from the text content. For example, returns `https://google.com` for `google.com`. </APIItem> <APIItem name="transformInput" type="(url: string | null) => string | undefined" optional> Callback function to optionally transform the submitted URL provided by the user to the URL input before validation. </APIItem> <APIItem name="getLinkUrl" type="(prevUrl: string | null) => Promise<string | null>" optional> On keyboard shortcut or toolbar mousedown, this function is called to get the link URL. The default behavior is to use the browser's native `prompt`. </APIItem> </APIOptions> </API>tf.insert.linkInserts a link node into the editor.
<API name="insert.link"> <APIParameters> <APIItem name="options" type="object"> Options for inserting the link. </APIItem> </APIParameters> <APIOptions type="InsertLinkOptions"> <APIItem name="createLinkNodeOptions" type="CreateLinkNodeOptions"> Options for creating the link node. </APIItem> <APIItem name="insertOptions" type="InsertNodesOptions" optional> Additional options for inserting nodes. </APIItem> </APIOptions> </API>api.floatingLink.hideHides the floating link and resets its state.
api.floatingLink.resetResets the floating link state without changing the openEditorId.
api.floatingLink.showShows the floating link for the specified mode and editor ID.
<API name="floatingLink.show"> <APIParameters> <APIItem name="mode" type="FloatingLinkMode"> The mode to set for the floating link ('edit' or 'insert'). </APIItem> <APIItem name="editorId" type="string"> The ID of the editor where the floating link should be shown. </APIItem> </APIParameters> </API>api.link.getAttributesGets the attributes for a link element.
<API name="link.getAttributes"> <APIParameters> <APIItem name="element" type="TLinkElement"> The link element for which to get attributes. </APIItem> </APIParameters> <APIReturns type="React.AnchorHTMLAttributes<HTMLAnchorElement>"> The HTML attributes for the link element. </APIReturns> </API>api.link.submitFloatingLinkInserts a link if the URL is valid, closes the floating link, and focuses the editor.
<APIReturns type="boolean"> Returns `true` if the link was inserted successfully. </APIReturns>insertLinkInserts a link node into the editor.
<API name="insertLink"> <APIParameters> <APIItem name="createLinkNodeOptions" type="CreateLinkNodeOptions"> Options for creating link node. </APIItem> <APIItem name="options" type="InsertNodesOptions" optional> Additional options for node insertion. </APIItem> </APIParameters> </API>submitFloatingLinkInserts a link if the URL is valid, closes the floating link, and focuses the editor.
triggerFloatingLinkTriggers the floating link.
<API name="triggerFloatingLink"> <APIOptions type="object"> <APIItem name="focused" type="boolean" optional> Whether the floating link should be focused. </APIItem> </APIOptions> </API>triggerFloatingLinkEditTriggers the floating link edit.
<API name="triggerFloatingLinkEdit"> <APIReturns type="boolean"> Returns `true` if the link was edited. </APIReturns> </API>triggerFloatingLinkInsertTrigger floating link. Do not trigger when:
unwrapLinkUnwraps a link node.
<API name="unwrapLink"> <APIOptions type="UnwrapLinkOptions"> <APIItem name="split" type="boolean" optional> If `true`, split the nodes if the selection is inside the link. </APIItem> </APIOptions> </API>upsertLinkInsert or update a link node. The behavior depends on the current selection and options:
insertTextInLink: true, inserts URL as text in linktext is empty, sets it to URLskipValidation: trueupdate: true in a link:
text is provided, replaces link textupsertLinkTextIf the text is different from the link above text, replaces the link children with a new text node. The new text node has the same marks as the first text node in the link.
<API name="upsertLinkText"> <APIOptions type="UpsertLinkTextOptions"> <APIItem name="text" type="string" optional> The new text to replace the link children with. </APIItem> </APIOptions> </API>validateUrlValidates a URL based on the plugin options.
<API name="validateUrl"> <APIOptions type="ValidateUrlOptions"> <APIItem name="url" type="string"> The URL to validate. </APIItem> </APIOptions> <APIReturns type="boolean"> Returns `true` if the URL is valid. </APIReturns> </API>wrapLinkWrap a link node with split.
<API name="wrapLink"> <APIOptions type="WrapLinkOptions"> <APIItem name="url" type="string"> The URL of the link. </APIItem> <APIItem name="target" type="string" optional> The target attribute of the link. </APIItem> </APIOptions> </API>CreateLinkNodeOptionsOptions for creating a new link node.
<API name="CreateLinkNodeOptions"> <APIOptions type="object"> <APIItem name="url" type="string"> The URL of the link node that is being created. </APIItem> <APIItem name="text" type="string" optional> The text that is displayed for the link node. If not provided, the URL is used as the display text. </APIItem> <APIItem name="target" type="string" optional> Specifies where to open the URL: - `_blank`: new tab - `_self`: same frame - `_parent`: parent frame - `_top`: full window </APIItem> <APIItem name="children" type="TText[]" optional> An array of text nodes that represent the link content. </APIItem> </APIOptions> </API>FloatingLinkNewTabInputThe input component for controlling whether a link opens in a new tab.
<API name="FloatingLinkNewTabInput"> <APIState> <APIItem name="checked" type="boolean"> Whether the link should open in a new tab. </APIItem> <APIItem name="setChecked" type="React.Dispatch<React.SetStateAction<boolean>>"> Function to update the checked state. </APIItem> <APIItem name="ref" type="RefObject<HTMLInputElement>"> Reference to the input element. </APIItem> </APIState> </API>FloatingLinkUrlInputThe input component for entering and editing link URLs.
<API name="FloatingLinkUrlInput"> <APIState> <APIItem name="ref" type="RefObject<HTMLInputElement>"> Reference to the input element. </APIItem> </APIState> </API>LinkOpenButtonThe button component for opening the link URL.
<API name="LinkOpenButton"> <APIState> <APIItem name="element" type="TLinkElement"> The link element containing the URL to open. </APIItem> </APIState> </API>useFloatingLinkEditThe behavior hook for the floating link edit functionality.
<API name="useFloatingLinkEdit"> <APIState> <APIItem name="floating" type="object" optional> The virtual floating returned object. </APIItem> </APIState> <APIReturns type="object"> <APIItem name="ref" type="function"> The ref callback for the floating element. </APIItem> <APIItem name="props" type="object"> Props for the floating element. <APISubList> <APISubListItem parent="props" name="style" type="object"> The style of the floating link. </APISubListItem> </APISubList> </APIItem> <APIItem name="editButtonProps" type="object"> Props for the edit button. <APISubList> <APISubListItem parent="editButtonProps" name="onClick" type="function"> The function to call when the edit button is clicked. </APISubListItem> </APISubList> </APIItem> <APIItem name="unlinkButtonProps" type="object"> Props for the unlink button. <APISubList> <APISubListItem parent="unlinkButtonProps" name="onClick" type="function"> The function to call when the unlink button is clicked. </APISubListItem> </APISubList> </APIItem> </APIReturns> </API>useFloatingLinkEnterListens for the Enter key press event and submits the floating link in the editor.
useFloatingLinkEscapeListens for the Escape key press event and handles the behavior of the floating link in the editor.
useFloatingLinkInsertThe behavior hook for inserting a link.
<API name="useFloatingLinkInsert"> <APIState> <APIItem name="floating" type="ReturnType<typeof useFloatingLinkInsertState>"> The virtual floating returned object. </APIItem> <APIItem name="refClickOutside" type="React.Ref"> The ref of the floating element. </APIItem> </APIState> <APIReturns type="object"> <APIItem name="ref" type="function"> The ref callback for the floating element. </APIItem> <APIItem name="props" type="object"> Props for the floating element. <APISubList> <APISubListItem parent="props" name="style" type="object"> The style of the floating link. </APISubListItem> </APISubList> </APIItem> <APIItem name="textInputProps" type="object"> Props for the text input. <APISubList> <APISubListItem parent="textInputProps" name="onChange" type="function"> The function to call when the text input value changes. </APISubListItem> <APISubListItem parent="textInputProps" name="defaultValue" type="string"> The default value of the text input. </APISubListItem> </APISubList> </APIItem> </APIReturns> </API>useLinkThe behavior hook for the link element.
<API name="useLink"> <APIOptions type="UseLinkOptions"> <APIItem name="element" type="TLinkElement"> The link element. </APIItem> </APIOptions> <APIReturns type="object"> <APIItem name="props" type="object"> Props for the link element. <APISubList> <APISubListItem parent="props" name="onMouseOver" type="function"> The function to call when the mouse is over the link. </APISubListItem> </APISubList> </APIItem> </APIReturns> </API>useLinkToolbarButtonThe behavior hook for the link toolbar button.
<API name="useLinkToolbarButton"> <APIState> <APIItem name="pressed" type="boolean"> Whether the selection is in a link. </APIItem> </APIState> <APIReturns type="object"> <APIItem name="props" type="object"> Props for the toolbar button. <APISubList> <APISubListItem parent="props" name="pressed" type="boolean"> Whether the link is pressed. </APISubListItem> <APISubListItem parent="props" name="onClick" type="function"> The function to call when the button is clicked. </APISubListItem> </APISubList> </APIItem> </APIReturns> </API>useVirtualFloatingLinkCustom hook for managing virtual floating of a link.
<API name="useVirtualFloatingLink"> <APIOptions type="object"> <APIItem name="editorId" type="string"> The ID of the editor to which the link belongs. </APIItem> <APIItem name="floatingOptions" type="UseVirtualFloatingOptions" optional> Options for virtual floating. </APIItem> </APIOptions> <APIReturns type="UseVirtualFloatingReturn"> The return value of the `useVirtualFloating` hook. </APIReturns> </API>ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā