shadcn-tiptap

repository·main·Indexed 20 days ago

https://github.com/niazmorshed2007/shadcn-tiptap

A collection of custom Tiptap editor extensions and toolbars styled for shadcn/ui. Features include a starter kit for basic formatting, search-and-replace, color and highlight tools, and advanced image handling via the ImagePlaceholder and Extended Image extensions. It provides a ToolbarProvider and useToolbar hook to manage editor instances within React contexts.

Tokens
15.9K
Snippets
60
Records
77
Agent score
63%

What's inside shadcn-tiptap

  1. Overview of shadcn-tiptap

    main

    shadcn-tiptap is a collection of tools designed to enhance the Tiptap editor using shadcn/ui components. It provides three main capabilities:

    1. Custom Tiptap Extensions: A set of specialized extensions built for the Tiptap editor.
    2. shadcn/ui Toolbars: Ready-to-use editor toolbars constructed using shadcn/ui components.
    3. CLI Integration: Easy setup and integration via the shadcn CLI.
  2. ImageExtension attributes and features

    main

    The ImageExtension is an extension of @tiptap/extension-image that adds the following attributes and capabilities:

    Attributes

    • src: The image source URL (default: null).
    • alt: Alternative text (default: null).
    • title: Image title/caption (default: null).
    • width: The width of the image (default: 100%). Supports manual resizing.
    • height: The height of the image (default: null).
    • align: The alignment of the image (left, center, or right).

    Features

    • Resizing: Drag the handles on the left or right sides of the image to scale it (minimum width: 150px).
    • Alignment: Use the toolbar buttons to align the image left, center, or right.
    • Toolbar Actions: Access via the MoreVertical menu:
      • Duplicate: Duplicates the image node.
      • Full Screen: Sets the width to fit-content.
      • Delete Image: Removes the node from the editor.
  3. Create custom toolbar buttons using useToolbar

    main

    To build custom toolbar components, use the useToolbar hook to access the Tiptap editor instance. Most toolbar components follow a pattern of using a Button wrapped in a Tooltip, where the onClick handler uses the editor's command chain (e.g., editor.chain().focus().toggleBold().run()).

    Key logic for toolbar buttons:

    • Active State: Use editor?.isActive('name') to apply active styling (e.g., bg-accent).
    • Availability: Use editor?.can().chain().focus().command().run() to disable the button when the command is not applicable.
    • Execution: Use editor?.chain().focus().command().run() to execute the command.
    import { useToolbar } from "@/components/toolbars/toolbar-provider";
    
    // Inside your component
    const { editor } = useToolbar();
    
    // Example usage in a button
    <Button
      onClick={() => editor?.chain().focus().toggleBold().run()}
      disabled={!editor?.can().chain().focus().toggleBold().run()}
      className={editor?.isActive("bold") ? "bg-accent" : ""}
    >
      Bold
    </Button>
  4. Install StarterKit via CLI

    main

    The fastest way to install the StarterKit extension and all its associated toolbar components is using the shadcn CLI. This command automatically adds the necessary toolbars to your project, including:

    • Bold, Italic, Strikethrough
    • Code, Code Block
    • Bullet List, Ordered List
    • Blockquote, Horizontal Rule, Hard Break
    • Redo, Undo
    npx shadcn add https://tiptap.niazmorshed.dev/r/starter-kit.json
  5. Configure utility helpers for the Image extension

    main

    If you are installing the extension manually or via CLI, ensure the following utility constants and functions are present in your @lib/utils.ts file to support styling and URL validation.

    export const NODE_HANDLES_SELECTED_STYLE_CLASSNAME = "node-handles-selected-style";
    
    export function isValidUrl(url: string) {
    	return /^https?:\/\/\S+$/.test(url);
    }
  6. Use the Search & Replace extension for TipTap

    main

    The Search & Replace extension enables users to find and replace specific text within a TipTap editor. It supports key features such as:

    • Search functionality: Locating specific strings within the document.
    • Case sensitivity: Toggling whether matches must respect character casing.
    • Sequential navigation: Moving through search results one by one.
    • Replace option: Swapping found text with new content.
  7. Manually install the Image placeholder toolbar

    main

    If you prefer manual installation, follow these steps:

    1. Install the required @shadcn/ui components: Button and Tooltip.
    2. Create a new file at components/toolbars/image-placeholder-toolbar.tsx and paste the component code provided below.

    The component uses the useToolbar hook to access the editor instance and executes the insertImagePlaceholder() command when clicked.

    "use client";
    
    import { Image } from "lucide-react";
    import React from "react";
    
    import { Button, type ButtonProps } from "@/components/ui/button";
    import {
    	Tooltip,
    	TooltipContent,
    	TooltipTrigger,
    } from "@/components/ui/tooltip";
    import { cn } from "@/lib/utils";
    import { useToolbar } from "@/components/toolbars/toolbar-provider";
    
    const ImagePlaceholderToolbar = React.forwardRef<
    	HTMLButtonElement,
    	ButtonProps
    >(({ className, onClick, children, ...props }, ref) => {
    	const { editor } = useToolbar();
    	return (
    		<Tooltip>
    			<TooltipTrigger asChild>
    				<Button
    					variant="ghost"
    					size="icon"
    					className={cn(
    						"h-8 w-8",
    						editor?.isActive("image-placeholder") && "bg-accent",
    						className,
    					)
    					onClick={(e) => {
    						editor?.chain().focus().insertImagePlaceholder().run();
    						oldClick?.(e);
    					}
    					ref={ref}
    					{...props}
    				>
    					{children || <Image className="h-4 w-4" />}
    				</Button>
    			</TooltipTrigger>
    			<TooltipContent>
    				<span>Image</span>
    			</TooltipContent>
    		</Tooltip>
    	);
    });
    
    ImagePlaceholderToolbar.displayName = "ImagePlaceholderToolbar";
    
    export { ImagePlaceholderToolbar };
  8. Install the Extended Image extension manually

    main

    To install the extension without the CLI, follow these steps:

    1. Install the core Tiptap image extension:
      npm install @tiptap/extension-image
    2. Install the required shadcn/ui components:
      • Button
      • DropdownMenu
      • Separator
    3. Create a new file at @components/extensions/image.tsx and paste the extension implementation code.
    npm install @tiptap/extension-image