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

@ncpa0cpl/vanilla-jsx

v0.0.1-alpha.32

Published

*VanillaJSX provides a syntactic sugar for creating HTMLElements*

Downloads

58

Readme

VanillaJSX provides a syntactic sugar for creating HTMLElements

Example

These two snippets are equivalent:

const container = document.createElement('div');
const header = document.createElement('h1');
header.textContent = 'Hello, world!';
header.classList.add('custom-header');
div.setAttribute('id', 'header-container');
container.appendChild(header);
const container = (
    <div id="header-container">
        <h1 class="custom-header">Hello, world!</h1>
    </div>
);

TypeScript and build step

To enable TypeScript support set these options in your tsconfig.json:

{
    "compilerOptions": {
        "jsx": "react-jsx",
        "jsxImportSource": "@ncpa0cpl/vanilla-jsx"
    }
}

Similar options will need to be set in the build tool you are using. For example the following option should be used in esbuild:

esbuild
  .build({
    jsxImportSource: "@ncpa0cpl/vanilla-jsx",
    ...rest
  })

Signals

On top of the basic syntax sugar it is also possible to easily bind signals to element attributes, children and listeners.

VanillaJSX does not enforce any specific signal implementation, but it does provide one if you wish to use it. For a given signal to be able to be used it needs to be registered with the SignalsReg:

import { SignalsReg } from "@ncpa0cpl/vanilla-jsx";
import { MySignal } from "./my-signal";

class MySignalInterop {
    is(maybeSignal: unknown): maybeSignal is MySignal<unknown> {
        return maybeSignal instanceof MySignal;
    }
    add(signal: MySignal<any>, listener: (value: any) => void) {
        signal.addListener(listener);
        listener(signal.value);
        return () => signal.removeListener(listener);
    }
}

SignalsReg.register(new MySignalInterop());

declare global {
    namespace JSX {
        interface SupportedSignals<V> {
            mySignal: MySignal<V>;
        }
    }
}

Provided interops

There's a few interops provided by default that can be imported and registered:

import {
    SignalsReg,
    JsSignalInterop,
    MiniSignalInterop,
    PreactSignalInterop,
} from "@ncpa0cpl/vanilla-jsx";

SignalsReg.register(new JsSignalInterop());
SignalsReg.register(new MiniSignalInterop());
SignalsReg.register(new PreactSignalInterop());

VanillaJSX Signals usage example

import { sig } from "@ncpa0cpl/vanilla-jsx/signals";

function getCounterComponent() {
    const counter = sig(0);

    const onClick = () => {
        counter.dispatch(current => current + 1);
    };

    return (
        <div>
            <button 
                class={counter.derive(c => `counter_${c}`)} 
                onClick={onClick}
            >
                Click me!
            </button>
            <p>{counter}</p>
        </div>
    );
};

Conditional rendering and maps

It's possible to achieve conditional rendering or maping a list of elements by using the derive() method of the provided signal implementation:

import { sig, Signal } from "@ncpa0cpl/vanilla-jsx/signals";

function displayElements(elems: Signal<string[]>) {
    return <div>
        {elems.derive(list => {
            if (list.length === 0) {
                return <p>No elements to display</p>;
            }
            return list.map((elem, i) => <p>{elem}</p>);
        })}
    </div>;
}

However in case of another signal implementation or for more optimized rendering of lists a few base components are available:

import { If } from "@ncpa0cpl/vanilla-jsx";
import { sig } from "@ncpa0cpl/vanilla-jsx/signals";

function conditionComponent() {
    const someCondition = sig(false);

    return <div>
        <If 
            condition={someCondition}
            then={() => <p>Condition Met!</p>}
            else={() => <p>Condition Not Met!</p>}
        />
    </div>;
}

import { Switch, Case } from "@ncpa0cpl/vanilla-jsx";

enum MyEnum {
 A, B, C
}

function displayOneOf(value: JSX.Signal<MyEnum>) {
    return <div>
        <Switch
            value={MyEnum.A}
        >
            <Case match={MyEnum.A}>
                {() => <div>Case A</div>}
            </Case>
            <Case match={MyEnum.B}>
                {() => <div>Case B</div>}
            </Case>
            <Case default>
                {() => <div>Default case</div>}
            </Case>
        </Switch>
    </div>;
}

import { Range } from "@ncpa0cpl/vanilla-jsx";

function displayList(list: JSX.Signal<string[]>) {
    return <div>
        <Range
            data={list}
            into={<ul />}
        >
            {(value) => <li>{value}</li>}
        </Range>
    </div>;
}