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

kleinui

v0.0.3

Published

klein UI is designed to allow for flexible creation of web UIs in typescript. The main aims of klein UI are to: - contribute minimally to bundle size - provide an intuitive builder pattern api for building dom nodes - allow for full control over when chan

Downloads

115

Readme

klein UI

klein UI is designed to allow for flexible creation of web UIs in typescript. The main aims of klein UI are to:

  • contribute minimally to bundle size
  • provide an intuitive builder pattern api for building dom nodes
  • allow for full control over when changes are rendered
  • reduce complexity and mental overhead vs larger ts frameworks

Install

npm i kleinui

Basic app setup

main.ts

import { container, header1 } from "kleinui/elements"
import { renderApp } from "kleinui"

const app = new container(
    new header1("Welcome to your first klein UI application!")
)

renderApp(app, document.getElementById("app")!)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>klein UI app</title>
</head>
<body style="margin: 0;">
    <div id="app" style="height: 100vh;"></div>
    <script src="out.js"></script>
</body>
</html>

Use your favourite build tool to bundle the typescript: esbuild main.ts --outfile="out.js" --bundle --watch

Then, simply run a basic web server such as vscode live server extension.

The basic premise of kleinui is that children of nodes are simply passed as arguments to the class constructor and then other attributes can be modified with the methods of the node. These methods can be chained together to make a readable and easy to follow tree of nodes. Strings are automatically converted to kleinTextNode.

For example, to add styles and an event listener:

const app = new container(
    new header1("Welcome to your first klein UI application!")
        .addStyle("color: red;")
        .addEventListener("click", ()=>{
              alert("clicked")
        })
)

If you wanted to change the contents of the h1 when it was clicked, the first argument given to the callback function of the event listener is the node itself. This means you don't have to store the node in a variable beforehand to reference it. In this example, the h1 will become a counter incrementing by 1 on every click.

var count = 0
const app = new container(
    new header1(count.toString())
        .addStyle("color: red;")
        .addEventListener("click", (self)=>{
              count++;
              (self.children[0] as kleinTextNode).content = count.toString()
              self.children[0].rerender()
        })
)

Although this looks quite cumbersome compared with counter examples with other TS frameworks, it allows for an unmatched level of flexibility. As you can see, you must explicitly call rerender() on nodes for their changes to appear in the DOM. I reccomend to only use rerender() on specifically text nodes like in this example and prefer lightRerender() for normal nodes. This just means that only changes are reflected as opposed to a complete restrucutring of the element which is not as performant.

Components

Components are completely left up to you but I would reccomend that you just use a function that returns a kleinNode. For example, here I have a component that takes in a name and an age and returns a simple card:

function Card(name: string, age: number) {
    return new container(
        new header1(name).addStyle("color: red; width: min-content;"),
        new paragraph(`This is ${name} and he/she is ${age} years old.`)
    ).addStyle("border: 1px solid black; border-radius: 2em; padding: 1em; width: 8em;")
}

I'll admit, I'm not an artist, but it's just an example!