"use client" import * as React from "react" // --- Lib --- import { parseShortcutKeys } from "@/lib/tiptap-utils" // --- Tiptap UI --- import type { Level, UseHeadingConfig, } from "@/components/tiptap-ui/heading-button" import { HEADING_SHORTCUT_KEYS, useHeading, } from "@/components/tiptap-ui/heading-button" // --- UI Primitives --- import type { ButtonProps } from "@/components/tiptap-ui-primitive/button" import { Button } from "@/components/tiptap-ui-primitive/button" import { Badge } from "@/components/tiptap-ui-primitive/badge" import { useTiptapEditor } from "@/hooks/use-tiptap-editor" export interface HeadingButtonProps extends Omit, UseHeadingConfig { /** * Optional text to display alongside the icon. */ text?: string /** * Optional show shortcut keys in the button. * @default false */ showShortcut?: boolean } export function HeadingShortcutBadge({ level, shortcutKeys = HEADING_SHORTCUT_KEYS[level], }: { level: Level shortcutKeys?: string }) { return {parseShortcutKeys({ shortcutKeys })} } /** * Button component for toggling heading in a Tiptap editor. * * For custom button implementations, use the `useHeading` hook instead. */ export const HeadingButton = React.forwardRef< HTMLButtonElement, HeadingButtonProps >( ( { editor: providedEditor, level, text, hideWhenUnavailable = false, onToggled, showShortcut = false, onClick, children, ...buttonProps }, ref ) => { const { editor } = useTiptapEditor(providedEditor) const { isVisible, canToggle, isActive, handleToggle, label, Icon, shortcutKeys, } = useHeading({ editor, level, hideWhenUnavailable, onToggled, }) const handleClick = React.useCallback( (event: React.MouseEvent) => { onClick?.(event) if (event.defaultPrevented) return handleToggle() }, [handleToggle, onClick] ) if (!isVisible) { return null } return ( ) } ) HeadingButton.displayName = "HeadingButton"