Skip to content
GitHubDiscord

Text Color

The textColor mark is used to represent text with a specific color. It will be rendered as <span> element with inline color style in HTML.

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import {
  createEditor,
  type NodeJSON,
} from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { sampleContent } from '../../sample/sample-doc-text-color'

import { defineExtension } from './extension'
import InlineMenu from './inline-menu'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension, defaultContent })
  }, [defaultContent])

  return (
    <ProseKit editor={editor}>
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
          <InlineMenu />
        </div>
      </div>
    </ProseKit>
  )
}

Add a color to the current selection. This command will override any existing color on the selected text.

editorconst editor: Editor<TextColorExtension>.commands
Editor<TextColorExtension>.commands: ToCommandAction<{
    addTextColor: [attrs: TextColorAttrs];
    removeTextColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addTextColor
addTextColor: CommandAction
(attrs: TextColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorTextColorAttrs.color: string: 'red' })
editorconst editor: Editor<TextColorExtension>.commands
Editor<TextColorExtension>.commands: ToCommandAction<{
    addTextColor: [attrs: TextColorAttrs];
    removeTextColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addTextColor
addTextColor: CommandAction
(attrs: TextColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorTextColorAttrs.color: string: '#0000ff' })
editorconst editor: Editor<TextColorExtension>.commands
Editor<TextColorExtension>.commands: ToCommandAction<{
    addTextColor: [attrs: TextColorAttrs];
    removeTextColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addTextColor
addTextColor: CommandAction
(attrs: TextColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorTextColorAttrs.color: string: 'rgb(255, 0, 0)' })
editorconst editor: Editor<TextColorExtension>.commands
Editor<TextColorExtension>.commands: ToCommandAction<{
    addTextColor: [attrs: TextColorAttrs];
    removeTextColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addTextColor
addTextColor: CommandAction
(attrs: TextColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorTextColorAttrs.color: string: 'var(--color-primary)' })

Remove the color from the current selection.

editorconst editor: Editor<TextColorExtension>.commands
Editor<TextColorExtension>.commands: ToCommandAction<{
    addTextColor: [attrs: TextColorAttrs];
    removeTextColor: [];
}>
All {@link CommandAction } s defined by the editor.
.removeTextColor
removeTextColor: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()