# Link {#link}

Source: [Add and remove links in a React editor · Yurich](https://leeyujoo.com/yurich/features/link/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

Add a URL to a selection or insert a link at the caret.

## Try it {#try-it}

Select “the formatting guide”, click **Add link**, and use `https://example.com`. The output includes `<a href="https://example.com">the formatting guide</a>`. Click inside the link or select its text, then click **Remove link**.

## Add it to your editor {#setup}

Enable `Link` and call `editor.execute('setLink', { href })`. This example opens a prompt while preserving the selection and adds a separate remove button.

```tsx
import { EditorContent, EditorProvider, Link, useEditor, useIsActive } from 'yurich';

export function LinkEditor() {
  const editor = useEditor({
    extensions: [Link],
    initialValue: "<p>Read the formatting guide before clicking every button.</p>",
  });
  const active = useIsActive(editor, 'link');

  function addLink() {
    const href = window.prompt("Link URL", 'https://example.com');
    if (href) editor.execute('setLink', { href });
  }

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="Link">
        <button
          type="button"
          onMouseDown={event => event.preventDefault()}
          onClick={addLink}
        >
          Add link
        </button>
        <button
          type="button"
          disabled={!active}
          onMouseDown={event => event.preventDefault()}
          onClick={() => editor.execute('removeLink')}
        >
          Remove link
        </button>
      </div>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}
```

## Behavior to account for {#behavior}

The supported URL prefixes are `http://`, `https://`, and `mailto:`. At a caret, adding a link inserts its URL as the visible text. `removeLink` can remove a whole link at the caret.

[Features](https://leeyujoo.com/yurich/features/)
