Skip to content

Typewriter Component โœ โ€‹

๐Ÿ“Œ Warning

XMarkdown Component has been released and can be used in combination with the Typewriter Component. Please upgrade to version beta 1.2.2.

๐Ÿ’Œ Info

v1.2.0 version provides simple solutions for style override, chart rendering, and custom code highlighting styles, custom plugins

  1. We've added the official prismjs CSS style file to the component library, which can be directly imported in projects to solve md code block highlighting issues.

  2. We've added Mermaid.js to the component library. Used to solve simple chart rendering issues with mermaid format.

  3. We've exposed the code highlighting methods and plugins built into markdown-it. Making it easier for developers to better integrate third-party ecosystem styles and plugins

๐Ÿต This friendly reminder update time: 2025-07-06

Introduction โ€‹

Typewriter is a highly customizable typewriter component, inspired by the ant-design-x official bubble component case, extracting the typing method. Supports Markdown rendering and dynamic typing effects.

Code Examples โ€‹

Basic Usage โ€‹

Basic usage.

Markdown Rendering โ€‹

Control whether to enable Markdown rendering mode through the isMarkdown attribute.

MD-Code Block Highlighting (v1.2.0 New) โ€‹

Provides a built-in style

ts

// Import different theme styles for Prism syntax highlighting (based on style files provided by vue-element-plus-x plugin)
// Each file corresponds to an independent code highlighting theme style, which can be selected and enabled according to project requirements

// 1. Coy theme (simple light style, suitable for daily reading)
import 'vue-element-plus-x/styles/prism-coy.min.css'

// 2. Dark theme (dark background theme, suitable for night mode or low-light environments)
import 'vue-element-plus-x/styles/prism-dark.min.css'

// 3. Funky theme (vibrant color style, strong contrast in code syntax highlighting)
import 'vue-element-plus-x/styles/prism-funky.min.css'

// 4. Okaidia theme (dark high-contrast theme, focuses on code structure distinction)
import 'vue-element-plus-x/styles/prism-okaidia.min.css'

// 5. Solarized Light theme (soft light theme, based on Solarized color scheme)
import 'vue-element-plus-x/styles/prism-solarizedlight.min.css'

// 6. Tomorrow theme (modern minimalist style, suitable for wide screens and large fonts)
import 'vue-element-plus-x/styles/prism-tomorrow.min.css'

// 7. Twilight theme (twilight color theme, balanced style between light and dark)
import 'vue-element-plus-x/styles/prism-twilight.min.css'

// 8. Prism core base styles (must be imported, contains basic styles and structure for syntax highlighting)
import 'vue-element-plus-x/styles/prism.min.css'

/* Usage Instructions:
1. prism.min.css is the core style of Prism, containing basic code block layout and common styles, must be retained
2. Other files starting with prism- are different theme styles, which can be selected and imported according to project visual design
3. If multiple themes are imported simultaneously, later imported styles will override earlier ones (can dynamically switch themes by switching class names)
4. Theme names correspond to Prism official preset themes (such as Coy, Okaidia, etc.), style details can refer to Prism theme documentation
*/

MD-Plugin Mode (v1.2.0 New) โ€‹

If you think the built-in styles don't look good or the built-in plugins can't meet your needs, you can customize styles and plugins through plugin mode.

You can also find custom plugins in the markdown-it community to implement more custom functionality. Through the md-plugins attribute, pass in a markdown-it plugin array to use custom plugins in markdown-it. Through the highlight function, pass in Prism's highlighting function, or other highlighting libraries, to use Prism's highlighting functionality in markdown-it.

For detailed Mermaid format, see: Mermaid.js

Enable Typing Effect โ€‹

Control whether to enable typing rendering mode through the typing attribute. typing can also be an object, setting the step property to control how many characters to type each time, the interval property to control typing interval, and the suffix property to control the suffix added during typing.

๐Ÿ“Œ Warning

The suffix attribute can only be set to a string, and it becomes ineffective when isMarkdown is true, because the suffix will be affected by markdown rendering and will always be displayed on a new line, which also occurs in ant-design-x. So we temporarily decided not to display the suffix when isMarkdown is true, to make the typewriter as beautiful as possible.

Typewriter Fog Effect โ€‹

Control whether to enable fog effect through the isFog attribute. Note that this attribute only takes effect when isTyping is true. It will override the default typing suffix attribute.

Dynamic Content Updates โ€‹

๐Ÿ’ When using the typing attribute, updating content will continue output if it's a subset of the previous content, otherwise it will restart output.

Control Typing โ€‹

๐Ÿ’ฉ Better control over interrupting output, continuing typing, and destroying operations You can get the following methods and properties through the component's ref instance:

  • interrupt Interrupt typing process typerRef.interrupt()
  • continue Continue unfinished typing typerRef.continue()
  • restart Restart typing typerRef.restart()
  • destroy Destroy component (clean up resources) typerRef.destroy()
  • renderedContent Get current rendered content. typerRef.renderedContent.value
  • isTyping Get whether currently typing. typerRef.isTyping.value
  • progress Get current progress percentage. typerRef.progress.value

๐Ÿ’ก Tip

You can also set component monitoring events to get component state.

  • @start Triggered when typing starts
  • @finish Triggered when typing ends
  • @writing Triggered during typing

Three methods, default parameter returns component instance.

Attributes โ€‹

AttributeTypeRequiredDefaultDescription
contentStringNo''Text content to display, supports plain text or Markdown format.
isMarkdownBooleanNofalseWhether to enable Markdown rendering mode.
typingBoolean | { step?: number, interval?: number, suffix?: string }NofalseWhether to enable typewriter effect.
typing.stepNumberNo2How many characters to type each time.
typing.intervalNumberNo50Interval time for each typing, unit (ms).
typing.suffixStringNo'|'Typewriter suffix cursor character (only effective in non-Markdown mode).
isFogBoolean | { bgColor?: string, width?: string }NofalseWhether to enable fog effect, can set background color and width.

Events โ€‹

Event NameParametersTypeDescription
@startref instanceFunctionTriggered when typing effect starts
@finishref instanceFunctionTriggered when typing effect completes
@writingref instanceFunctionContinuously triggered during typing effect

Ref Instance Methods โ€‹

Property NameTypeDescription
interruptFunctionInterrupt typing.
continueFunctionContinue unfinished typing.
restartFunctionRestart typing.
destroyFunctionActively destroy typewriter component.
renderedContentStringGet the rendered content of the typewriter component.
isTypingBooleanWhether currently typing.
progressNumberTyping progress, range 0 - 100.

Features โ€‹

  1. Markdown Support: Supports rendering Markdown format text and applies GitHub-style styling.
  2. Dynamic Typing Effect: Can simulate typewriter effect, gradually displaying text content.
  3. Code Highlighting: Built-in Prism.js, supports syntax highlighting for code blocks.
  4. XSS Security: Uses DOMPurify to filter HTML content, preventing XSS attacks.
  5. Flexible Configuration: Supports custom typing speed, cursor character, suffix, and other parameters.
  6. Customizable Development: Supports customized development based on component typing state.