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

@webfruits/core

v1.0.6

Published

A TypeScript library for building user interfaces using the real DOM.

Downloads

45

Readme

webfruits/core   Language TypeScript GitHub license GitHub package.json version npm version

... is all about creating highly customized, fast and interactive user interfaces using the real DOM and not a virtual one. It is super slim, modular and has no dependencies. All declarations and coding can be done with TypeScript. There is no need to learn any proprietary template language.

To make things easy, webfruits/core provides mainly one class with an easy API to work with: UIComponent.

A UIComponent combines the creation of HTMLElements, the styling of those elements and any kind of logic. UIComponents can be combined and extended in any way to create simple to complex reusable custom components.

Here comes a little snippet of how webfruits/core looks on Reacts HelloMessage example:

class HelloMessage extends UIComponent {
    constructor(message: string) {
        super("hello-message");
        this.view.innerHTML = message;
    }
}

let helloMessage = new HelloMessage("Taylor");
document.body.appendChild(helloMessage.view);

The above example outputs following to the DOM:

<body>
    <hello-message>Taylor</hello-message>
</body>

Features

  • Flexible. With UIComponent you can create CustomElements, use existing or native HTMLElements.
  • All in one place. Elements, styles and logic are defined within each UIComponent.
  • Inline styling to have fun with. All styles are defined with TypeScript and will be processed and updated automatically. Use transform properties like x, y, scale or rotate as direct styling properties.
  • Signals. webfruits/core uses its own implemenation of Signals with the same API like js-signals.
  • DOMObserver. With UIComponent you can subscribe to signals like onAddedToStageSignal or onRemovedFromStageSignal.
  • Extended support for native events. Adding and removing (!) native events has never been easier.
  • Modular. UIComponent uses a handful classes, which all can be used indepedently like DeviceUtils, NativeEventsController or DOMObserver.

Benefits

  • No dependencies. webfruits/core is a simple, super lightweight and modular ui library written in TypeScript.
  • No virtual DOM. Create and access the real DOM. This brings you in full control and best performance.
  • It's not a framework. It's a library. So it's up to your needs how to structure your application. But there is a recommondation for a best practice at webfruits/best-practice
  • It's vanilla coding. Just use pure TypeScript to code your UI.
  • Easy to learn and work with. Even the source code itself should be understandable within minutes. So you are in full control to fix or extend webfruits/core for your needs.

Installation

webfruits/core is intended to use with npm and webpack or similar module-handler.

npm install @webfruits/core --save

Documentation

There is no documention available at the moment. Meanwhile, please have look at the source code, which should not that hard to understand.

Usage

After installation you have to import UIComponent to make use of it:

import {UIComponent} from "@webfruits/core";

Direct use of UIComponent

The direct use of a UIComponent are mostly done within an extended UIComponent.

Create an UIComponent from an existing HTMLElement:

In this case UIComponent acts like a controller. It doesn't create a view, but instead it uses an existing HTMLElement as its view. For a WebApp this should be a rare use case, but with server side rendered pages this could get very handy to create additional features.

let bodyComponent = new UIComponent(document.body);
let headerComponent = new UIComponent(document.getElementById('header')); 

Create an UIComponent with a CustomElement:

CustomElements are part of the WC3 WebComponents Standard. Using them makes reading and understanding of the DOM much more specific. Instead of having something like this: <div class="ui-component"> you are getting <ui-component>.
If UIComponent is getting constructed with no argument, it will create the default CustomElement <ui-component>. If a element name is giving as an argument, this will create a individual CustomElement like: <ELEMENT-NAME>. The element name has to contain a hyphen ("-") to meet the specification for creating CustomElements.

// creates the webfruits default CustomElement for UIComponent.view ("<ui-component>")
let defaultComponent = new UIComponent();

// creates a individual CustomElement for UIComponent.view ("<custom-component>")
let customComponent = new UIComponent("custom-component");

Create an UIComponent with a native HTMLElement:

If you need native APIs of specific HTMLElements like HTMLImageElement or HTMLAnchorElement, you can do that by using a native tag name as element name. Tip: In TypeScript you can pass the type of the view to offer full code support.

// creates a native HTMLImageElement for UIComponent.view ("<img>")
let imageComponent = new UIComponent<HTMLImageElement>("img");
imageComponent.view.src = "image.jpg";

// creates a native HTMLAnchorElement for UIComponent.view ("<a>")
let imageComponent = new UIComponent<HTMLAnchorElement>("a");
imageComponent.view.href = "https://github.com";

Extending UIComponent

UIComponent gets really powerful by extending it to create reusable, encapsuled custom components.
The following snippet ports Reacts Timer example to webfruits:

class Timer extends UIComponent {
    
    private _intervalID;
    private _time = 0;
    
    constructor() {
        super("timer-component");
        this.onAddedToStageSignal.add(() => this.start());
        this.onRemovedFromStageSignal.add(() => this.stop());
        this.render();
    }
    
    public start() {
        this._intervalID = setInterval(() => this.tick(), 1000);
    }
    
    public stop() {
        clearInterval(this._intervalID);
    }
    
    private tick() {
        this._time += 1;
        this.render();
    }
    
    private render() {
        this.view.innerHTML = "Seconds: " + this._time;
    }
}
let timer = new Timer();
document.body.appendChild(timer.view);

Styling a UIComponent

class StyledComponent extends UIComponent {
    
    private _childComponent: UIComponent;
    
    constructor() {
        super("styled-component");
        this._childComponent = new UIComponent("child-component");
        this.addChild(this._childComponent);
    }
    
    // override UIComponent.updateStyles
    // updateStyles will be called after adding this component to DOM
    // and on every window resize event
    public updateStyles() {
        
        // styling of this view
        this.applyStyle({
            backgroundColor: 0xFF0000,
            userSelect: "none" 
        });
        
        // styling of a childComponent view
        this._childComponent.applyStyle({
            position: "relative",
            left: 100, // numeric value are in pixels (px)
            x: 100,
            top: "50%", // use string values
            y: "-50%",
            width: this.calcChildWidth(), // use functions to calculate complex values 
            height: 50 + "vh"
        })
    }
    
    private calcChildWidth(): number {
        return window.innerWidth > 500 ? 300 : 200;
    }
}

more webfruits

Licence

webfruits/core is MIT licensed.