MML for React

0.4.7 · active · verified Tue Apr 21

MML React is a library from GetStream for rendering Message Markup Language (MML) content within React applications. It provides a foundational `<MML />` component that parses MML strings and translates them into corresponding React components for display. The library is currently stable at version `0.4.7` and has seen a series of maintenance and dependency updates, particularly around early 2022, though new feature releases appear less frequent. Key differentiators include its tight integration with the GetStream ecosystem for building rich chat UIs, extensive customization options for overriding default MML tag-to-React component mappings, and a flexible theming system using SCSS variables. It abstracts away the complexity of parsing and rendering structured message content, offering a component-based approach to build interactive and styled UI elements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to render dynamic MML content using the `<MML />` component. It includes an editable textarea to update the MML source live and showcases how to override a default MML tag (like 'button') with a custom React component using the `converters` prop.

import React, { useState } from 'react';
import { MML } from 'mml-react';

// Example of custom converter for the 'button' tag
const customButtonConverter = (tag: any, children: React.ReactNode) => {
  const { text, action } = tag.node.attributes;
  return (
    <button
      key={tag.key}
      onClick={() => {
        // In a real application, parse 'action' safely or use predefined callbacks
        if (action && action.startsWith('console.log')) {
          // eslint-disable-next-line no-eval
          eval(action); // DANGER: Evaluate untrusted input with caution
        } else if (action && action.startsWith('alert')) {
          // eslint-disable-next-line no-eval
          eval(action);
        }
      }}
      style={{
        backgroundColor: '#6200EE',
        color: 'white',
        padding: '8px 16px',
        border: 'none',
        borderRadius: '4px',
        cursor: 'pointer',
        margin: '5px 0',
        fontWeight: 'bold',
      }}
    >
      {text || children || 'Default Button'}
    </button>
  );
};

export default function MMLDemo() {
  const [mmlContent, setMmlContent] = useState<string>(`
    <mml>
      <card>
        <card-header>Welcome to MML React!</card-header>
        <card-body>
          <text>This is an example of <strong color="primary-accent">Message Markup Language</strong> rendered in React.</text>
          <image url="https://picsum.photos/seed/mml/300/150" alt="Random image" />
          <button text="Log to console" action="console.log('Button clicked from MML!')" />
          <button text="Show Alert" action="alert('Hello from MML!')" />
          <input type="text" placeholder="Enter something..." />
          <row>
            <col width="50%"><text>Column 1</text></col>
            <col width="50%"><text>Column 2</text></col>
          </row>
        </card-body>
      </card>
    </mml>
  `);

  const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    setMmlContent(e.target.value);
  };

  return (
    <div style={{ fontFamily: 'sans-serif', maxWidth: '600px', margin: '20px auto', padding: '20px', border: '1px solid #ddd', borderRadius: '8px' }}>
      <h1>MML React Live Demo</h1>
      <p>Edit the MML source below to see changes reflected instantly.</p>
      <textarea
        value={mmlContent}
        onChange={handleInputChange}
        rows={15}
        style={{ width: '100%', minHeight: '200px', marginBottom: '20px', padding: '10px', borderColor: '#ccc', borderRadius: '4px' }}
      />
      <h2>Rendered MML:</h2>
      <div style={{ border: '1px solid #eee', padding: '15px', borderRadius: '5px', backgroundColor: '#f9f9f9' }}>
        <MML source={mmlContent} converters={{ button: customButtonConverter }} />
      </div>
    </div>
  );
}

view raw JSON →