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

tsxe

v2.1.4

Published

Create HTML elements by TSX/JSX syntax without React.

Downloads

3

Readme

TSXe

Create DOM objects by JSX syntax without React.

JSX syntax have the advantages of easy to write, read and maintenance for complicated HTML elements creation in JavaScript and JSX usually mixed with React JS.

However, if you may want to control the DOM directly or don't want to deal with React lifecycle, TSXe will be an option. It makes possible to create DOM objects with JSX syntax without React and supports most browsers, includes IE 11.

Featues

  • Supports Visual Studio Code's IntelliSense
  • Supports custom components, both Function Component and Class Component
  • Supports fragments (Short Syntax only available on TypeScript 4.0+)
  • Supports Refs
  • Type checking for HTML tags and attributes
  • Type checking for CSS properties and values (by CSSType)

Install

npm install tsxe csstype

Setup

TSXe supports both TSX and JSX style of syntax. To enable your code run in browsers, a transpiler and JavaScript module bundler are required.

TSX: webpack + TypeScript

Install the packages you neeeded

npm install --save-dev typescript ts-loader webpack webpack-cli

Create / Update your tsconfig.json:

{
    "compilerOptions": {
        "module": "CommonJS",
        "jsx": "react",
        "jsxFactory": "TSXe.createElement",
        /* TypeScript 4.0+ Only */
        "jsxFragmentFactory": "TSXe.Fragment",
    }
}

Create / Update your webpack.config.js:

const path = require("path");

module.exports = {
    mode: 'development',
    devtool: "source-map",
    entry: {
        index: __dirname + "/src/index.tsx",
    },
    output: {
        path: path.resolve(__dirname, "./dist"),
        filename: "index.js"
    },
    plugins: [
    ],
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                include: /src(\\|\/)/,
                use: ["ts-loader"],
            }
        ],
    },
    resolve: {
        extensions: [".tsx", ".ts"],
    }
};

JSX: webpack + Babel

Install the packages you neeeded

npm install --save-dev @babel/core @babel/preset-env @babel/preset-react webpack webpack-cli babel-loader

Create / Update your webpack.config.js

const path = require("path");

return {
    // This is a partial configuration
    // For the details, please refer webpack and Babel official documents
    module: {
        rules: [
            {
                test: /\.jsx$/,
                use: [
                    {
                        loader: "babel-loader",
                        options: {
                            presets: [
                                "@babel/preset-env",
                                [
                                    "@babel/preset-react",
                                    {
                                        pragma: "TSXe.createElement",
                                        pragmaFrag: "TSXe.Fragment"
                                    }
                                ]
                            ]
                        }
                    },
                ],
            }
        ],
    },
    resolve: {
        extensions: [".jsx", ".js"],
    }
};

Examples

Elements Creation

import TSXe from "tsxe";

document.body.appendChild(
    <div class="appWrapper">
        <h1>My Great App</h1>
        <div class="appBody" dataset={{appData: "Hello"}}>
            <button onClick={()=>{ alert("Hello World"); }} style={{color: "red"}}>
                Say Hello
            </button>
        </div>
    </div>
);

Class Component

import TSXe from "tsxe";

interface MyComponentProps {
    title: string
}

class MyComponent extends TSXe.Component<MyComponentProps> {
    render(): Node {
        return <h1>{this.props.title}</h1>
    }
}

document.body.appendChild(<MyComponent title="My Awsome Component"/>);

Function Component

import TSXe from "tsxe";

interface FuncProps {
    name: string
}
function FuncRender(props: TSXe.PropsWithChildren<FuncProps>) {
    return (
        <div>
            <h1>{props.name}</h1>
            {props.children}
        </div>
    );
}

document.body.appendChild(
    <FuncRender name="Function Render">
        <div>Some text here</div>
    </FuncRender>
);

Fragment

import TSXe from "tsxe";

var fragment = (
    <TSXe.Fragment>
        <label for="password">Password</label>
        <input id="password" type="password" />
    </TSXe.Fragment>
);

document.body.appendChild(<div>{fragment}</div>);

Event

import TSXe from "tsxe";

interface WelcomeProps {
    name: string
}
class Welcome extends TSXe.Component<WelcomeProps> {
    private count = 0;
    getTextContent() {
        return `Hello, ${this.props.name}. You clicked me ${this.count} times.`;
    }
    update() {
        this.node.textContent = this.getTextContent();
    }
    addCount() {
        this.count++;
        this.update();
    }
    render() {
        return <h1 onClick={this.addCount.bind(this)}>{this.getTextContent()}</h1>;
    }
}

document.body.appendChild(<Welcome name="Alex" ref={welcomeComponent}></Welcome>)

APIs

TSXe.render(component: string | Node | Component<any>, container: Node): void

Render Text / Node / Component into the container node.

TSXe.Component<T>

Base class of class component, all class components should inherit from this class.

TSXe.Component.createComponent(name: Component<P>, props?: TSXeProperties, ...content: (string | Node Component<any>)[]): Node

Create component with specificed component class. This method is a low level method.

TSXe.createRef<T>()

Create reference object.

Example:

import TSXe from "tsxe";

interface WelcomeProps {
    name: string
}
class Welcome extends TSXe.Component<WelcomeProps> {
    updateName(name: string) {
        this.node.textContent = `Hello, ${name}`;
    }
    render() {
        return <h1>Hello, {this.props.name}</h1>;
    }
}

const welcomeComponent = TSXe.createRef<Welcome>();
document.body.appendChild(<Welcome name="Boo" ref={welcomeComponent}></Welcome>);

welcomeComponent.current.updateName("Alex");

Supported Browsers

Modern browsers and Internet Explorer 11

Roadmap

  • And eslint to ensure the code quality
  • Add unit tests
  • Add async rendering
  • Add dependency injection
  • Create automatic tools to update JSX content
  • Create command line tools to create new projects

License

Copyright (c) 2020 - 2022 hkm-mo

Licensed under the MIT license.

HTML document by MDN is licensed under CC-BY-SA 2.5.