# riu-component

> A component-based JavaScript framework for building user interfaces. It is highly flexible and compatible with other frameworks and libraries. You can easily integrate it in existing projects or use it to create a web app from scratch. The framework provi

Latest version **1.0.6** (published 2021-02-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install riu-component
pnpm add riu-component
yarn add riu-component
bun add riu-component
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2021-02-23 |
| First published | 2021-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 57.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mishieck Mwale |
| Maintainers | mishieck |
| Keywords | riu-component, javascript-framework, javascript-library, framework, library, riu |

## Links

- npm: https://www.npmjs.com/package/riu-component
- Repository: https://github.com/riu-web/riu-component
- Homepage: https://github.com/riu-web/riu-component#readme
- Issues: https://github.com/riu-web/riu-component/issues
- npm.io page: https://npm.io/package/riu-component

## Dependencies (1)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.12.13

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@atlaskit/theme](https://npm.io/package/@atlaskit/theme.md) — 402.0K weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2021-02-23
- 1.0.5 — 2021-02-15
- 1.0.4 — 2021-02-14
- 1.0.3 — 2021-02-14
- 1.0.2 — 2021-02-10
- 1.0.1 — 2021-02-10
- 1.0.0 — 2021-02-08

## README

<P align="center">
  <img src="https://user-images.githubusercontent.com/57598264/105575616-978ba400-5d75-11eb-81d0-6b6da44b8b2f.png" />
  <h1 align="center" style="font-weight:bold;">RIU Component</h1>
</p>

A component-based JavaScript framework for building user interfaces. It is highly flexible and compatible with other frameworks and libraries. You can easily integrate it in existing projects or use it to create a web app from scratch. The framework provides a number of ways to manage resources in terms of reusability, loading and more. It provides built in support for plugins, allowing you to extend its functionality easily. RIU Component provides both declarative and procedural ways of building user interfaces.

## __Quick Start__

### __Introduction__

The easiest way to get started with RIU Component is using a CDN. Another way is by using NPM.

### __Installation__

#### NPM

`$ npm i riu-component --save`

#### CDN

```html
<script src="https://unpkg.com/riu-component"></script>
```

or

```html
<script src="https://cdn.jsdelivr.net/npm/riu-component"></script>
```

### __Importing__

```javascript
import { riuComponent } from "riu-component";
```

### __Usage__

A CDN will be used for this guide. To create a component, you can use either `riuComponent` or `$riu`. The method `$riu` is just an alias for `riuComponent`. We are going to use `$riu` because it is shorter and the sign `$` provides an easy way of indicating that the function is used to create a component. The variables for components will be prefixed with `$` in order to differenciate them from other variables.

The easiest way to create and use components is by using a schema. The schema contains attributes that are used to create a component. A schema will be used in this guide.

HTML:
```html
<div id="hello-world"></div>
```

JS:
```javascript
(async () => {
  const markup = `
    <main>
      <button>Say Hello</button>
    </main>
  `;

  const styles = `
    :scope {
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    #say-hello {
      padding: 1rem;
      font-size: 2rem;
      font-weight: bold;
      color: #0cffff;
      background-color: #7f3fff;
      cursor: pointer;
    }
  `;

  const events = {
    "button": [
      {
        type: "click",
        listener: function (event) {
          alert("Hello World!");
        }
      }
    ]
  };

  const schema = { markup, styles, events };
  const $home = await $riu(schema);
  $home.replace("#hello-world");
})();
```

The HTML contains a `<div>` element with id `hello-world`. This is the element that will be replaced by our component. In the JavaScript, we crated the component and added to the DOM. A couple of steps were taken to achieve this. Let us look at each one of the steps.

#### 1. Create Markup

The variable `markup` was declared and a template string containing the markup was assigned to it. This is the markup that will be used to create the component. To make things easy, HTML was used. XML can be used too, but it involves a little more work.

#### 2. Create Styles

We put the styling for the component in a template string (`styles`) containing CSS. Notice the use of `:scope` to select the root element `<main>`. In standard CSS, the selector `:scope` selects the root element of the DOM no matter where the selector is used. In RIU Component, however, the selector always selects the root element of the component (in this case `<main>`). This is the only unusual behaviour of CSS selectors, the rest is normal CSS. The selector `#say-hello` selects the button.

#### 3. Create Events

To make the component reactive, we added a click event to the button. In `events`, a CSS selector (`#say-hello`) was used to select the button. A click event was put in an array. The array can contain any number of events. The property `type` specified that the event is a `click` event. The property `listener` specified an event listener for the event.

#### 4. Create Schema

A `schema` was created with the properties `markup`, `styles` and `events`.

#### 5. Create Component

The component was created using `$riu`. The schema was passed as an argument in to `$riu`. Notice the use of `await` when creating the component (`$home`). This is because `$riu` is asynchronous.

#### 6. Add Component Element to DOM

Finally, we inserted the component into the DOM using `$HelloWorld.replace`. The function inserts the component into the DOM by replacing the element (`div#hello-world`) specified by the CSS selector passed as the parameter.


### Insertions

Components, elements, markup, text and data can be inserted into a component. Items can be inserted using either attribute names or attribute values. Attribute values are used to insert data, while attribute names are used to insert other items.

#### Data

Data is inserted using attribute values that start with `@` and followed by the path to the value using object dot notation. Let us use `schema.utils.data` to insert data into an element's attribute.

```javascript
const markup = `
  <main>
    <button title="@data.title">Say Hello</button>
  </main>
`;

const data = { title: "Say Hello" };
const utils = { data };
const schema = { ..., utils };

// ...

```

The attribute `title` of  `<span>` will be updated to `Say Hello`. When you hover over `span`, the tootip will desplay "Say Hello". Note that `data` was added as a property of `utils`, which was the put in the `schema`.

#### Text

Let us change the text displayed on the button by programatically inserting text into the markup. This is done using the attribute `riu-txt`. The span element will be replaced with the text specified in the value of the attribute.

```javascript
const markup = `
  <main>
    <button title="@data.title">
      <span riu-txt="action"></span>
    </button>
  </main>
`;

const data = { title: "Hello" };
const texts = { action: "Say Hello" };
const utils = { data, texts };
const schema = { ..., utils };

// ...

```

The button will contain the same text as before. The value `action` of `riu-text` points to `schema.utils.texts.action`.

### Conditionals

Conditionals can be used to load, show and hide elements based on a particular condition. Conditionals are set on elements using attributes.

#### Loading

The loading conditional lets us load content when a particular condition has been met. Let us load content using lazy loading, which is one of the loading conditionals.

Markup:

```html
<div class="images">
  <img src="/image-1.png" />
  <img src="/image-2.png" />
  <img src="/image-3.png" riu-loading="lazy" />
</div>
```

Images `image-1.png` and `image-2.png` will be loaded instantly. However, `image-3.png` will be loaded only if the user scrolls down the the area the image is supposed to occupy. The attribute `riu-loading` has a normal string or a JSON string as its value. In this case, we have used a normal string with the value `lazy`, in which case the default values for lazy loading will be used. Check out [Loading](./documentation/conditionals.md#loading) for more information.

#### Display

You can display elements only if a particular condition is met using the display conditional. Let us see how we can make a button not to be displayed on large screens.

```html
<button class="menu-toggle" riu-display='{"dislay": "none", "conditions": [{"query": "(min-width: 992px)"}]'>
  <img src="./menu.svg" />
</button>
```

The value of attribute `riu-display` is JSON object. The object has two attributes, `type` and `conditions`. The type of display of the element is indicated by `type`. The conditions that must be met for the element to have a display value specified in `type` are put in `conditions`, an array. The array can have any number of conditions. We have used a media query to make the button not to be displayed on screens with width atleast `992px`. Check out [Display](./documentation/conditionals.md#display) for more information.

### Collections

Collections are a way to turn a single element into a collection of elements containing variants of the element.

```javascript
const markup = `
  <ul>
    <li riu-multiple='{"data": "@data.users"}'>
      <span riu-text="@datum"></span>
    </li>
  </ul>
`;

const users = ["user-1@email.com", "user-2@email.com", "user-3@email.com"];
const data = { users };
const utils = { data };
const schema = { utils };

// ...

```

In this case `@datum` refers to each item in the array `schema.utils.data.users`. The list will contain three items, each one of them containing a user email.

## Contributing

Refer to our [Contributing to RIU Component](https://github.com/riu-web/riu-component/blob/main/CONTRIBUTING.md) guide for information concerning contributing.

## Contact Us

You can contact us via email at <riucomponent@gmail.com>.

## License

This framework is under [MIT license](http://choosealicense.com/licenses/mit/).

Copyright (c) 2021, __Mishieck Mwale__.

---
_Source: https://npm.io/package/riu-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
