Find out what's new in Tiptap Editor 3.0

Color Text Button

A fully accessible text color button for Tiptap editors. Apply foreground colors to selected text with keyboard shortcut support and flexible customization options.

Installation

Add the component via the Tiptap CLI:

npx @tiptap/cli@latest add color-text-button

Components

<ColorTextButton />

A prebuilt React component that applies text colors to selected text.

Usage

export default function MyEditor() {
  return (
    <ColorTextButton
      editor={editor}
      textColor="var(--tt-color-text-blue)"
      text="Blue Text"
      hideWhenUnavailable={true}
      showShortcut={true}
      onApplied={({ color, label }) => console.log(`Applied ${label} text color: ${color}`)}
    />
  )
}

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
textColorstringrequiredThe text color to apply (CSS color value)
textstringundefinedOptional text label for the button
hideWhenUnavailablebooleanfalseHides the button when text color is not applicable
onApplied({ color, label }) => voidundefinedCallback fired after applying a text color
showShortcutbooleanfalseShows a keyboard shortcut badge (if available)

Hooks

useColorText(config)

A custom hook to build your own color text button with full control over rendering and behavior.

Usage

function MyColorTextButton() {
  const { isVisible, isActive, canColorText, handleColorText, label, shortcutKeys, Icon } =
    useColorText({
      editor: myEditor,
      textColor: 'var(--tt-color-text-red)',
      label: 'Red Text',
      hideWhenUnavailable: true,
      onApplied: ({ color, label }) => console.log(`Applied: ${label}`),
    })

  if (!isVisible) return null

  return (
    <button
      onClick={handleColorText}
      disabled={!canColorText}
      aria-label={label}
      aria-pressed={isActive}
      style={{ color: isActive ? textColor : 'inherit' }}
    >
      <Icon />
      {label}
      {shortcutKeys && <Badge>{parseShortcutKeys({ shortcutKeys })}</Badge>}
    </button>
  )
}

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
textColorstringrequiredThe text color to apply
labelstringrequiredAccessible label for the button
hideWhenUnavailablebooleanfalseHides the button if text color cannot be applied
onApplied({ color, label }) => voidundefinedCallback fired after applying text color

Return Values

NameTypeDescription
isVisiblebooleanWhether the button should be rendered
isActivebooleanIf the text color is currently active
canColorTextbooleanIf the text color can be applied
handleColorText() => booleanFunction to apply the text color
labelstringAccessible label text for the button
shortcutKeysstringKeyboard shortcut (Cmd/Ctrl + Shift + T)
IconReact.FCIcon component for the text color button

Utilities

canColorText(editor)

Checks if text color can be applied in the current editor state.

import { canColorText } from '@/registry/tiptap-ui/color-text-button'

const canApply = canColorText(editor)

isColorTextActive(editor, color)

Checks if a specific text color is currently active in the selection.

import { isColorTextActive } from '@/registry/tiptap-ui/color-text-button'

const isRedActive = isColorTextActive(editor, 'red')
const isBlueActive = isColorTextActive(editor, 'var(--tt-color-text-blue)')

Keyboard Shortcuts

The color text button supports the following keyboard shortcut:

  • Cmd/Ctrl + Shift + T: Apply text color

The shortcut is automatically registered when using either the <ColorTextButton /> component or the useColorText() hook. It applies the configured text color to the current selection.

Requirements

Dependencies

  • @tiptap/react - Core Tiptap React integration
  • @tiptap/extension-text-style - Text style extension for color support
  • react-hotkeys-hook - Keyboard shortcut management

Referenced Components

  • use-tiptap-editor (hook)
  • button (primitive)
  • badge (primitive)
  • tiptap-utils (lib)
  • text-color-small-icon (icon)