MVU The 28873rd most popular library on jsDelivr
Published: 2023-10-09 08:52:32

It seems that few people have been interested by trying  [MVU](https://www.jsdelivr.com/package/gh/artydev/mvu?tab=stats) :-)

In fact, as many of us I keep an eye on all the novelties in front end development, but the one I use because I feel comfortable with it, is MVU (derived from [DML] (https://efpage.de/DML/DML_homepage/))

To give you an idea, of it's simplicity look at the following example and see what can be accomplished with only the following four functions included in MVU.

Those functions aren the following :

- m
- dom
- udom
- render

**m** function is a tag elements fabric.
**dom** create an element from where all successives elements are attached to.
**udom** release the attachement from the latest **dom** call.
**render** as it's name suggest render the view on the page

You can rename them as your will, for example you coud use 'begin' and 'end' for dom an udom.

The name **m** is a credit to Mithril which I mainly use before MVU.

Let's see how to use them.
You can paste all the following snippets on this page [Flems](https://flems.io/#0=N4IgtglgJlA2CmIBcA2A7AOgIwE4A0IAZhAgM7IDaoAdgIZiJIgYAWALmLCAQMYD21NvEHIQAHlI8AThAAObAASkpPALwAdEOzazSSAPT6eUahgBWpKPFgQAblIzV4bfQHMW+2lLYBPK7f0wWwBXAAEAJgwABgwAZn0oCFIXIOCMYLAoDEhTC00APjF9SRl5fPVqCrFE2wVoDRBaWVkCioUFKoS7fO4QUmt4HjYIAXImcKQsABYQAF88GnpGZgte-kFhNlF15IVgBTA8BSg+Q4Vgk7OpYSspBVmFVQOQjABZADUAVQBuCoqdxQ1R4HAAUmhqmgAlH9qDUwSAABLWWB8KEw3r9BBDEbUMYgACsSCiAFookS5gsQHQGKIMDxSOReAIhCImBhSDYrO1gG1jklZLBaD4kAAjFE8ADWv2oswxA2xo1EWEmM1mAF1ZkA)


First, we import the functions from MVU.

```js
const { m, dom, udom, render} = mvu.MVU;
```
Here is how we create a 'div' elements fabric.

```js
const div = m("div") // return a function 
```

Calling the **div** function will result in the creation of a 'div' tag element.
(don't be intrigated by the syntax, we will see a better way later)

Example :

```js
const { m, dom, udom, render } = mvu.MVU;

const div = m("div")

div("Hello")

```

If you run the previous example, you will see the word 'hello' displayed on the page.
By default, the elements are appended to the body of the page.

If you want to attached to a predifined element, simply pass a reference to it to the **dom** function;

```html
<div id="app" style="border:1px solid red"> </div>
``` 

```js
const { m, dom, udom, render } = mvu.MVU;

const div = m("div")

let app = document.querySelector("#app")

dom(app)
  div("Hello")
udom()

```
Here is the display :

![A Circle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybhkjf1hytgwtz2c42ha.png)

Will you believe me if I say you, that with this in place you can build any complex UI you want ?

You are sceptic?

Let's create a very simple "colors slider" like this one :

![Widget Colors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pc808per3ahyl6ssnpx.png)

Let's go :

The circle :

```js
const { dom, udom, render, m } = mvu.MVU;
 
const div = m("div")

const mcircle = (size=40) => 
  div('', `width:${size}px;height:${size}px;background:red;border-radius:50%`);

```
Result :

![A Circle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ml9hxfn0zu1f7ss5i944.png)

Notice how  we easily create a "function component'  for the circle.

Let's add the 'slider component'

```js
const { dom, udom, render, m } = mvu.MVU;
 
const div = m("div");
const input = m("input")

const mcircle = (size=40) => 
  div('', `width:${size}px;height:${size}px;background:red;border-radius:50%`);
  
  
const slider = (min, max, value) => 
  input("", {type : "range", style:"display:block", max, min, value});
  
mcircle()
slider(0, 255, 128)


```
Result:

![SliderComponent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/naj82qyheqgwlln65a0i.png)

Let's wrap our components in an App component, and we are done.

```js
const { dom, udom, m } = mvu.MVU;
 
const div = m("div");
const input = m("input")

const mcircle = (size=40) => 
  div('', `width:${size}px;height:${size}px;background:red;border-radius:50%`);
  
const slider = (min, max, value) => 
  input("", {type : "range", style:"display:block", max, min, value});
  
function setColor(elt, R, G, B){ 
  elt.style.backgroundColor = `rgb(${R.value},${G.value},${B.value})`
}; 

function App () {
  const circle = mcircle ();
  const [R, G, B] = [slider(0, 255, 128), slider(0, 255, 23), slider(0, 255, 67)];
  [R, G, B].forEach((s) => {s.oninput = () => setColor(circle, R, G, B)});
  setColor(circle, R, G, B);
}

App()

```

Notice how we can use multiple sliders components and get a reference with all of them through the R, V and B variables.

![Sliders](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25c8htn2e74jrw1ajqnp.png)

You can test the code here [Colors](https://flems.io/#0=N4IgZglgNgpgziAXAbVAOwIYFsZJAOgAsAXLKEAGhAGMB7NYmBvAHjmoCcIAHYgAjgdqAXgA6IEsW5xEAelnUAJmnwArOIphQIANw740MYrIDmhWRg7EAnpp2ysOgK4ABAEz4ADPgDMsxRBwxo5O+E5YivhYECrq4gB8LLLsXLzxomgZLAE6fBCKYiAY3NwJGXx8Wf668ZQgcFow1MQQ9AiIIAAsiABsIAC+FOjYuB1qCFR0DEzEeFNBfMB8irRYFHxOK2t8WHz9fMI7zvgAsgBqAKoA3OUZ8-w5BzsAFOI54gCUN2j3eWjcTn4hywrxAMQBxE+GTubX4WGoECEsCezzgEAAXjBhJ1PB8DvFKmgKjlngByUnrAAGAHd8sRCIgACTANGY-rcAAeV0IMAgZmITJZGJg7K5ACMMNQANYmDi0JxoRSIDgwRRXMW0DiaDgAWg4GACThkAFZPABSSlfcqE35wbTalHRNDrLAYDnrHQYKBOGB44QE63gwGg8TrYA2bgwPiISogfVoEwwUMCGywRBvQLcKAYayIMVQWjS5Ou907GIer0+-pWomEsAK5qtIkNYgAYVoBY4zy0xHWACV1gBxdYAIQ+S2tPfwQWssHwEulsvlivbnaelI4JjFz2Zffwnu9IoozMH+8rR+ZI7Ph+rlIy-SuhIy9Z+LXofAAgiU+M88cBrb8CJIlGwJAdQyK-t8FS-MgA58MOfAjgAuk8yB2vkMBdp46xuMaxrrAAjG4AAcHzrOh2rPNhfC4fhNE+GRAj2phVE4Xh6w9AA7B8SFQXwsFDqOSH4GAmoAKKSoQzyon6BIsvg9BBkCP6yQIRirpqzxgbA-aCYhHzVnxLYaV22kwLp8GjjW-TQmgX7cL+GR1A0sCNm0eBuD4iCeAMQwgJgOB4Pg1BwBMND0IwzAdAMSH9EAA)

We have created a simple component, it'up to you to create new ones, assemble them, and create great UI's.


Tips :

Tao, creator of the astounishing VanJS, got a very interesting idea in order to create elements.

Let's implement a very very simplified implementation for MVU


```js
const { dom, udom, m } = mvu.MVU;
 
const handler = {
  get : function (target, name) {
    return m(name)
  }
}

let tags = new Proxy({}, handler)

const { h1 } = tags

h1("Hello", style='background:blue;color:yellow')
```

It'way better than :

```js
const h1 = m("h1")
```  

You can test it here : [ProxyTags](https://flems.io/#0=N4IgtglgJlA2CmIBcAOADAGhAMwggzsgNqgB2AhmIkiAHQAWALmLCFgMYD2pj8PyIADz52AJwgAHRgAJ8o9gF4AOiCaMJ+JAHot7KKVoArfFHiwIAN1G1S8RloDm9LeVGMAnqYtawFgK4AAgBMtGi0AMxaUBD49r5+tH5gULSQBsYqAHyCWiLiUplKpEWC0RbS0Mog5BISWUXS0iVRlplsIPhm8OyMENyENAAsSABsIAC+GGSU1HTG7Vw8fIwCi7HSwNJQnGAY0n7bu9Jg0uPSCsf+tACyAGoAqgDcDUVrMvTkpHDwoucbDdIHHZpEhpNg-KQen1SNIABSMVxAxh7ChUACU-xhjWkojsflEMLAsNR8DRAPGRQpxVICBkCIc+D+tgA7tIAAqiTgAD3csOAk2kHy+CFEZOpbw2goAjKc-vT8EUivQpbCVAAJMywTgqPaxdwIBQAcgARuR2ABrByciFQJDG2B+eCPLha0RIdyazjMw1i9qdBBQ-oCKXhJBoAC0aDDEymIBJAlo7HwhA43F4-BoEwAuuMgA).