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

teact

v1.3.0

Published

Generate React elements with CoffeeScript functions

Downloads

19

Readme

Teact

It's better than cjsx.

Build React element trees by composing functions.
You get full javascript control flow, and minimal boilerplate. It's also quite simple, just a thin wrapper around React.createElement like JSX, making it fast and lightweight (2KB gzipped).

Build Status NPM version

Usage

{crel} = require 'teact'

crel 'div', '#root.container', ->
  unless props.signedIn
    crel 'button', onClick: handleOnClick, 'Sign In'
  crel.text 'Welcome!'

Transforms into:

React.createElement('div',
  {id: root, className: 'container'}, [
    (props.signedIn ? React.createElement('button',
      {onClick: handleOnClick}, 'Sign In'
    ) : null)
    'Welcome!'
  ]
)

Use it from your component's render method:

{Component} = require 'react'
{crel} = require 'teact'

class Widget extends Component
  render: ->
    crel 'div', className: 'foo', =>
      crel 'div', 'bar'

Or in a stateless component:

module.exports = (props) ->
  crel 'div', className: 'foo', ->
    crel 'div', props.bar

Nesting Components

crel is just a thin wrapper around React.createElement, so you can pass it components instead of crel names:

class DooDad extends Component
  render: ->
    crel 'div', className: 'doodad', =>
      crel 'span', @props.children

class Widget extends Component
  handleFiddle: =>
    # ...

  render: ->
    crel 'div', className: 'foo', =>
      crel DooDad, onFiddled: @handleFiddle, =>
        crel 'div', "I'm passed to DooDad.props.children"

If you need to build up a tree of elements inside a component's render method, you can escape the element stack via the pureComponent helper:

{crel, pureComponent} = require 'teact'

Teas = pureComponent (teas) ->
  teas.map (tea) ->
    # Without pureComponent, this would add teas to the element tree
    # in iteration order.  With pureComponent, we just return the reversed list
    # of divs without adding the element tree.  The caller may choose to add
    # the returned list.
    crel 'div', tea
  .reverse()

class Widget extends Component
  render: ->
    crel 'div', Teas(@props.teas)

Sugar Syntax

Teact exports bound functions for elements, giving you options for terser syntax if you're into that:

T = require 'teact'

T.div className: 'foo', ->
  T.text 'Blah!'

or the Teacup / CoffeeCup signatures:

{div, text} = require 'teact'

div '.foo', ->
  text 'Blah!'

Performance

A super-basic performance test suggests that teact has no discernible impact on React rendering performance:

$ npm run benchmark

> native x 5,197 ops/sec ±3.30% (76 runs sampled)
> teact x 5,339 ops/sec ±2.23% (82 runs sampled)
> Fastest is teact,native

It's also lightweight, at 5KB minified, 2KB gzipped.

How is this better than CJSX?

  • Familiar control flow with branching and loops. See examples above.

  • No transpiler to maintain.

  • No extraneous enclosing tags required:

    renderHeader: ->
      unless @props.signedIn
        crel 'a', href: '...', 'Sign in'
      crel 'h1', 'Tea Shop'

    Just works.

  • Nice syntax errors.

  • Half the lines of code. Those closing tags really add up.

Other folks have reached similar conclusions. They were later bit by using the React API directly when the implementation changed. A thin wrapper like Teact should minimize this risk.

Legacy

Markaby begat CoffeeKup begat CoffeeCup and DryKup which begat Teacup which begat Teact.

Contributing

$ git clone https://github.com/hurrymaplad/teact && cd teact
$ npm install
$ npm test