All Projects → krakenjs → Jsx Pragmatic

krakenjs / Jsx Pragmatic

Licence: apache-2.0
Build JSX structures, then decide at runtime which pragma you want to use to render them.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jsx Pragmatic

Azure Sdk For Python
This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
Stars: ✭ 2,321 (+1378.34%)
Mutual labels:  hacktoberfest
K6
A modern load testing tool, using Go and JavaScript - https://k6.io
Stars: ✭ 14,829 (+9345.22%)
Mutual labels:  hacktoberfest
Products.cmfplone
The core of the Plone content management system
Stars: ✭ 157 (+0%)
Mutual labels:  hacktoberfest
Home Assistant.io
📘 Home Assistant User documentation
Stars: ✭ 2,377 (+1414.01%)
Mutual labels:  hacktoberfest
C Plus Plus
Collection of various algorithms in mathematics, machine learning, computer science and physics implemented in C++ for educational purposes.
Stars: ✭ 17,151 (+10824.2%)
Mutual labels:  hacktoberfest
Playframework
Play Framework
Stars: ✭ 12,041 (+7569.43%)
Mutual labels:  hacktoberfest
Openlinkwith
Open the current webpage you have in another app. Magic! 🔮
Stars: ✭ 158 (+0.64%)
Mutual labels:  hacktoberfest
Photos
📸 Your memories under your control
Stars: ✭ 157 (+0%)
Mutual labels:  hacktoberfest
Nginxconfig.io
⚙️ NGINX config generator on steroids 💉
Stars: ✭ 14,983 (+9443.31%)
Mutual labels:  hacktoberfest
Syliusresourcebundle
Simpler CRUD for Symfony applications
Stars: ✭ 156 (-0.64%)
Mutual labels:  hacktoberfest
Enqueue Dev
Message Queue, Job Queue, Broadcasting, WebSockets packages for PHP, Symfony, Laravel, Magento. DEVELOPMENT REPOSITORY - provided by Forma-Pro
Stars: ✭ 1,977 (+1159.24%)
Mutual labels:  hacktoberfest
Akka
Build highly concurrent, distributed, and resilient message-driven applications on the JVM
Stars: ✭ 11,938 (+7503.82%)
Mutual labels:  hacktoberfest
Brain.js
brain.js is a GPU accelerated library for Neural Networks written in JavaScript.
Stars: ✭ 12,358 (+7771.34%)
Mutual labels:  hacktoberfest
Rich Markdown Editor
The open source React and Prosemirror based markdown editor that powers Outline. Want to try it out? Create an account:
Stars: ✭ 2,468 (+1471.97%)
Mutual labels:  hacktoberfest
Eleventy Garden
🌱 A starter site for building a mind garden with eleventy
Stars: ✭ 157 (+0%)
Mutual labels:  hacktoberfest
Pulley
A library to imitate the iOS 10 Maps UI.
Stars: ✭ 1,928 (+1128.03%)
Mutual labels:  hacktoberfest
Nodebb
Node.js based forum software built for the modern web
Stars: ✭ 12,303 (+7736.31%)
Mutual labels:  hacktoberfest
Go Appimage
Go implementation of AppImage tools. Still experimental
Stars: ✭ 156 (-0.64%)
Mutual labels:  hacktoberfest
Resources
Resources on various topics being worked on at IvLabs
Stars: ✭ 158 (+0.64%)
Mutual labels:  hacktoberfest
Documentation
Pantheon Docs
Stars: ✭ 157 (+0%)
Mutual labels:  hacktoberfest

JSX Pragmatic

  • Build JSX templates
  • Decide at runtime how you want to render them
  • Easily build custom renderers - render to HTML, DOM, or anything else!

Because JSX is pretty useful, even without React!

Build an abstract jsx component

First we'll build a small component. We're not tying ourselves to any particular framework yet, or any render target.

/* @jsx node */

import { node } from 'jsx-pragmatic';

function Login({ prefilledEmail }) {
  return (
    <section>
      <input type="text" placeholder="email" value={prefilledEmail} />
      <input type="password" placeholder="password" />
      <button>Log In</button>
    </section>
  );
}

Render on the server

Let's say we're on the server-side, and we want to render the jsx to html to serve to a client. Just pass html() to the renderer:

/* @jsx node */

import { node, html } from 'jsx-pragmatic';
import { Login } from './components'

function render() {
  return (
    <Login prefilledEmail='[email protected]' />
  ).render(html());
}

Render on the client

Now let's render the same jsx template on the client-side, directly to a DOM element:

/* @jsx node */

import { node, dom } from 'jsx-pragmatic';
import { Login } from './components'

function render() {
  return (
    <Login prefilledEmail='[email protected]' />
  ).render(dom());
}

