On this page

RichText

Requires:
@ray-js/ray≥0.5.10

Description

Rich text component for rendering rich text content.

Props

PropertyTypeRequiredDefaultDescription
No-Node list (array of element/text nodes) or an HTML string
No-Triggered when a tag inside the rich text is clicked

Examples

Basic usage

import React from 'react';
import { View, RichText } from '@ray-js/ray';

export default function Demo() {
  return (
    <View style={{ padding: 20 }}>
      <RichText
        nodes='<div><h2 style="color: #333;">Title</h2><p style="color: #666; line-height: 1.6;">This is a piece of rich text content</p></div>'
      />
    </View>
  );
}

HTML String format

import React from 'react';
import { View, RichText } from '@ray-js/ray';

export default function Demo() {
  const htmlString = `
    <div style="padding: 10px;">
      <h1 style="color: #007aff; margin-bottom: 10px;">Welcome to RichText</h1>
      <p style="color: #333; line-height: 1.8;">
        Life is <i>like</i> a box of <b style="color: red;">chocolates</b>.
      </p>
      <p style="color: #666; margin-top: 10px;">
        You never know what you are gonna get.
      </p>
    </div>
  `;

  return (
    <View style={{ padding: 20 }}>
      <RichText nodes={htmlString} />
    </View>
  );
}

Text node list format

import React from 'react';
import { View, RichText } from '@ray-js/ray';

export default function Demo() {
  const nodes = [
    { type: 'text' as const, text: 'This is the first paragraph.' },
    { type: 'text' as const, text: 'This is the second paragraph, supports HTML entities such as &amp;.' },
  ];

  return (
    <View style={{ padding: 20 }}>
      <RichText nodes={nodes} />
    </View>
  );
}

Tag click event

import React from 'react';
import { View, RichText } from '@ray-js/ray';

export default function Demo() {
  const html = `
    <div style="padding: 10px;">
      <p>Click the link text below to see console output:</p>
      <a style="color: #007aff; text-decoration: underline;">Clickable link</a>
    </div>
  `;

  return (
    <View style={{ padding: 20 }}>
      <RichText
        nodes={html}
        onSelect={(e) => console.log('Tag clicked:', e.detail.name, e.detail.attrs)}
      />
    </View>
  );
}

Trusted HTML Nodes and Attributes

class and style attributes are globally supported; id attribute is not supported.

NodeAttributes
a
abbr
address
article
aside
b
bdi
bdodir
big
blockquote
br
caption
center
cite
code
colspan, width
colgroupspan, width
dd
del
div
dl
dt
em
fieldset
font
footer
h1
h2
h3
h4
h5
h6
header
hr
i
imgalt, src, height, width
ins
label
legend
li
mark
nav
olstart, type
p
pre
q
rt
ruby
s
section
small
span
strong
sub
sup
tablewidth
tbody
tdcolspan, height, rowspan, width
tfoot
thcolspan, height, rowspan, width
thead
trcolspan, height, rowspan, width
tt
u
ul
iframe
pre