Zoadian / plusnew

This component-framework believes that typesafety and good maintainability, is the right way. We think that we should do as little magic as possible and be very explicit and predictable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

plusnew Build Status Coverage Status

This Framework is build, because we believe that having typesafety and good debuggability are key. Why another framework? Because plusnew puts the Developer-Expierence first, and cares strongly about maintanability.

Plusnew keeps the magic as little as possible, and puts explicity first. E.G. when you write a line of code which changes the state, the dom will change immediately. This is important for predictability and testability.

Component

Components in plusnew just need a name and a render-function, the render-function gets called when a new instance of that component is created.

When props from the parent, or stores are changing, the render-function does not get called again. But only a subset that you can define with a closure.

Component-Types

Function-Factory

import plusnew, { component, Props } from 'plusnew';

type props = { value: string };

export default component(
  // ComponentName for debuggability enhancements
  'ComponentName',
  // The render-function which gets called at initialisation
  // and gets a Observer-Component which delivers the properties from the parent
  (Props: Props<props>) =>
      <>
        <span>some static content</span>
        <Props>{props =>
          // in props.value is the value you got from your parent-component
          // this render-function gets called each time when the parent gives you new properties
          <div>{props.value}</div>
        }</Props>
      </>
);

Class

import plusnew, { Component, Props } from 'plusnew';

type props = { value: string };

export default class AppComponent extends Component<props> {
  // ComponentName for debuggability enhancements
  static displayName: 'ComponentName';
  // The render-function which gets called at initialisation
  // and gets a Observer-Component which delivers the properties from the parent
  render(Props: Props<props>) {
    return (
      <>
        <span>some static content</span>
        <Props>{props =>
          // in props.value is the value you got from your parent-component
          // this render-function gets called each time when the parent gives you new properties
          <div>{props.value}</div>
        }</Props>
      </>
    );
  }
}

Props

The props-values aren't given to you directly, but as a "observer-component".

This given component expects a render-function as a child. This function will be called, each time when props are changing. This way you have more control of what will be checked for rerender.

Stores

Stores are used to persist your state and inform the components when the state changed. They consist of the initialStateValue and a reducer function.

The reducer function gets called, when a new action gets dispatched. This reducer gets the current state of the store and the action which just got dispatched. The new state will be that, what the reducer returns.

The reducer function is optional. When it is not given, the dispatched value is the new state of the store.

Each store has a Observer-Component, which expects a render-function as a child. When a store changes it's state, this render-function gets called.

import plusnew, { component, store } from 'plusnew';

const INITIAL_COUNTER_VALUE = 0;

export default component(
  'ComponentName',
  () => {
    const counter = store(INITIAL_COUNTER_VALUE, (previousState, action: number) => previousState + action);

    return (
      <div>
        <button
          onclick={(evt) => {
            evt.currentTarget; // Typescript knows that this element is a HTMLButtonElement, because of context
            counter.dispatch(1)
           }}
        />
        <button
          onclick={(evt) => counter.dispatch(2)}
        />
        <counter.Observer>{state =>
          // This function is the only part which gets executed on a state change
          <div>{state}</div>
        }</counter.Observer>
      </div>
    );
  },
);

Helper-Components

Portal

With portals you can render elements outside of your normal tree, but whereever you want.

import plusnew, { component, Portal } from 'plusnew';

export default component(
  'ComponentName',
  () =>
    <Portal target={document.getElementById('somewhere') as HTMLElement}>
      <div>your element is appended inside the #somewhere element</div>
    </Portal>,
);

Animate

The Animate-Component can take care of dom-elements which were mounted and of dom-elements to be unmounted. When a dom-element gets created the according elementDidMount or elementWillUnmount gets called, with the dom-element as a parameter.

Same goes for dom-elements which will get unmounted, simply return a resolved Promise when the animation is done and you want the dom-element to get actually be deleted.

Note: Dom-Elements inside Dom-Elements will not trigger the callbacks, only the most outer dom-elements will trigger the callback.

import plusnew, { component, Animate, store } from 'plusnew';

export default component(
  'ComponentName',
  () => {
    const show = store(true);

    return (
      <Animate
        elementDidMount={(element) => {
          // For example you can call the Element.animate function
          // https://developer.mozilla.org/en-US/docs/Web/API/Element/animate
          element.animate([{ opacity: '0' }, { opacity: '1' }], { duration: 3000 })
        }}
        elementWillUnmount={(element) => {
          // either return undefined, to delete the element immediately or a promise
          // e.g. https://developer.mozilla.org/en-US/docs/Web/API/Animation/finished
          return element.animate([{ opacity: '1' }, { opacity:  '0' }], { duration: 3000 }).finished;
        }}
      >
        <show.Observer>{state =>
          // When this button gets created it calls the elementDidMount
          // when it gets deleted the elementWillUnmount gets called beforehand 
          state === true && <button onclick={() => show.dispatch(false)}>Remove me :)</button>
        }</show.Observer>
      </Animate>
    );
  },
);

Async

The Async-Component is used for displaying asynchronous content. Return as a promise, and it will show the loading content, until the promise got resolved.

For example you can implement that a dom element gets created after a period of time, or you can lazyload another module and display it when it got loaded. The given Promise should get resolved with an JSX-Element you want to show.

Note: it is necessary that the promise gets resolved and not rejected, it is recommended to catch your own promise.

import plusnew, { component, Async } from 'plusnew';

const lazyModule = () => import('path/to/lazy/module')
                           .then(module => <module.default />)
                           .catch(() => <span>Could not load the module</span>)

export default component(
  'ComponentName',
  () =>
    <Async
      pendingIndicator={<span>Loading asynchronously a module</span>}
    >
      {lazyModule}
    </Async>
);

Idle

This component is for displaying expensive but lowpriority content. The children of this component will be displayed, when the browser is saying that it is in idle. Or until the application is telling, that it is urgent now.

import plusnew, { component, Idle } from 'plusnew';
import ExpensiveComponent from './ExpensiveComponent';

export default component(
  'ComponentName',
  () =>
    <Idle urgent={false}>
      <ExpensiveComponent /> {/* component will not be displayed immediately */}
    </Idle>,
);

About

This component-framework believes that typesafety and good maintainability, is the right way. We think that we should do as little magic as possible and be very explicit and predictable.

License:MIT License


Languages

Language:TypeScript 99.3%Language:JavaScript 0.7%