Render in a React app

Or if we're using the same component in React, we can render it as a React component:

/* @jsx node */

import { node, react } from 'jsx-pragmatic';
import { Login } from './components'

function render() {
  return (
    <Login prefilledEmail='[email protected]' />
  ).render(react({ React }));
}

Render in a Preact app

Or if we're using the same component in Preact, we can render it as a Preact component:

/* @jsx node */

import { node, preact } from 'jsx-pragmatic';
import { Login } from './components'

function render() {
  return (
    <Login prefilledEmail='[email protected]' />
  ).render(preact({ Preact }));
}

Write your own renderer

Renderers are just functions!

  • Write a factory like customDom. This will take some options and return our renderer.
  • Return a renderer which takes name, props and children and renders them in whatever way you want!

This example renders the jsx directly to DOM elements:

/* @jsx node */

import { node, NODE_TYPE } from 'jsx-pragmatic';
import { Login } from './components'

function customDom({ removeScriptTags } = { removeScriptTags: false }) {

  let domRenderer = (node) => {
    if (node.type === NODE_TYPE.COMPONENT) {
      return node.renderComponent(domRenderer);
    }

    if (node.type === NODE_TYPE.TEXT) {
      return document.createTextNode(node.text);
    }

    if (node.type === NODE_TYPE.ELEMENT) {
      if (removeScriptTags && node.name === 'script') {
        return;
      }

      let el = document.createElement(node.name);

      for (let [ key, val ] of Object.entries(node.props)) {
        el.setAttribute(key, val);
      }

      for (let child of node.children) {
        el.appendChild(child.render(domRenderer));
      }

      return el;
    }
  }

  return domRenderer;
}

Then when you're ready to use your renderer, just pass it into .render() and pass any options you want to use to configure the renderer.

function render() {
  return (
    <Login prefilledEmail='[email protected]' />
  ).render(customDom({ removeScriptTags: true }));
}

Use Fragments

You can either import Fragment from jsx-pragmatic:

/* @jsx node */

import { node, Fragment } from 'jsx-pragmatic';

function Login({ prefilledEmail }) {
  return (
    <Fragment>
      <input type="text" placeholder="email" value={prefilledEmail} />
      <input type="password" placeholder="password" />
      <button>Log In</button>
    </Fragment>
  );
}

Or use the @jsxFrag comment, and the new <> </> syntax for Fragments, providing you're using Babel 7:

/* @jsx node */
/* @jsxFrag Fragment */

import { node, Fragment } from 'jsx-pragmatic';

function Login({ prefilledEmail }) {
  return (
    <>
      <input type="text" placeholder="email" value={prefilledEmail} />
      <input type="password" placeholder="password" />
      <button>Log In</button>
    </>
  );
}

Why?

JSX is a neat way of parsing and compiling templates to vanilla javascript. Right now most people use JSX with React. But in reality, the technology is decoupled enough from React that it can be used to render anything:

  • HTML
  • XML
  • DOM Nodes

This library helps you do that.

Can't you do that with Babel?

Yep, Babel provides a neat pragma option which lets you choose what your jsx is compiled to; if you don't want to use React.createElement, you can write your own pragma to convert the jsx to anything else.

The only problem with that is, the decision of which pragma to use is made entirely at build-time. Let's say you have a template which needs to be:

  • Rendered as an html string on the server side.
  • Rendered directly as a DOM element in some client environments.
  • Rendered as a React component in other client environments.

jsx-pragmatic helps you achieve that by allowing you decide when you render what your jsx should be transformed into.

It also abstracts away some of the stuff in jsx that's a little tricky to deal with; like nested children arrays, dealing with basic element vs function components, and fragments -- leaving you to focus on the renderer logic.

Quick Start

Install

npm install --save jsx-pragmatic

Getting Started

  • Fork the module
  • Run setup: npm run setup
  • Start editing code in ./src and writing tests in ./tests
  • npm run build

Building

npm run build

Tests

  • Edit tests in ./test/tests

  • Run the tests:

    npm run test
    

Testing with different/multiple browsers

npm run karma -- --browser=PhantomJS
npm run karma -- --browser=Chrome
npm run karma -- --browser=Safari
npm run karma -- --browser=Firefox
npm run karma -- --browser=PhantomJS,Chrome,Safari,Firefox

Keeping the browser open after tests

npm run karma -- --browser=Chrome --keep-open

Publishing

Before you publish for the first time:
  • Delete the example code in ./src, ./test/tests and ./demo
  • Edit the module name in package.json
  • Edit README.md and CONTRIBUTING.md
Then:
  • Publish your code: npm run release to add a patch
    • Or npm run release:path, npm run release:minor, npm run release:major
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].