Enable JSX for Template Literal Tags based projects.

Overview

JSX2TAG

Build Status Coverage Status

Social Media Photo by Andre Taissin on Unsplash

Enable JSX for Template Literal Tags based projects.

Features

  • a createPragma(tag, config?) utility to have a React.createElement like function to use as pragma
  • a bind utility to mimic .prop=${value} in the template
  • automatic onEventName to @eventName conversion
  • automatic ?prop=${value} conversion in the template, when the property is boolean
  • optionally boost performance via @ungap/plugin-transform-static-jsx, able to create best template literals tags' arguments

Example

See test/index.jsx to see all features applied.

/** @jsx h *//** @jsxFrag h */

// your template literal library of choice
const {render, html} = require('uhtml-ssr');

// this module utils
const {bind, createPragma} = require('jsx2tag');

// create your `h` / pragma function
const h = createPragma(html);
// if your env works already with `React.createElement`, use:
// const React = {createElement: createPragma(html)};

// any component (passed as template value)
const Bold = ({children}) => html`<strong>${children}</strong>`;

// any generic value
const test = 123;

// test it!
const myDocument = (
  <p class="what" test={bind(test)} onClick={console.log}>
    <Bold>Hello</Bold>, <input type="password" disabled={false} />
    <span id="greetings">Hello</span>
  </p>
);

render(String, myDocument);
// <p class="what" test="123"><strong>Hello</strong>, <input type="password"><span id="greetings">Hello</span></p>

How To Transpile JSX

Specify pragma and pragmaFrag or use this syntax on top:

/** @jsx h */
/** @jsxFrag h */

Otherwise, follow @Robbb_J post about minimal requirements and you'll be good.

A huge thanks to him for writing such simple, step by step, guide.

How to render keyed components

The config object accepts a keyed(tagName, props) callback that can return a keyed version of the component.

/** @jsx h *//** @jsxFrag h */
import {createPragma} from '//unpkg.com/jsx2tag?module';
import {render, html} from '//unpkg.com/uhtml?module';

// used as weakMap key for global keyed references
const refs = {};
const h = createPragma(html, {
  // invoked when a key={value} is found in the node
  // to render regular elements (or µbe classes)
  keyed(tagName, {key}) {
    const ref = refs[tagName] || (refs[tagName] = {});
    return html.for(ref, key);
  }
});

render(document.body, <div key={'unique-id'} />);

Alternatively, each library might have its own way, but the gist of this feature, whenever available, is that the key property is all we're after:

/** @jsx h *//** @jsxFrag h */

import {createPragma} from '//unpkg.com/jsx2tag?module';
import {render, html} from '//unpkg.com/uhtml?module';

const h = createPragma(html);

const App = ({name, key}) => html.for(App, key)`Hello ${name} 👋`;

render(document.body, <App name="JSX" key={'specific-key'} />);

Conditional keyed components are also possible.

Here another uhtml example:

/** @jsx h *//** @jsxFrag h */

import {createPragma} from '//unpkg.com/jsx2tag?module';
import {render, html} from '//unpkg.com/uhtml?module';

const h = createPragma(html);

const App = ({name, key}) => {
  const tag = key ? html.for(App, key) : html;
  return tag`Hello ${name} 👋`;
};

render(document.body, <App name="JSX" key={'specific-key'} />);

In few words, there's literally nothing stopping template literal tags libraries to be keyed compatible.

You might also like...

Yet Another JSX using tagged template

Yet Another JSX using tagged template

우아한 JSX Yet Another Simple JSX using tagged template 언어의 한계가 곧 세계의 한계다 - Ludwig Wittgenstein 우아한 JSX가 캠퍼들의 표현의 자유를 넓히고 세계를 넓히는데 도움이 되었으면 합니다 Example i

Sep 22, 2022

Admin dashboard template built with tailwindcss & vue-jsx.

K UI Admin Dashboard Template (Vue 3 & JSX) ⚠️ This template is not finished yet and still in design phase. We are building it in public. Live preview

Aug 14, 2022

Web Components engine based on JSX & TypeScript

Web Components engine based on JSX & TypeScript

WebCell Web Components engine based on JSX & TypeScript Usage Demo & GitHub template: https://web-cell.dev/scaffold/ Project bootstrap Command npm ini

Nov 17, 2022

jQuery tags input plugin based on Twitter Bootstrap.

Bootstrap Tags Input Bootstrap Tags Input is a jQuery plugin providing a Twitter Bootstrap user interface for managing tags. Current stable version: v

Dec 21, 2022

Dynamic content filtering in org-mode exported HTML documents based on tags.

Dynamic content filtering in org-mode exported HTML documents based on tags.

Dynamic tag filtering in org-mode exported HTML documents This JavaScript code adds dynamic tag filtering to HTML documents exported from Emacs Org-mo

Sep 6, 2022

A responsive HTML template for coding projects with a clean, user friendly design. Crafted with the latest web technologies, the template is suitable for landing pages and documentations.

A responsive HTML template for coding projects with a clean, user friendly design. Crafted with the latest web technologies, the template is suitable for landing pages and documentations.

Scribbler - a responsive HTML template for coding projects and documentations Scribbler is a responsive HTML/CSS/Javascript template designed for deve

Jan 1, 2023

Write JSX-driven components with functions, promises and generators.

Crank.js Write JSX-driven components with functions, promises and generators. Documentation is available at crank.js.org. Crank.js is in a beta phase,

