reactive-cards
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

reactive-cards

Renders JSX components to Adaptive Cards JSON on the server and in the browser.

Usage

This module provides two functions; one to create elements and one to render them. If you've worked with React or React-like libraries before then they're the equivalent to React.createElement() and ReactDOM.renderToString().

The example below shows how to render a simple component using reactive-cards:

import { h, render } from 'reactive-cards'

const Welcome = () =>
  h(
    'card',
    { type: 'AdaptiveCards' },
    h('h1', null, 'Hello World!'),
    h('p', null, 'This component was rendered with ReactiveCards')
  )

render(Welcome())

Although you can use reactive-cards without a compilation step, we'd recommend using JSX to more succinctly describe your markup. Here is the same component as before but rewritten to use JSX syntax:

import { h, render } from 'reactive-cards'

const Welcome = () => (
  <div class="welcome-banner">
    <h1>ReactiveCards</h1>
    <p>This component was rendered with ReactiveCards</p>
  </div>
)

render(<Welcome />)

API

ReactiveCards.h() / ReactiveCards.createElement()

ReactiveCards.h(type[, props][, ...children])

Returns an element with the given props. It accepts the following arguments:

  • type The type of element to create which can be the name of an HTML element (such as "div"), a component, or a fragment.
  • props An object containing data to pass to a component or HTML attributes to render. See the props documentation for more information.
  • ...children Any number of child elements which may be simple values or other elements. See the children documentation for more information.

ReactiveCards.render() / ReactiveCards.renderToObject()

ReactiveCards.render(element)

Returns the rendered element as a string. It accepts the following arguments:

  • element An element created with ReactiveCards.h()

Functional Components

All components can be defined as functions. With the introduction of the hooks API, Components are no longer needed

ReactiveCards.Fragment

A Fragment is a special component which enables multiple elements to be rendered without a wrapper. See the using fragments documentation for more information.

<dl>
  {props.definitions.map(item => (
    <ReactiveCards.Fragment>
      <dt>{item.title}</dt>
      <dd>{item.description}</dd>
    </ReactiveCards.Fragment>
  ))}
</dl>

Syntax

Components

Components are reusable pieces of UI which can be composed in useful ways. There are two types of components supported by ReactiveCards:

  • Class components, which are ES6 classes extending ReactiveCards.Component and have a render() method which returns elements.
  • Functional components which are functions that accept props and return elements.

Here is an example showing the same component written using a class and as a function:

// Class component
class SubmitButton extends ReactiveCards.Component {
  render() {
    return <button type="submit">{this.props.text}</button>
  }
}

// Functional component
const SubmitButton = props => <button type="submit">{props.text}</button>

When using React or React-like libraries class components are usually used to add extra functionality such as hooking into lifecycle methods and maintain state. ReactiveCards renders static HTML so there is no state nor lifecycle methods.

Props

Props are objects either containing data to share with components or HTML attributes for a HTML element. A component should never modify the props it receives.

// Pass data to a component as props
h(SubmitButton, { text: 'Submit' })

// Render props as HTML attributes
h('button', { type: 'submit' })

Default prop values can be defined on component or functional components by adding a defaultProps property. These will be combined with any props received by the component:

// Class component
class SubmitButton extends Component {
  // ...

  static get defaultProps() {
    return {
      text: 'Submit'
    }
  }
}

// Functional component
const SubmitButton = props => {
  // ...
}

SubmitButton.defaultProps = {
  text: 'Submit'
}

Card Attributes

When props are used to render attributes some property names and values will be treated differently by ReactiveCards:

  • Because class and for are reserved words in JavaScript you may use the aliases className and htmlFor instead.

  • Boolean attributes, such as hidden or checked, will only be rendered if assigned a truthy value.

  • Enumerated attributes which accept the values "true" or "false", such as contenteditable, will be rendered with their assigned value.

  • Any attributes requiring hyphens, such as aria-* and data-* should be written with hyphens.

  • Framework specific props such as key and ref will not be rendered.

Styles

The style attribute accepts a JavaScript object containing CSS properties and values.

