🎁 100 free AI Toolkit licenses – apply by August 15.Learn more

在你的编辑器中集成按钮

Available for free

一个可点击的元素,在激活时执行某个操作。

安装

你可以通过 Tiptap CLI 添加该原始组件

npx @tiptap/cli@latest add button

用法

import { Button } from '@/components/tiptap-ui-primitive/button'
import { BoldIcon } from '@/components/tiptap-icons/bold-icon'

export default function MyComponent() {
  return (
    <Button
      data-style="ghost"
      data-active-state="on"
      tooltip="粗体"
      shortcutKeys="Ctrl+B"
      onClick={() => console.log('粗体被点击')}
    >
      <BoldIcon className="tiptap-button-icon" />
      <span className="tiptap-button-text">粗体</span>
    </Button>
  )
}

属性

按钮

NameTypeDefaultDescription
variant'ghost' | 'primary' | 'check'undefined视觉样式,或 check 功能变体
size'small' | 'default' | 'large'undefined按钮尺寸
tooltipReactNodeundefined工具提示中显示的内容
shortcutKeysstringundefined工具提示中显示的键盘快捷键
showTooltipbooleantrue设置 tooltip 时渲染工具提示包装器
data-stylestringundefined按钮样式,在提供 variant 时根据其设置
data-active-state'on' | 'off'undefined按钮激活状态
data-sizestringundefined按钮尺寸,在提供 size 时根据其设置
data-appearancestringundefined按钮外观
data-disabledbooleanundefined视觉上的禁用状态

该按钮会将所有其他 ButtonHTMLAttributes<HTMLButtonElement> 转发到底层元素。

Check 变体

variant="check" 会渲染复选框样式的切换按钮:一个全宽行,左侧显示标签,右侧显示勾选指示器。它默认为小尺寸的 ghost 样式,提供 role="checkbox",并渲染自身的指示器,因此你只需提供标签和状态。

使用 aria-checked 控制它,该属性默认为 false,也接受 "mixed"

import { useState } from 'react'
import { Button } from '@/components/tiptap-ui-primitive/button'
import { CaseSensitiveIcon } from '@/components/tiptap-icons/case-sensitive-icon'

export default function MatchCaseOption() {
  const [matchCase, setMatchCase] = useState(false)

  return (
    <Button
      variant="check"
      aria-checked={matchCase}
      onClick={() => setMatchCase((current) => !current)}
    >
      <CaseSensitiveIcon className="tiptap-button-icon" />
      <span className="tiptap-button-text">Match case</span>
    </Button>
  )
}

该变体会在元素上设置 data-variant="check",并将指示器渲染为 .tiptap-button-check,因此这两者都可用于设置样式。其颜色来自 button-colors.scss 中的 --tt-button-check-* 自定义属性,并会安装 check-icon 组件。