# jsx-dom-builder

> this package is deprecated and no longer will be updated. a new version made from scratch, that does not have some issues this package has is available here https://www.npmjs.com/package/magic-dom ## Description This is a JSX library, with reactivity, c

Latest version **4.0.11** (published 2023-07-31) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install jsx-dom-builder
pnpm add jsx-dom-builder
yarn add jsx-dom-builder
bun add jsx-dom-builder
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.0.11 |
| Published | 2023-07-31 |
| First published | 2022-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 41 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | thiago-kaique |
| Keywords | front-end, dom, jsx, vite |

## Links

- npm: https://www.npmjs.com/package/jsx-dom-builder
- Repository: https://github.com/Thiago099/jsx-dom-builder
- Homepage: https://github.com/Thiago099/jsx-dom-builder#readme
- Issues: https://github.com/Thiago099/jsx-dom-builder/issues
- npm.io page: https://npm.io/package/jsx-dom-builder

## Dependencies (2)

- [acorn](https://npm.io/package/acorn.md) 8.8.2
- [escodegen](https://npm.io/package/escodegen.md) 2.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.11 (latest) — 2023-07-31
- 4.0.10 — 2023-05-13
- 4.0.9 — 2023-05-13
- 4.0.8 — 2023-05-13
- 4.0.7 — 2023-03-27
- 4.0.6 — 2023-03-26
- 4.0.5 — 2023-03-26
- 4.0.4 — 2023-03-26
- 4.0.3 — 2023-03-26
- 4.0.2 — 2023-03-26
- 4.0.1 — 2023-03-26
- 4.0.0 — 2023-03-26
- 3.4.0 — 2023-03-26
- 3.3.0 — 2023-03-25
- 3.2.0 — 2023-03-18
- … 75 more at https://npm.io/package/jsx-dom-builder/versions

## README

# jsx-dom-builder

## Deprecated
this package is deprecated and no longer will be updated. a new version made from scratch, that does not have some issues this package has is available here https://www.npmjs.com/package/magic-dom
## Description
This is a JSX library, with reactivity, components, input synchronizing and you can fully edit every JSX variable and append it to any element.
Alternatively you can use anything you normally would with the original html dom element. to make it reactive you add a $ at the start of the property
## Instalation

You can get started by running the following commands
```
git clone https://github.com/Thiago099/basic-jsx-dom-builder-project your-project-name
cd your-project-name
npm install
```

You can do the same but with this repository if you want unit tests
```
git clone https://github.com/Thiago099/basic-jsx-dom-builder-project-with-tests your-project-name
```

you can start the project with
```
npm run dev
```
and build it with
```
npm run build
```
# Example

## From

![image](https://user-images.githubusercontent.com/66787043/215284801-698eb143-87e0-4983-ac0b-34f6f3e85688.png)


[Web page with this interactive example](https://thiago099.github.io/jsx-dom-builder-form-example/)

[Full source code](https://github.com/Thiago099/jsx-dom-builder-form-example)


```js

// import the style
import './style.css'

// The effect object can be used just like a normal object.
// It will update the the dom
var data = state(
{
    name:"pedro",
    age:20
});


// A sample submit method that is called when you click on the button
function submit()
{
    alert("Submit logic here")
}

const app = 
<div class="main-container">
    <div>
        <div class="input-group">
            <div class="input-container half">
                <label>Name:</label>
                <input type="text" model={data.name}></input>
            </div>
            <div class="input-container half">
                <label>Age:</label>
                <input type="text" model={data.age}></input>
            </div>
        </div>
        <div class="tooltip">The model parameter makes the input in sync with any variable.</div>
    </div>
    <div class="card">
        {/* You can add value to the dom that will update in the same
          * occasions as the model */}
        <div> Name: {data.name} </div>
        <div> Age: {data.age} </div>
    </div>
    <div class="tooltip">You can also add them directly in the element that they will update either using effect or manually using the "element.$update()".</div>
    <div class="footer-button-container">
        { /* You can add events, using the property on: and the name of the event */ }
        <button on:click={submit}> Submit </button>
    </div>
</div>

// The $parent function will append an element to another element, in this case the body
app.$parent(document.body)
```

## Editing the element after its creation and an alternative to state

you can edit every object after its creation, and instead of using state you can manually update the elements

![image](https://user-images.githubusercontent.com/66787043/214976038-8a1d6937-6630-4e7b-bcd3-6f83f4a7af72.png)

[Web page with this interactive example](https://thiago099.github.io/jsx-dom-builder-random-color-example/)

[Full source code](https://github.com/Thiago099/jsx-dom-builder-random-color-example)

```js

import './style.css'

function generate_random_colors()
{
    var random_number = Math.floor(Math.random()*16777215);
    var random_color = '#' + random_number.toString(16)
    var inverse = '#' + (16777215-random_number).toString(16)
    return [random_color, inverse]
}

const [random_color,inverse] = generate_random_colors()
var foreground = random_color
var background = inverse

generate_random_colors()

const app = <div class="main-item" draggable>Click on me to change the color</div>

app
.$style('background-color', background)
.$style('color', foreground)
.$on('click', () => {
    const [random_color,inverse] = generate_random_colors()
    foreground = random_color
    background = inverse
    // The update function does manually what the effect does automatically, after calling the update function
    // The dynamic part of the element and its children will be updated
    app.$update()
})

app.$parent(document.body)
```


## other examples

### ref

```js

import './style.css'
// If you don't want to set the attributes directly on the html
// you can use ref to acess a element that is not the root of your subtree

const title = ref()
const app =
<div>
    <h1 ref={title}>Hello, world!</h1>
</div>

var color = "red"
// Then you can edit any of its attributes just like you would with the root
title
    .$style("color", color)
    .$on("click",()=>{
        color = "blue"
        title.$update()
    })

app.$parent(document.body)
```

### Components

you can create components with the following syntax
```js
export default function Card({title}, ...children) {
    return (
    <div class="card">
        <div class="card-header">
            {title}
        </div>
        <div class="card-body">
            {children}
        </div>
    </div>
    )
}
```
then you can use it like that
```js
import Card from '@/components/Card.jsx'
const app = <div>
    <Card title="My card title">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
        <p>Quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
    </Card>
</div>
```

the functions you can call for each element
```js
my_element
    // adds a class to a element
    .$class("my-class")
    // If the value is an expression, the class will be replaced by the current value every time the element updates
    .$class(my_var)
    
    // Appends the element to a parent that can be either a dom element or a jsx-dom-builderjsx-dam-builder element
    .$parent(element2)
    
    // Listens to a event (calls the element. addEventListener)
    .$on("click",e=>{
        console.log("element1 was clicked")
    })
    
    // Add children to an element that can be either a dom element, jsx-dom-builder element, string, object, or an array of either of them combined
    .$child(<div></div>)
    
    // Synchronizes the value to a variable
    .$model(data)
    
    // Replaces the element style for the following style
    // If the value is an expression, it will be replaced every time the element updates
    .$style("background-color:red")
    
    // Sets a single instance of the style of the element
    // Will also update
    .$style("color","blue")
    
    // Removes the element from the dom
    .$remove()
```

The router feature allows you to create a single page application with the following syntax
```js

const container = Router({
    '/': () => import("./home"), 
    'about': () => import("./about")
})

container.$parent(document.body)
```

home.jsx
```js
export default Home
function Home({go})
{
    return (
        <div>
            <h1>Home</h1>
            <p>This is the home page</p>
            <button on:click={()=>go("about")}>About</button>
        </div>
    )
}
```

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