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

@ryanmorr/viewdoo

v1.0.4

Published

A crude Svelte-inspired UI library just because

Downloads

8

Readme

viewdoo

Version Badge License Build Status

A crude Svelte-inspired UI library just because

Description

This project is a proof of concept, built on the principles of another project of mine called voodoo. Combined with the functionality of stache and csscope to create a basic implementation that mimics the core features of Svelte.

Install

Download the CJS, ESM, UMD versions or install via NPM:

npm install @ryanmorr/viewdoo

Usage

A viewdoo component features similar composition and functionality to a Svelte component; encapsulating scoped styles, reactive scripting, and HTML templating to form reusable, self-contained views:

import viewdoo from '@ryanmorr/viewdoo';

const Counter = viewdoo(`
    <style>
        .counter {
            padding: 1em;
            border: 1px solid black;
        }
    </style>

    <script>
        this.count = 0;
                
        function increment() {
            count += 1;
        }
    </script>

    <div class="counter">
        <p>Count: {{count}}</p>
        <button onclick={{increment}}>Increment</button>
    </div>
`);

Components are defined by providing the source as a string consisting of HTML markup with optional style and script tags. The script tag contains just regular JavaScript responsible for managing the state and behavior of a component instance in an isolated context. Any variables defined within the script are available to the template:

const HelloWorld = viewdoo(`
    <script>
        const message = 'World';
    </script>

    <h1>Hello {{message}}</h1>
`);

A state variable can be defined by assigning properties to this within the script. These variables are reactive by nature, meaning they will automatically trigger an update of the component when the value is changed:

const Clock = viewdoo(`
    <script>
        const getTime = () => new Date().toLocaleTimeString();

        this.time = getTime();
        
        setInterval(() => (time = getTime()), 1000);
    </script>

    <div>Time: {{time}}</div>
`);

The HTML structure is formulated using mustache-style templating that supports simple value interpolation, expressions, loops, and if statements:

const Users = viewdoo(`
    <script>
        this.users = [
            {name: 'Joe', isLoggedIn: true},
            {name: 'John', isLoggedIn: false},
            {name: 'Jane', isLoggedIn: true},
            {name: 'Jim', isLoggedIn: true},
            {name: 'Jen', isLoggedIn: false}
        ];
    </script>

    <ul>
        {{each users as {name, isLoggedIn}, i}}
            {{if isLoggedIn}}
                <li class="logged-in">{{i + 1}}: {{name}}</li>
            {{else}}
                <li class="logged-out">{{i + 1}}: {{name}}</li>
            {{/if}}
        {{/each}}
    </ul>
`);

Including a style tag allows you to declare CSS styles that are automatically scoped to the component, supporting all CSS selectors and media queries:

const Foo = viewdoo(`
    <style>
        .foo {
            background-color: red;
        }

        @media screen and (max-width: 600px) {
            .foo {
                background-color: blue;
            }
        }
    </style>

    <div class="foo"></div>
`);

The source string of the component is compiled and returns a constructor function for creating instances. You can than create instances of the component with an optional initial state, the properties of which will become reactive state variables within the inner script of the component. It returns an array with the rendered component inside a document fragment at the first index and an external state object at the second index. The properties of this external state object and the internal reactive state variables of the same name will always remain in sync with one another because they are in fact one and the same:

// Create an instance of a component
const [fragment, state] = Component({
    foo: 1,
    bar: 2,
    baz: 3
});

// Mount the component instance to the DOM
document.body.appendChild(fragment);

// Changing the component instance state externally will trigger an update
state.foo = 20;

License

This project is dedicated to the public domain as described by the Unlicense.