npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ts-delight/fluent-react.macro

v2.0.1

Published

Zero-overhead fluent type-safe alternative to JSX and React.createElement

Downloads

12

Readme

About

Fluent, Terse and Type-safe alternative to JSX for React.

Examples

import R from "@ts-delight/fluent-react.macro";

// Your component:
const Container = () =>

  // Helpers are exposed from top level for all DOM & SVG tags:
  // |
  // V
  R.div().id("name")()
      //  ^         ^
      //  |         |_ | Complete the chain by invoking the last
      //  |            | part of the chain as a function
      //  |
      //  `- Use setter functions to specify props

  // Equivalent to:
  // <div id="name" />

const UserAvatar = (props: {user: User}) =>

  //  _ For custom components just call R as a function:
  // |
  // |        __ Use similar setter functions for custom component specific props
  // |       |       (Your IDE will auto-complete them for you)
  // V       V
  R(Popover).containerId(user.id)(
    // As a shorthand children can be passed through the terminating function call
    R.div()
      .className("popover-inner-container")
      .children(user.name)(),     // <------------|  Of course you can also
    R.span().className("arrow")() //              |  use children as a prop
                          //   ^
                          //   |___ For nested components this tailing invocation is
                          //        optional
  );

  // So the above can be more tersely written as:
  R(Popover).containerId(user.id)(
    R.div().className("popover-inner-container")(user.name),
    R.span().className("arrow")
  )

  // Equivalent to:
  // <Popover containerId={user.id}>
  //     <div className="popover-inner-container">
  //         {user.name}
  //     </div>
  // </Popover>

  // Dynamic attributes can be passed to the factory functions directly:
  //
  // So the above can be written as:
  R(Popover, {containerId: user.id})(
    R.div({className: "popover-inner-container"})(user.name),
    R.span({className: "arrow"})()
  )

In above snippet, when we say "equivalent", we don't just mean conceptually equivalent. During build, the fluent API is compiled down to the same React.createElement invocations as the JSX syntax so there is no additional overhead of using this.

See more examples.

Why ?

We wanted an XML-free typescript-friendly alternative to JSX that didn't bring in any additional overhead.

We found that using createElement directly results in code that is not quite as readable. Also when using createElement directly, the __source: {fileName, lineNumber} prop auto-injected by the babel transformer is not present which makes debugging a bit harder.

Installation

This utility is implemented as a babel-macro.

Refer babel's setup instructions to learn how to setup your project to use babel for compilation.

  1. Install babel-plugin-macros and @ts-delight/fluent-react.macro:
npm install --save-dev babel-plugin-macros @ts-delight/fluent-react.macro
  1. Add babel-plugin-macros to .babelrc (if not already preset):
// .babelrc

module.exports = {
  presets: [
    // ... other presets
  ],
  plugins: [
    "babel-plugin-macros" // <-- REQUIRED
    // ... other plugins
  ]
};
  1. Import @ts-delight/fluent-react.macro in your code:
// src/foo.js

import R from "@ts-delight/fluent-react.macro";

const Hello = () =>
  R("div")
    .children("hello")
    .end();

Usage with TypeScript

This library is type-safe and comes with type definitions.

All code must be processed through babel. Compilation through tsc (only) is not supported.

Recommended babel configuration:

// .babelrc

module.exports = {
  presets: [
    "@babel/preset-typescript"
    // ... other presets
  ],
  plugins: [
    "babel-plugin-macros"
    // ... other plugins
  ]
};

Caveats

  1. Every fluent builder chain must end with an .end invocation without interruptions.

For example:

const intermediate = R.div().id("container");
const result = intermediate.className("large")();

Above code will fail to compile.

Because the entire fluent chain is compiled away, anything returned by R, or the prop setters, can not be assigned, referenced, or used in any computation.

  1. This plugins currently assumes that files are ES6 modules. At the moment, this is not configurable.

You may also like:

  • if-expr.macro: Type-safe expression-oriented alternative to Javascript if statements
  • switch-expr.macro: Type-safe expression-oriented alternative to Javascript switch statements

Alternatives:

  1. react-hyperscript: Encourages some patterns which are not type-safe: eg. h('h1#heading').
  2. ijk: Terser, but also not type-safe.
  3. babel-plugin-transform-react-pug: Nice indented syntax, also not type-safe.

License

MIT