Font family extension for Tiptap rich text editor that allows applying custom font families to selected text
npx @tessl/cli install tessl/npm-tiptap--extension-font-family@3.4.0Font Family Extension is a Tiptap editor extension that enables users to apply custom font families to selected text within Tiptap editor instances. It extends Tiptap's core functionality by integrating with the text-style extension to provide seamless font family styling capabilities.
npm install @tiptap/extension-font-family @tiptap/extension-text-styleimport { FontFamily } from "@tiptap/extension-font-family";
import FontFamily from "@tiptap/extension-font-family"; // default importFor CommonJS:
const { FontFamily } = require("@tiptap/extension-font-family");Import types:
import { FontFamilyOptions } from "@tiptap/extension-font-family";import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import { FontFamily } from "@tiptap/extension-font-family";
import { TextStyle } from "@tiptap/extension-text-style";
// Create editor with FontFamily extension
const editor = new Editor({
element: document.querySelector('#editor'),
extensions: [
StarterKit,
TextStyle, // Required dependency
FontFamily.configure({
types: ['textStyle'], // Default configuration
}),
],
});
// Set font family for selected text
editor.commands.setFontFamily('Arial, sans-serif');
editor.commands.setFontFamily('Georgia, serif');
// Remove font family from selected text
editor.commands.unsetFontFamily();Main extension class that provides font family functionality to Tiptap editor.
/**
* FontFamily extension for Tiptap editor
* Provides commands to set and unset font family on selected text
*/
declare const FontFamily: Extension<FontFamilyOptions>;
export default FontFamily;
export { FontFamily };Extension configuration interface defining which node types support font family styling.
export interface FontFamilyOptions {
/**
* A list of node names where the font family can be applied.
* @default ['textStyle']
* @example ['heading', 'paragraph']
*/
types: string[]
}The extension adds font family commands to the Tiptap editor instance.
Sets font family on selected text or at current cursor position.
/**
* Set the font family for selected text
* @param fontFamily The font family value (CSS font-family format)
* @returns Command execution result
* @example editor.commands.setFontFamily('Arial, sans-serif')
* @example editor.commands.setFontFamily('Georgia, "Times New Roman", serif')
*/
editor.commands.setFontFamily(fontFamily: string): boolean;Usage Examples:
// Set single font family
editor.commands.setFontFamily('Arial');
// Set font family with fallbacks
editor.commands.setFontFamily('Helvetica, Arial, sans-serif');
// Set font family with quoted names for fonts with spaces
editor.commands.setFontFamily('"Times New Roman", Times, serif');
// Set web font
editor.commands.setFontFamily('"Open Sans", sans-serif');Removes font family styling from selected text.
/**
* Remove font family styling from selected text
* @returns Command execution result
* @example editor.commands.unsetFontFamily()
*/
editor.commands.unsetFontFamily(): boolean;Usage Examples:
// Remove font family from current selection
editor.commands.unsetFontFamily();
// Chain with other commands
editor.commands.selectAll().unsetFontFamily();This extension requires and integrates with @tiptap/extension-text-style to function properly.
The extension adds font family support to configured node types:
interface TextStyleAttributes {
fontFamily?: string | null;
}The extension automatically handles HTML conversion:
style="font-family: ..." attributes from HTML elementsHTML Example:
<!-- Input HTML -->
<p>This text has <span style="font-family: Arial, sans-serif">custom font family</span>.</p>
<!-- Editor automatically parses the font-family style -->
<!-- Output HTML maintains the same format -->Complete TypeScript definitions for all exported types:
/**
* Configuration options for FontFamily extension
*/
export interface FontFamilyOptions {
types: string[];
}
/**
* Extension class type
*/
export declare const FontFamily: Extension<FontFamilyOptions>;
/**
* Module augmentation for editor commands
*/
declare module '@tiptap/core' {
interface Commands<ReturnType> {
fontFamily: {
setFontFamily: (fontFamily: string) => ReturnType;
unsetFontFamily: () => ReturnType;
}
}
}
/**
* Module augmentation for text style attributes
*/
declare module '@tiptap/extension-text-style' {
interface TextStyleAttributes {
fontFamily?: string | null;
}
}This extension has peer dependencies that must be installed:
npm install @tiptap/extension-font-family @tiptap/extension-text-styleThe @tiptap/extension-text-style package must be included in your editor extensions array for FontFamily to work properly.