Skip to content
GitHubDiscord

Background Color

The backgroundColor mark is used to represent text with a specific background color. It will be rendered as <span> element with inline background-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 background color to the current selection. This command will override any existing background color on the selected text.

editorconst editor: Editor<BackgroundColorExtension>.commands
Editor<BackgroundColorExtension>.commands: ToCommandAction<{
    addBackgroundColor: [attrs: BackgroundColorAttrs];
    removeBackgroundColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addBackgroundColor
addBackgroundColor: CommandAction
(attrs: BackgroundColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorBackgroundColorAttrs.color: string: 'yellow' })
editorconst editor: Editor<BackgroundColorExtension>.commands
Editor<BackgroundColorExtension>.commands: ToCommandAction<{
    addBackgroundColor: [attrs: BackgroundColorAttrs];
    removeBackgroundColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addBackgroundColor
addBackgroundColor: CommandAction
(attrs: BackgroundColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorBackgroundColorAttrs.color: string: '#ffff00' })
editorconst editor: Editor<BackgroundColorExtension>.commands
Editor<BackgroundColorExtension>.commands: ToCommandAction<{
    addBackgroundColor: [attrs: BackgroundColorAttrs];
    removeBackgroundColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addBackgroundColor
addBackgroundColor: CommandAction
(attrs: BackgroundColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorBackgroundColorAttrs.color: string: 'rgb(255, 255, 0)' })
editorconst editor: Editor<BackgroundColorExtension>.commands
Editor<BackgroundColorExtension>.commands: ToCommandAction<{
    addBackgroundColor: [attrs: BackgroundColorAttrs];
    removeBackgroundColor: [];
}>
All {@link CommandAction } s defined by the editor.
.addBackgroundColor
addBackgroundColor: CommandAction
(attrs: BackgroundColorAttrs) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ colorBackgroundColorAttrs.color: string: 'var(--color-highlight)' })

Remove the background color from the current selection.

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