Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 2.16 KB

readme.md

File metadata and controls

61 lines (45 loc) · 2.16 KB

Standard library

These are constructs for control-flow, lifecycles, context, error boundaries, and other higher-level ideas beyond basic JSX and reactive state.

Many of these are unimplemented, but are foreseen because they're popular in other libraries; often popularized by React. I won't implement unnecessary constructs, personally. If I don't use it you won't find it here. I'm also more aligned to use JS constructs rather than JSX DSL like Solid/React do, hence having when() as a function.

Implemented, though not necessarily right here:

Again, these will be implemented as necessary...

when<T>(condition: ($: SubToken) => T, views: { [key: T]: () => El }): Wire<El | undefined>

Uses condition function to build and return a wire. This wire matches the output of the condition to a key in views to return DOM content. Try returning readable values such as "T"/"F" as shown below. When a view is unrendered, all its nested wires are paused so the view doesn't keep updating in the background.

Usage:

import { h } from 'haptic';
import { signal, wire } from 'haptic/state';
import { when } from 'haptic/stdlib';

const data = signal({
  count: 0,
  countNext: wire($ => data.count($) + 1),
});

const Page = () =>
  <div>
    <p>Content below changes when <code>data.count > 5</code></p>
    <button onClick={() => data.count(data.count() + 1)}>
      Increment to {wire(data.countNext)}
    </button>
    {when($ => data.count($) > 5 ? "T" : "F", {
      T: () => <p>There have been more than 5 clicks</p>,
      F: () => <p>Current click count is {wire(data.count)}</p>,
    })}
  </div>;

document.body.appendChild(<Page/>);

When it says "There have been more than 5 clicks" the wire(data.count) wire won't run anymore even when data.count is being written to.