5.10.0 • Published 8 months ago

untrue v5.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Untrue

JavaScript library for rendering user interfaces.

Installation

The easiest way to get started with Untrue is through a web app.

npm i untrue @untrue/web

Compatible with any build tool: Parcel, Vite, Webpack, etc.

Native app development available with Detonator.

Get started

You can add Untrue to any part of your page.

import $ from "untrue";

import { Tree } from "@untrue/web";

import App from "./App";

const tree = new Tree(document.body);

// $ is a shorthand to represent slots

tree.mount($(App));

In this case, we're adding Untrue to body.

More on App in the next section.

Basic features

Interactivity

A component state can change at any time and Untrue knows which nodes should be updated in the DOM.

import $, { Component, Props, State } from "untrue";

interface AppState extends State {
  counter: number;
}

class App extends Component<Props, AppState> {
  init(): void {
    this.state = { counter: 0 };
  }

  onIncrement = (): void => {
    const { counter } = this.state;

    this.updateState({ counter: counter + 1 });
  };

  render(): any {
    // after the first click, counter is no longer 0 but 1

    const { counter } = this.state;

    // regular arrays are used to return multiple slots

    return [
      $("span", counter),
      $("button", { onclick: this.onIncrement }, "increment"),
    ];
  }
}

export default App;

The output HTML will be:

<span>0</span> <button>increment</button>

button will have an onclick listener attached to it.

span will be updated with the new counter every time button is clicked.

Modularity

Components can be classes or functions and are used to group multiple slots.

import $, { Component, Props, State } from "untrue";

function App(): any {
  return [
    $(Header, { title: "Untrue" }), // pass title as prop (external data)
    $(Footer, { year: 2049 }), // pass year as prop (external data)
  ];
}

interface HeaderProps extends Props {
  title: string;
}

interface HeaderState extends State {
  counter: number;
}

class Header extends Component<HeaderProps, HeaderState> {
  init(): void {
    this.state = { counter: 0 };
  }

  onIncrement = (): void => {
    const { counter } = this.state;

    this.updateState({ counter: counter + 1 });
  };

  render(): any {
    const { title } = this.props; // external data

    const { counter } = this.state; // internal data

    return $("header", [
      $("h1", title),
      $("span", counter),
      $("button", { onclick: this.onIncrement }, "increment"),
    ]);
  }
}

interface FooterProps extends Props {
  year: number;
}

function Footer({ year }: FooterProps): any {
  return $("footer", [
    $("span", `copyright, ${year}`),
    $("a", { href: "https://example.com" }, "follow me"),
  ]);
}

export default App;

The output HTML will be:

<header>
  <h1>Untrue</h1>
  <span>0</span>
  <button>increment</button>
</header>
<footer>
  <span>copyright, 2049</span>
  <a href="https://example.com">follow me</a>
</footer>

Header will be stateful while Footer will be stateless. Both components receive props.

Lifecycle events

  • mount: The first render.
  • update: Every render after the first one.
  • render: Every render. It's fired after mount or update events.
  • unmount: Component has been unmounted.

Multiple event listeners can be attached to a single event. Specially useful to have more organized code.

import $, { Component, Props, State } from "untrue";

interface TimerState extends State {
  counter: number;
}

class Timer extends Component<Props, TimerState> {
  init(): void {
    this.state = { counter: 0 };

    let interval: number | undefined;

    // start interval on mount

    this.on("mount", () => {
      interval = setInterval((): void => {
        const { counter } = this.state;

        this.updateState({ counter: counter + 1 });
      }, 1000);
    });

    // clear interval on unmount

    this.on("unmount", (): void => {
      clearInterval(interval);
    });

    // check "counter" change on update

    this.on("update", (): void => {
      // this.props and this.prevProps are also available

      const { counter } = this.state;
      const { counter: prevCounter } = this.prevState!;

      if (counter !== prevCounter) {
        console.log("Counter has been updated.", { counter, prevCounter });
      }
    });
  }

  render(): any {
    const { counter } = this.state;

    return $("span", counter);
  }
}

interface AppState extends State {
  running: boolean;
}

class App extends Component<Props, AppState> {
  init(): void {
    this.state = { running: false };
  }

  onClick = (): void => {
    const { running } = this.state;

    this.updateState({ running: !running });
  };

  render(): any {
    const { running } = this.state;

    return [
      $(
        "button",
        { onclick: this.onClick },
        running ? "end timer" : "start timer"
      ),
      $("br"),
      running ? $(Timer) : null,
    ];
  }
}

export default App;

After the button click, the output HTML will be:

<button>end timer</button>
<br />
<span>0</span>

A console.log happens every second because interval updates counter on every call.

5.9.2

8 months ago

5.10.0

8 months ago

5.8.1

9 months ago

5.8.0

9 months ago

5.9.1

8 months ago

5.9.0

8 months ago

5.6.8

10 months ago

5.6.7

10 months ago

5.6.6

10 months ago

5.7.3

9 months ago

5.7.2

10 months ago

5.7.1

10 months ago

5.7.0

10 months ago

5.3.1

11 months ago

5.3.0

11 months ago

4.5.8

1 year ago

4.5.7

1 year ago

4.5.4

1 year ago

4.5.6

1 year ago

4.5.5

1 year ago

5.4.2

11 months ago

5.4.1

11 months ago

5.4.0

11 months ago

5.0.1

12 months ago

5.0.0

12 months ago

4.7.0

1 year ago

5.5.9

11 months ago

5.5.8

11 months ago

5.5.7

11 months ago

5.5.6

11 months ago

5.5.11

11 months ago

5.5.5

11 months ago

5.5.4

11 months ago

5.5.3

11 months ago

5.5.10

11 months ago

5.5.2

11 months ago

5.5.1

11 months ago

5.5.0

11 months ago

5.1.1

12 months ago

5.1.0

12 months ago

4.7.1

1 year ago

4.6.1

1 year ago

4.6.0

1 year ago

5.6.5

10 months ago

5.6.4

10 months ago

5.6.3

10 months ago

5.2.7

12 months ago

5.6.2

10 months ago

5.2.6

12 months ago

5.6.1

10 months ago

5.2.5

12 months ago

5.6.0

10 months ago

5.2.4

12 months ago

5.2.3

12 months ago

5.2.2

12 months ago

5.2.1

12 months ago

5.2.0

12 months ago

4.6.2

1 year ago

4.5.3

1 year ago

4.5.2

1 year ago

4.5.0

1 year ago

4.5.1

1 year ago

4.4.5

1 year ago

4.4.1

2 years ago

4.0.5

2 years ago

4.4.0

2 years ago

4.0.4

2 years ago

4.4.3

2 years ago

4.4.2

2 years ago

4.0.6

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.4.4

2 years ago

4.3.2

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

3.11.4

2 years ago

3.11.3

2 years ago

3.11.6

2 years ago

3.9.0

2 years ago

3.11.8

2 years ago

3.11.7

2 years ago

3.11.9

2 years ago

3.10.1

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.10.3

2 years ago

3.10.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

3.11.0

2 years ago

3.11.1

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

3.7.4

2 years ago

3.7.3

2 years ago

3.7.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

1.1.19

2 years ago

3.1.1

2 years ago

1.1.18

2 years ago

3.1.0

2 years ago

1.1.17

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.5.0

2 years ago

3.1.4

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

3.1.8

2 years ago

3.4.0

2 years ago

3.4.1

2 years ago

3.0.0

2 years ago

1.0.33

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

3.3.0

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.0

3 years ago