# 标题 {#heading}

Source: [为 React 编辑器添加 H1 至 H6 标题 · Yurich](https://leeyujoo.com/yurich/zh-cn/features/heading/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

将段落转为 H1 到 H6 的标题。

## 试一试 {#try-it}

将光标放进 “工具栏自己做” 并点 **标题 2**，会生成 `<h2>工具栏自己做</h2>`。**段落** 恢复段落。

## 添加到编辑器 {#setup}

启用 `Heading` 并给 `setHeading` 传入 1 到 6 的数字。

```tsx
import { EditorContent, EditorProvider, Heading, useEditor, useIsActive } from 'yurich';

export function HeadingEditor() {
  const editor = useEditor({
    extensions: [Heading],
    initialValue: "<p>工具栏自己做</p>" + "<p>先加一个加粗按钮。</p>",
  });
  const isH2 = useIsActive(editor, 'setHeading', 2);

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="标题">
        <button
          type="button"
          aria-pressed={isH2}
          onMouseDown={event => event.preventDefault()}
          onClick={() => editor.execute('setHeading', 2)}
        >
          标题 2
        </button>
        <button
          type="button"
          onMouseDown={event => event.preventDefault()}
          onClick={() => editor.execute('removeHeading')}
        >
          段落
        </button>
      </div>
      <EditorContent ariaLabel="编辑器" />
    </EditorProvider>
  );
}
```

## 行为说明 {#behavior}

多块选择会作用于每个选中块。列表项设为标题后会移出列表。

[功能](https://leeyujoo.com/yurich/zh-cn/features/)
