Now Available: Notion-like editor templateNotion-style editor for Tiptap Cloud

Color highlight popover

Available for free

A fully accessible highlight color selection popover for Tiptap editors. Apply background highlights with a curated color palette, remove highlights, and provide an intuitive interface for text highlighting with keyboard shortcuts and mobile support.

Installation

Add the component via the Tiptap CLI:

npx @tiptap/cli@latest add color-highlight-popover

Components

<ColorHighlightPopover />

A prebuilt React component that provides highlight color selection in a compact popover interface.

Usage

import { EditorContent, EditorContext, useEditor } from '@tiptap/react'
import { StarterKit } from '@tiptap/starter-kit'
import { Highlight } from '@tiptap/extension-highlight'
import { ColorHighlightPopover } from '@/components/tiptap-ui/color-highlight-popover'

import '@/components/tiptap-node/paragraph-node/paragraph-node.scss'

export default function MyEditor() {
  const editor = useEditor({
    immediatelyRender: false,
    extensions: [StarterKit, Highlight.configure({ multicolor: true })],
    content: `
        <p style="text-align: left">
            <mark data-color="var(--tt-color-highlight-purple)" style="background-color: var(--tt-color-highlight-purple); color: inherit">
                <span class="selection">Highlight text</span>
            </mark>
            <span class="selection"> </span>
            <mark data-color="var(--tt-color-highlight-red)" style="background-color: var(--tt-color-highlight-red); color: inherit">
                <span class="selection">in</span>
            </mark>
            <span class="selection"> </span>
            <mark data-color="var(--tt-color-highlight-green)" style="background-color: var(--tt-color-highlight-green); color: inherit">
                <span class="selection">different</span>
            </mark>
            <span class="selection"> </span>
            <mark data-color="var(--tt-color-highlight-blue)" style="background-color: var(--tt-color-highlight-blue); color: inherit">
                <span class="selection">colors</span>
            </mark>
            <span class="selection"> to draw attention to key points.</span>
        </p>
        `,
  })

  return (
    <EditorContext.Provider value={{ editor }}>
      <ColorHighlightPopover
        editor={editor}
        hideWhenUnavailable={true}
        onApplied={({ color, label }) => console.log(`Applied highlight: ${label} (${color})`)}
      />

      <EditorContent editor={editor} role="presentation" />
    </EditorContext.Provider>
  )
}

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
colorsHighlightColor[]Default paletteCustom highlight colors to display
hideWhenUnavailablebooleanfalseHides the popover when highlighting is not available
onAppliedHighlightHandlerundefinedCallback fired when a highlight is applied or removed

<ColorHighlightPopoverContent />

The content component that displays the color palette and remove option. Can be used independently for custom popover implementations.

Usage

<PopoverContent>
  <ColorHighlightPopoverContent editor={editor} colors={customColors} />
</PopoverContent>

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
colorsHighlightColor[]Default paletteCustom highlight colors to display

Hooks

useColorHighlight()

A custom hook to build your own highlight functionality with full control over behavior and rendering.

Usage

function MyHighlightButton() {
  const {
    isVisible,
    isActive,
    canColorHighlight,
    handleColorHighlight,
    handleRemoveHighlight,
    label,
    shortcutKeys,
    Icon,
  } = useColorHighlight({
    editor: myEditor,
    highlightColor: 'var(--tt-color-highlight-yellow)',
    label: 'Yellow Highlight',
    hideWhenUnavailable: true,
    onApplied: ({ color, label }) => {
      console.log(`Highlight applied: ${label} (${color})`)
    },
  })

  if (!isVisible) return null

  return (
    <button
      onClick={handleColorHighlight}
      disabled={!canColorHighlight}
      aria-label={label}
      aria-pressed={isActive}
    >
      <Icon />
      {label}
      <kbd>{shortcutKeys}</kbd>
    </button>
  )
}

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
highlightColorstringundefinedCSS color value for the highlight
labelstringundefinedAccessible label for the highlight
hideWhenUnavailablebooleanfalseHide when highlighting is not available
onAppliedHighlightHandlerundefinedCallback for highlight changes

Return Values

NameTypeDescription
isVisiblebooleanWhether the button should be rendered
isActivebooleanIf the highlight is currently active
canColorHighlightbooleanIf highlighting is currently allowed
handleColorHighlight() => booleanFunction to apply/toggle the highlight
handleRemoveHighlight() => booleanFunction to remove any highlight
labelstringAccessible label text for the button
shortcutKeysstringKeyboard shortcut (Cmd/Ctrl + Shift + H)
IconReact.FCIcon component for the highlight button

Utility Functions

canColorHighlight(editor)

Checks if highlighting is available in the current editor state.

import { canColorHighlight } from '@/components/tiptap-ui/color-highlight-button'

const canHighlight = canColorHighlight(editor)
if (canHighlight) {
  // Show highlight options
}

removeHighlight(editor)

Programmatically removes any highlight from the current selection.

import { removeHighlight } from '@/components/tiptap-ui/color-highlight-button'

const success = removeHighlight(editor)
if (success) {
  console.log('Highlight removed!')
}

Keyboard Shortcuts

The highlight popover supports the following keyboard shortcut:

  • Cmd/Ctrl + Shift + H: Toggle highlight (when a specific color is configured)
// Shortcut works automatically with useColorHighlight hook
const { shortcutKeys } = useColorHighlight({
  editor,
  highlightColor: 'var(--tt-color-highlight-yellow)',
})

console.log(shortcutKeys) // "mod+shift+h"

Requirements

Dependencies

  • @tiptap/react - Core Tiptap React integration
  • @tiptap/extension-highlight - Highlight extension for text highlighting

Referenced Components

  • use-tiptap-editor (hook)
  • use-menu-navigation (hook)
  • use-mobile (hook)
  • button (primitive)
  • popover (primitive)
  • card (primitive)
  • separator (primitive)
  • highlighter-icon (icon)
  • ban-icon (icon)
  • color-highlight-button (component)
  • tiptap-utils (lib)