# Markdown Typewriter This library renders Markdown with [@tanstack/markdown](https://www.npmjs.com/package/@tanstack/markdown) and reveals it with a typewriter animation. Like @tanstack/markdown, it has two entry points: * `markdown-typewriter/react`: the `MarkdownTypewriter` React component, animated with [motion](https://www.npmjs.com/package/motion). * `markdown-typewriter/core`: the framework-agnostic part (no React, no motion) — the @tanstack/markdown extensions it parses with and the typewriter timing rules — to build the same effect with any other framework or plain JavaScript. See [Core (without React)](#core-without-react). > **Renamed:** this package was published as `react-markdown-typewriter` up to 2.0; it is now `markdown-typewriter`. Every release is still published under both names, but `react-markdown-typewriter` is deprecated: see [Migrating from 2.0](#migrating-from-20). Live demo: > The effect produced by this library closely resembles the streaming text animation used by AI chat interfaces such as **ChatGPT**, **Claude**, **Gemini**, and similar tools — where responses appear character by character as they are generated. If you are building an AI-powered chat UI or any application that streams text to the user, this library gives you that familiar, polished look with full Markdown support out of the box. **Upgrading from 1.x?** Version 2 replaces react-markdown with @tanstack/markdown. See [Migrating from 1.x](#migrating-from-1x). ## Why? This library was born during the development of my game engine [pixi-vn](https://www.npmjs.com/package/@drincs/pixi-vn). I needed a component that would display the current dialogue of a character with the "Typewriter" effect and I also wanted to give the developer the possibility to use Markdown to add style to the text. For this reason I created this component that I later decided to make available on npm. ## Install ```bash # React npm install markdown-typewriter motion # core only (no React) npm install markdown-typewriter ``` `@tanstack/markdown` is installed automatically. The React entry point also needs `react`, `react-dom` (18 or newer) and `motion` (12.9 or newer), declared as optional peer dependencies (the core does not use them). The package ships both ESM (`import`) and CommonJS (`require`) builds. `@tanstack/markdown` is published as ESM only, so the CommonJS build bundles it: `require("markdown-typewriter")` works even where `require()` cannot load ES modules. ## Use This is a very simple example of how to use the component: ```tsx import { MarkdownTypewriter } from "markdown-typewriter/react"; export default function NarrationScreen() { return (
Hello **World**
); } ``` This is a more complex example: ```tsx import { useCallback, useRef } from "react"; import { MarkdownTypewriter } from "markdown-typewriter/react"; export default function NarrationScreen() { const paragraphRef = useRef(null); const scrollToEnd = useCallback((ref: { current: HTMLSpanElement | null }) => { if (paragraphRef.current && ref.current) { const scrollTop = ref.current.offsetTop - paragraphRef.current.clientHeight / 2; paragraphRef.current.scrollTo({ top: scrollTop, behavior: "auto", }); } }, []); return (
{ console.log("Typewriter finished"); }, characterVariants: { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { opacity: { duration: 0 } } }, }, onCharacterAnimationComplete: scrollToEnd, }} > {'Hello blue *World*'}
); } ``` ## Markdown support The Markdown syntax is the one supported by [@tanstack/markdown](https://github.com/TanStack/markdown): headings, paragraphs, emphasis, strong, links, images, inline code and fenced code blocks, lists, blockquotes and thematic breaks, plus the GFM features most commonly used — **tables**, **strikethrough**, **task lists** and **footnotes** — with no plugin to install. HTML character references such as ` `, `&` or `…` are decoded, as in CommonMark. Extra syntax is enabled with @tanstack/markdown [extensions](https://github.com/TanStack/markdown/blob/main/docs/guides/extensions.md), passed through the `extensions` prop. For example, GitHub-style alerts (`> [!NOTE]`): ```tsx import { calloutsExtension } from "@tanstack/markdown/extensions/callouts"; import { MarkdownTypewriter } from "markdown-typewriter/react"; const extensions = [calloutsExtension()]; export default function Note() { return {"> [!NOTE]\n> Typed out too."}; } ``` Keep the `extensions` array stable (define it outside the component or memoize it): the Markdown is parsed again whenever it changes. ## Raw HTML Raw HTML is disabled by default (it is rendered as plain text). Enable it with `allowHtml`: ```tsx {'The sky is deep **blue** tonight.
A new line.'}
``` Unlike `@tanstack/markdown`'s own `Markdown` component (which injects raw HTML with `dangerouslySetInnerHTML`), `MarkdownTypewriter` rebuilds the HTML into real React elements: * the text inside HTML elements is **typed out like the rest of the document**, and Markdown inside them (`**bold**`, `*italic*`, links…) still works; * attributes are kept: `style` (converted to a React style object), `class` (as `className`) and the others, so `…` is blue while it types; * block elements wrapping Markdown work too (`
`, blank line, Markdown, blank line, `
`); * unsafe content is dropped: `