Jan 1, 2023

Full CSS support for JSX without compromises

Full CSS support for JSX without compromises

styled-jsx Need help? Full, scoped and component-friendly CSS support for JSX (rendered on the server or the client). Code and docs are for v3 which w

Jan 4, 2023

High performance JSX web views for S.js applications

High performance JSX web views for S.js applications

Surplus const name = S.data("world"), view = h1Hello {name()}!/h1; document.body.appendChild(view); Surplus is a compiler and runtime to all

Dec 30, 2022

Like JSX, but native and fast

Like JSX, but native and fast

esx High throughput React Server Side Rendering For a simplified example of esx in action, check out esx-demo. esx is designed to be a high speed SSR

Jan 2, 2023

🍼 650B Virtual DOM - Use reactive JSX with minimal overhead

🍼 little-vdom Forked from developit's little-vdom gist. npm: npm i @luwes/little-vdom cdn: unpkg.com/@luwes/little-vdom 650B Virtual DOM Components S

Nov 12, 2022

Video conference layout with tailwindcss & vuejs jsx

Video conference layout with tailwindcss & vuejs jsx

Nov 22, 2022

✏️ Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped

✏️ Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped

Sugar High Introduction Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped Usage npm install --save sugar-high import { h

Dec 8, 2022

Create a html response with JSX.

Htm Create a text/html response with JSX, powered by Preact and UnoCSS. Usage To use Htm, create a server.tsx file like this: /** @jsx h */ import {

Dec 9, 2022

Converts JSX to HTML strings at compile time.

unplugin-jsx-string Converts JSX to HTML strings at compile time. Installation npm i unplugin-jsx-string Vite // vite.config.ts import JsxString from

Sep 3, 2022

Add the `className` to all JSX elements in your project

Babel Plugin Transform JSX Classes This babel plugin adds the className in each JSX element. Before After function FancyComponent () { return (

Jul 15, 2022

Unofficial Library for using Discord.JS with JSX Syntax.

@chooks22/discord.jsx WARNING! This project is in very early stage of development. And while I do have plans to finish it to completion, consider this

Nov 14, 2022

Use jsx to make scriptable's layout.

scriptable-jsx This project helps you to write Scriptable widgets with JSX syntax. And add some useful tools by the way. you can check demos in demo f

Oct 10, 2022

Minimalist Virtual DOM library with JSX and factory pattern for stateful components.

Minimalist Virtual DOM library with JSX and factory pattern for stateful components.

Reflex Reflex JS is a tiny virtual-dom library with factory based functional components. See this example running See more complex demos Table of cont

Sep 2, 2022
Comments
  • Improved signature with xml, cache, and attribute

    Improved signature with xml, cache, and attribute

    This MR is about improving this module, making it more portable/versatile, and more feature reach in general.

    It won't be merged until README is updated, the signature is typed, and coveralls is 100% happy.

    opened by WebReflection 0
  • Optimize hoisted static content

    Optimize hoisted static content

    Based on this tweet, and its code example, "truly constant elements" are passed right away as children.

    uhtml and friends have this Hole class concept, that would make it kinda easier to aggregate children as arrray, but there's no way to pass this check around and help other cases too, and currently, the logic doesn't aggregate "holes" in an efficient way, as empty text nodes are inserted in between.

    This might not even be a real-world performance issue, but it's something to think about, either here or in the specialized transformer, if even possible with ease.

    P.S. right now everything looks fine and working as expected

    enhancement 
    opened by WebReflection 0
Owner
Andrea Giammarchi
Web, Mobile, IoT and all Web & JS things since 00's
Andrea Giammarchi
The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

Bootstrap Sleek, intuitive, and powerful front-end framework for faster and easier web development. Explore Bootstrap docs » Report bug · Request feat

Bootstrap 161.1k Jan 4, 2023
Simple TypeScript fastify template.

Simple fastify template. Instructions: 1.) Clone the repo 2.) Type npm i to install all dependencies 3.) Type npm run dev to make it autorecompile on

Nick 2 Mar 17, 2022
Simple and elegant component-based UI library

Simple and elegant component-based UI library Custom components • Concise syntax • Simple API • Tiny Size Riot brings custom components to all modern

Riot.js 14.7k Jan 4, 2023
Semantic is a UI component framework based around useful principles from natural language.

Semantic UI Semantic is a UI framework designed for theming. Key Features 50+ UI elements 3000 + CSS variables 3 Levels of variable inheritance (simil

Semantic Org 50.3k Jan 3, 2023
Materialize, a CSS Framework based on Material Design

MaterializeCSS Materialize, a CSS Framework based on material design. -- Browse the docs -- Table of Contents Quickstart Documentation Supported Brows

Alvin Wang 38.8k Dec 28, 2022
A responsive JS that supports literal expression of HTML

WEBX 支持 View Model 混合书写 支持字面上声明模型绑定 其实这是个响应式的 JS ,只是额外支持了 HTML 的字面表达 和一般的 MV* 框架实现方式不太一样,WEBX 通过编译时语义分析转化将更改的响应最小化(不需要在运行时执行庞大的 VDOM 比对),更新效率大概是 VUE 的

null 21 Apr 26, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
A typescript transform that converts exported const enums into object literal.

ts-transformer-optimize-const-enum A typescript transformer that convert exported const enum into object literal. This is just like the one from @babe

Fonger 22 Jul 27, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023