CSS Properties may be written in camelCase for consistency with accessing the properties with JavaScript in the browser (e.g. element.style.marginBottom). Vendor prefixes other than ms should always begin with a capital letter (e.g. WebkitHyphens).

ReactiveCards will automatically append a px suffix to number values but certain properties will remain unitless (e.g. z-index and order). If you want to use units other than px, you should specify the value as a string with the desired unit. For example:

// Input:
const styles = {
  display: 'flex',
  order: 2,
  width: '50%',
  marginBottom: 20,
  WebkitHyphens: 'auto',
}

<div style={styles}></div>

// Output:
<div style="display:flex;order:2;width:50%;margin-bottom:20px;-webkit-hyphens:auto;></div>

HTML entities

ReactiveCards will escape all string values so if you need to output a HTML entity you can run into issues with double escaping. The simplest way to work-around this issue is to write the unicode character directly in your code (and use UTF-8 encoding for you source files). Otherwise, you can find the unicode number for the required character. For example:

// Incorrect. Outputs: <h1>Mac &amp;amp; Cheese</h1>
<h1>Mac &amp; Cheese</h1>
// Correct. Outputs: <h1>Mac &amp; Cheese</h1>
<h1>Mac & Cheese</h1>
// Correct. Outputs: <h1>Mac &amp; Cheese</h1>
<h1>{`Mac ${String.fromCharCode(38)} Cheese`}</h1>

Inner HTML

ReactiveCards supports the dangerouslySetInnerJSON property to inject unescaped HTML code. This is potentially dangerous and should never be used around any user input, but it can be useful as a last resort.

const json = { __json: '<i>Mac &amp; Cheese</i>' }
<body dangerouslySetInnerJSON={json}></body> // Outputs: <body><i>Mac &amp; Cheese</i></body>

Children

Components can render any number of child elements. Children can be strings, numbers, or other components. Components will receive references to any children via a children prop which enables components to be composed in useful ways.

const Wrapper = props => <p>{props.children}</p>
const html = <Wrapper>Hello</Wrapper> // Outputs: <p>Hello</p>

Please note that child elements will not be rendered for void elements.

Fragments

In React and React-like frameworks components must always return a single enclosing element. But sometimes it is required to return a list of elements, either because you don't need the extra elements or the extra elements would create invalid HTML output. For example, when rendering a description list the title and detail (<dt> and <dd>) elements are usually grouped in pairs:

function DescriptionList(props) {
  return (
    <dl>
      {props.definitions.map((item) => (
        <dt>{item.title}</dt>
        <dd>{item.description}</dd>
      ))}
    </dl>
  )
}

However, several tools will throw an error when evaluating the code above because the title and description elements are not wrapped in an enclosing element but wrapping them with an element would result in invalid HTML.

To solve this React 16.2 introduced the concept of fragments which enable a list of elements to be wrapped with an enclosing element without rendering anything extra. To use fragments in your JSX code ReactiveCards provides a special Fragment component:

function DescriptionList(props) {
  return (
    <dl>
      {props.definitions.map(item => (
        <ReactiveCards.Fragment>
          <dt>{item.title}</dt>
          <dd>{item.description}</dd>
        </ReactiveCards.Fragment>
      ))}
    </dl>
  )
}

JSX

Not familiar with JSX? Check out WTF is JSX and JSX in Depth first.

If you're authoring your components with JSX syntax you will need to transpile your code into plain JavaScript in order to run them. Depending on the toolchain you're using there will be different plugins available. Some popular tools to transpile JavaScript are Typescript, Babel (with the JSX plugin), Bublé (with JSX enabled) and Sucrase.

Prior art

This module was forked from and inspired by the hyperons package and also borrows from a few other JSX to string implementations:

  • hyperons i-like-robots / hyperons
  • vhtml Virtual dom by the developer of preact
  • [Hyperaon] (style stringification)
  • Hyperapp Render (style stringification)
  • React DOM (boolean attributes)
  • Rax (performance improvements)

License

ReactiveCards is MIT licensed.

Package Sidebar

Install

npm i reactive-cards

Weekly Downloads

8

Version

1.2.0

License

MIT

Unpacked Size

117 kB

Total Files

47

Last publish

Collaborators

  • guycreate