@narrative/control-flow
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Narrative

Travis CI Status Codecov License

Packages

Package Badges
@narrative/control-flow NPM Version NPM Downloads
@narrative/babel-plugin-compiler NPM Version NPM Downloads
@narrative/swc-plugin-compiler NPM Version NPM Downloads
@narrative/vite-plugin-compiler NPM Version NPM Downloads

Introduction

Narrative(abbreviated as nt) is a compiler tool for create neater control flow tags such as <If>/<For>/<Switch> for JSX/TSX.

Live demo is here.

Features

  • ⚡ No runtime code, 0kb size.

Table of Contents

Installation

Using with Babel

npm i @narrative/control-flow @narrative/babel-plugin-compiler

.babelrc:

{
  "plugins": ["@narrative/compiler"]
}

Using with SWC

npm install @narrative/control-flow @narrative/swc-plugin-compiler

Configure swc

{
  "jsc": {
    "experimental": {
      "plugins": [["@narrative/swc-plugin-compiler", {}]]
    }
  }
}

Basic Overview

import { useState, FC } from 'react';
import { If, ElseIf, Else, For, Empty, Switch, Case, Default } from '@narrative/control-flow';

const App: FC = () => {
  const [todos, setTodos] = useState([]);

  const addTodo = () => {
    setTodos(todos.concat(`Item ${todos.length}`));
  };

  return (
    <div className="app">
      <ul>
        <For of={todos}>
          {(todo, { index }) => (
            <If when={index > 5}>
              <li key={index}>{todo * 3}</li>
              <ElseIf when={index > 10}>
                <li key={index}>{todo * 4}</li>
              </ElseIf>
              <Else>
                <li key={index}>{todo * 5}</li>
              </Else>
            </If>
          )}
          <Empty>
            <li>No data</li>
          </Empty>
        </For>
      </ul>
      <ul>
        <For in={{ a: 1, b: 2, c: 3 }}>
          {(item, { key }) => <li key={key}>{item}</li>}
          <Empty>
            <li>No data</li>
          </Empty>
        </For>
      </ul>
      <Switch value={todos.length}>
        <Case is={1}>1</Case>
        <Case is={2}>2</Case>
        <Case in={[3, 4, 5]}>3/4/5</Case>
        <Default>More than 2</Default>
      </Switch>
    </div>
  );
};

The Origin of Name

🤖 RX-9 Narrative Gundam, ready to launch!

Narrative

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @narrative/control-flow

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

17 kB

Total Files

19

Last publish

Collaborators

  • joe_sky