Coder Social home page Coder Social logo

dom-chef's Introduction





Build regular DOM elements using JSX

With dom-chef, you can use Babel or TypeScript to transform JSX into plain old DOM elements, without using the unsafe innerHTML or clumsy document.createElement calls.

It supports everything you expect from JSX, including:

If something isn't supported (or doesn't work as well as it does in React) please open an issue!

Install

$ npm install dom-chef

Usage

Make sure to use a JSX transpiler, set JSX pragma to h and optionally the pragmaFrag to DocumentFragment if you need fragment support.

// babel.config.js

const plugins = [
	[
		'@babel/plugin-transform-react-jsx',
		{
			pragma: 'h',
			pragmaFrag: 'DocumentFragment',
		},
	],
];

// ...
import {h} from 'dom-chef';

const handleClick = e => {
	// <a> was clicked
};

const el = (
	<div class="header">
		<a href="#" class="link" onClick={handleClick}>
			Download
		</a>
	</div>
);

document.body.appendChild(el);

Alternative usage

You can avoid configuring your JSX compiler by just letting it default to React and exporting the React object:

import React from 'dom-chef';

This has the advantage of enabling Fragment support with the TypeScript compiler, if you're using it compile JSX without Babel. Related issue: microsoft/TypeScript#20469

TS17016: JSX fragment is not supported when using --jsxFactory

Recipes

Set classes

const el = <span class="a b c">Text</span>;

// or use `className` alias
const el = <span className="a b c">Text</span>;

Inline styles

const el = <div style={{padding: 10, background: '#000'}} />;

Inline event listeners

const handleClick = e => {
	// <span> was clicked
};

const el = <span onClick={handleClick}>Text</span>;

This is equivalent to: span.addEventListener('click', handleClick)

Nested elements

const title = <h1>Hello World</h1>;
const body = <p>Post body</p>;

const post = (
	<div class="post">
		{title}
		{body}
	</div>
);

Set innerHTML

const dangerousHTML = '<script>alert();</script>';

const wannaCry = <div dangerouslySetInnerHTML={{__html: dangerousHTML}} />;

Render SVG

Note: Due to the way dom-chef works, tags <a>, <audio>, <canvas>, <iframe>, <script> and <video> aren't supported inside <svg> tag.

const el = (
	<svg width={400} height={400}>
		<text x={100} y={100}>
			Wow
		</text>
	</svg>
);

Use functions

If element names start with an uppercase letter, dom-chef will consider them as element-returning functions:

function Title() {
	const title = document.createElement('h1');
	title.classList.add('Heading');
	return title;
}

const el = <Title className="red">La Divina Commedia</Title>;
// <h1 class="Heading red">La Divina Commedia</h1>

This makes JSX also a great way to apply multiple attributes and content at once:

const BaseIcon = () => document.querySelector('svg.icon').cloneNode(true);

document.body.append(
	<BaseIcon width="16" title="Starry Day" className="margin-0" />
);

To improve compatibility with React components, dom-chef will pass the function's defaultProps property to itself (if present). Note that specifying attributes won't override those defaults, but instead set them on the resulting element:

function AlertIcon(props) {
	return <svg width={props.size} className={props.className} />
}

AlertIcon.defaultProps = {
	className: 'icon icon-alert'
	size: 16,
}

const el = <AlertIcon className="margin-0" size={32} />;
// <svg width="16" class="icon icon-alert margin-0" size="32" />

License

MIT ยฉ Vadim Demedes

dom-chef's People

Contributors

fregante avatar therealsyler avatar reyronald avatar artusm avatar bfred-it avatar filipekiss avatar floedelmann avatar ngryman avatar yuta0801 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.