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

@miqro/jsx

v0.0.8

Published

very ***experimental*** module re-implementing a very small faction of react hooks api ***just for fun***.

Downloads

5

Readme

@miqro/jsx

very experimental module re-implementing a very small faction of react hooks api just for fun.

setup

first setup typescript to use JSX factory editing tsconfig.json and setting jsx, jsxFactory and jsxFragmentFactory to react, JSX.createElement and JSX.Fragment.

{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "JSX.createElement",
    "jsxFragmentFactory": "JSX.Fragment",
    ...
  },
  ...
}

you also need to import JSX in your .tsx files like this.

import JSX from "@miqro/jsx";

function SomeComponent() {
  return <p>Hello</p>;
}

implemented hooks

useState

import JSX, { useState } from "@miqro/jsx";

function SomeComponent() {
  const [count, setCount] = useState(0);

  if(count < 10) {
    setCount(count + 1);
  }

  return <p>{count}</p>;
}

useEffect

import JSX, { useEffect, useState } from "@miqro/jsx";

function SomeComponent() {
  const [count, setCount] = useState(0);

  useEffect()=>{
    const timeout = setTimeout(()=>{
      setCount(count + 1);
    }, 1000);
    return () => {
      clearTimeout(timeout);
    }
  }, [count]);

  return <p>{count}</p>;
}

useContext

import JSX, { createContext, useContext } from "@miqro/jsx";

const context = createContext();

function ParentComponent() {
  return <context.provider value={true}>
    <SomeComponent />
  </context.provider>
}

function SomeComponent() {
  const value = useContext(context);

  return <p>{value}</p>;
}

useQuery

import JSX, { useQuery } from "@miqro/jsx";

function SomeComponent() {
  const [offset, setOffset] = useQuery("offset", 0);

  return <p>{offset}</p>;
}

usePathname

import JSX, { usePathname } from "@miqro/jsx";

function SomeComponent() {
  const pathname = usePathname();

  return <p>{pathname}</p>;
}

runtimes

to support server side and browser rendering you need to specify the runtime needed or implement your own.

import { runtime } from "@miqro/jsx-node";
import { createContainer, createElement } from "@miqro/jsx";
import AppComponent from "./app.js";

const root = runtime.createElement("div");
const container = createContainer(runtime, root);
container.render(createElement(AppComponent));

browser

to use in the browser install @miqro/jsx-dom module and use the createDOMContainer function to create a container.

first install the dom runtime.

npm install @miqro/jsx-dom

import { createDOMContainer } from "@miqro/jsx-dom";
import { createElement } from "@miqro/jsx";
import AppComponent from "./app.js";

window.onload = function() {
  const root = document.getElementById("root");
  const container = createDOMContainer(root);
  container.render(createElement(AppComponent));
  //container.disconnect();
}

on Node.js

to use in Node.js server side install @miqro/jsx-node module and use the createNodeContainer function to create a container.

first install the Node.js runtime.

npm install @miqro/jsx-node

import { createNodeContainer, runtime } from "@miqro/jsx-node";
import { createElement } from "@miqro/jsx";
import AppComponent from "./app.js";

const root = runtime.createElement("div");
const container = createNodeContainer(root);
container.render(createElement(AppComponent));

console.log(root.toString()); // prints rendered html

//container.disconnect();

SSR ( Server Side Rendering )

first install the Node.js runtime.

npm install @miqro/jsx-node

import { createServer } from "http";
import { SSRApp } from "@miqro/jsx-node";

const ssrApp = SSRApp({
  appPath: "./build/app.js"
});

createServer(ssrApp.serverOptions, ssrApp.listener).listen(8080, (err) => {
  if (err) {
    console.err(err);
    process.exit(1);
  } else {
    console.log("listening");
  }
});

by default SSRApp changes the default runtime options to disable effects, events, ref listeners and refresh conditions.

to enable this features to need to pass the clientRuntimeOptions and serverRuntimeOptions options like this.

import { createServer } from "http";
import { SSRApp } from "@miqro/jsx-node";

const ssrApp = SSRApp({
  appPath: "./build/app.js",
  // this execute options will apply on rendering on the server
  serverRuntimeOptions: {
    disableEffects: true,
    disableEvents: true,
    disableRefListener: true,
    disableRefresh: true
  },
  // this execute options will apply on rendering on the client
  clientRuntimeOptions: {
    disableEffects: false,
    disableEvents: false,
    disableRefListener: false,
    disableRefresh: false
  }
});

Component Options

shadowInit

by default all components are rendered inside a ShadowRoot with mode: "closed".

to change this behavior set the shadowInit attribute of the component's function.

import JSX from "@miqro/jsx";

function Component {

}
Component.shadowInit = false; // to disable the use of ShadowRoot
/*
// to set custom ShadowInit options
Component.shadowInit = {
  mode: "open"
}; 
*/

asFragment

by default all components are rendered inside an HTMLElement, this is due to support ShadowRoot usage and other uses.

to change this behavior set the asFragment attribute of the component's function. the default value is false.

import JSX from "@miqro/jsx";

function Component {

}
Component.asFragment = true;

set default options

import JSX, { setDefaultOptions } from "@miqro/jsx";

// only one call to this method is allowed and only if no Component has been rendered, so place it earliest in your code.
setDefaultOptions({
  asFragment: true, // defaults to false
  shadowInit: false // defaults to { mode: "closed" }
});

function Component {

}

debug log

to enable debug loging into the runtime's console call the enableDebugLog at earliest in the execution.

import { enableDebugLog } from "@miqro/jsx";

enableDebugLog();

escape hatches

to access the underlying HTMLElement rendering the component use useElement().

useElement

import JSX, { useElement } from "@miqro/jsx";

function Component {
  const element = useElement();
}

useRef

to access the underlying reference node of something rendered use useRef().

import JSX, { useEffect, useRef } from "@miqro/jsx";

function Component {
  const ref = useRef(null);

  useEffect(()=>{
    if(ref.current) {
      ref.current.textContent = "Another Text";
    }
  }, [ref.current]);

  return <>
    <p ref={ref}>Some text </p>
  <>
}

useRefresh

import JSX, { useRefresh } from "@miqro/jsx";

function Component {
  const refresh = useRefresh();

  // to force a re-render with the same props and children
  // refresh();
}

useRuntime

import JSX, { useRuntime } from "@miqro/jsx";

function Component {
  const runtime = useRuntime();

  console.log(runtime.name);

  return <p>Hello</p>
}