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

@adarovsky/vlayout

v1.7.6

Published

A port of simple reactive layout engine running in browsers

Downloads

6

Readme

vlayout Build Status Coverage Status

A port of simple reactive layout engine running in browsers.

Give it a layout description, wire external views, button click handlers and put resulting React.JS component to your page. Layout will auto-update itself.

Integrating to the app

import React, {Component} from 'react';
import './App.css';
import {Engine, Layout} from '@adarovsky/vlayout';
import {EMPTY, interval} from "rxjs";
import {delay, pluck, scan, startWith} from "rxjs/operators";

class App extends Component {

  state = {
    error: null,
    isLoaded: false,
    layout: null
  };

  constructor(props) {
    super(props);

    this.engine = new Engine();
    this.engine.inputs.registerInput("counter", this.engine.numberType(), interval(1000).pipe(
        startWith(0),
        scan((acc, one) => {
          const [cur, delta] = acc;
          let d = delta;
          if (cur + d > 4 || cur + d < 0) {
            d = -d;
          }
          return [cur + d, d];
        }, [1, -1]),
        pluck(0)
    ));
    // this.engine.registerView('myView', x => <SampleView parentView={x} key={'123'}/>);
    this.engine.registerButton('myButton', async () => {
      console.log('clicked');
      await EMPTY.pipe(delay(1000)).toPromise();
    });
  }

  componentDidMount() {
    fetch("/test.vlayout")
        .then(res => res.text())
        .then(
            (result) => {
              try {
                this.setState({
                  isLoaded: true,
                  layout: <Layout engine={this.engine} content={result}/>
                });
              }
              catch (e) {
                this.setState({
                  isLoaded: true,
                  error: e
                });
              }
            },
            (error) => {
              this.setState({
                isLoaded: true,
                error
              });
            }
        )
  }

  render() {
    const {error, isLoaded, layout} = this.state;
    if (error) {
      return <div>Ошибка: {error.message}</div>;
    } else if (!isLoaded) {
      return <div>Loading...</div>;
    } else {
      return layout;
    }
  }
}

export default App;

Let's highlight key points here: Here we create a central point where we register all connections between layout and outer world

  this.engine = new Engine();
  this.engine.inputs.registerInput(name, type, source)

To pass external values inside layout, we use

  engine.inputs.registerInput(name: string, type: Engine.type, source: rxjs.Observable)

To receive button press event from layout, register button:

  this.engine.registerButton(name: string, handler: () => Promise);

Please note that button will be disabled until handler's promise finishes or fails. Button look is defined in layout description

To render external component in layout, one need to derive from ReactView<S extends ReactViewState> and be sure to pass style={self.state.style} in render code

Once preparations are done, we can create and use Layout component like so

  render() {
      return <Layout engine={this.engine} content={content}/>
  }

In the example above, layout content is downloaded using AJAX, but it's up to you where to get it.

Layout description

Layout description has 4 declarations. Let us show it by example. For full description, refer layout syntax

bindings {
  myButton: button
}

inputs {
  counter: Number
}

layout {
 layer {
    id: "background"
    z_order: 1

    roundRect {
        center { x: 0.5 y: 0.5 }
        size { width: 0.8 }
        aspect: 16/9
        backgroundColor: #cccccc
    }
 }

 layer {
    id: "label"
    z_order: 2

    vertical {
        alignment: .center
        center { x: 0.5 y: 0.5 }
        label {
            text: String(counter)
        }
        myButton {
            backgroundColor: #dddddd
            strokeColor: #aaaaaa
            strokeWidth: 4
            text: "Click me"
            cornerRadius: 0.5
        }
    }
 }
}

it results with an image like so: screenshot

Try to experiment with changing propery values. These are all expressions, not constants! For example, you can change button corner radius like so

  cornerRadius: switch(counter) {
      case 0|1 => 0.5 // you can match against a set of constants
      case 2 => 10    // note, that corner radius > 0.5 is absolute, specified in pixels
      case _ => 4     // set up a default value
  }

License

MIT license. Copyright ©️ Alexander Darovsky.

Credits: Marc J. Schmidt for ideas of handling element resize